-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathbrownies.min.js
1 lines (1 loc) · 9.02 KB
/
brownies.min.js
1
(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?factory(exports):typeof define==="function"&&define.amd?define(["exports"],factory):factory(global.brownies={})})(this,function(exports){"use strict";var options=Symbol("options");var cookies$1=function(data,opt){function defaults(obj,defs){obj=obj||{};for(var key in defs){if(obj[key]===undefined){obj[key]=defs[key]}}return obj}defaults(cookies$1,{expires:365*24*3600,path:"/",secure:window.location.protocol==="https:",nulltoremove:true,autojson:true,autoencode:true,encode:function(val){return encodeURIComponent(val)},decode:function(val){return decodeURIComponent(val)},fallback:false});opt=defaults(opt,cookies$1);function expires(time){var expires=time;if(!(expires instanceof Date)){expires=new Date;expires.setTime(expires.getTime()+time*1e3)}return expires.toUTCString()}if(typeof data==="string"){var value=document.cookie.split(/;\s*/).map(opt.autoencode?opt.decode:function(d){return d}).map(function(part){return part.split("=")}).reduce(function(parts,part){parts[part[0]]=part.splice(1).join("=");return parts},{})[data];if(!opt.autojson)return value;var real;try{real=JSON.parse(value)}catch(e){real=value}if(typeof real==="undefined"&&opt.fallback)real=opt.fallback(data,opt);return real}for(var key in data){var val=data[key];var expired=typeof val==="undefined"||opt.nulltoremove&&val===null;var str=opt.autojson?JSON.stringify(val):val;var encoded=opt.autoencode?opt.encode(str):str;if(expired)encoded="";var res=opt.encode(key)+"="+encoded+(opt.expires?";expires="+expires(expired?-1e4:opt.expires):"")+";path="+opt.path+(opt.domain?";domain="+opt.domain:"")+(opt.secure?";secure":"");if(opt.test)opt.test(res);document.cookie=res}return cookies$1};var subscriptions=[];const get=(target,key)=>{if(key===Symbol.iterator)return getIterator();if(key===options)return cookies$1;const value=cookies$1(key);return typeof value==="undefined"?null:value};const set=(target,key,value=null)=>{if(key===options){for(let key in value){cookies$1[key]=value[key];return true}}cookies$1({[key]:value});subscriptions.filter(sub=>sub.key===key).forEach(({check:check})=>check());return true};const getAll=()=>{const pairs=document.cookie.split(";");const cookies={};for(var i=0;i<pairs.length;i++){const pair=pairs[i].split("=");const key=(pair[0]+"").trim();try{cookies[key]=get({},key)}catch(error){console.warn(error)}}return cookies};const getIterator=()=>{const all=Object.values(getAll());return function*(){while(all.length)yield all.shift()}};const getOwnPropertyDescriptor=()=>({enumerable:true,configurable:true});const ownKeys=()=>Object.keys(getAll());const traps={get:get,set:set,deleteProperty:set,getOwnPropertyDescriptor:getOwnPropertyDescriptor,ownKeys:ownKeys};var cookies=new Proxy({},traps);var stringify=(obj,opts)=>{if(!opts)opts={};if(typeof opts==="function")opts={cmp:opts};var space=opts.space||"";if(typeof space==="number")space=Array(space+1).join(" ");var cycles=typeof opts.cycles==="boolean"?opts.cycles:false;var replacer=opts.replacer||function(key,value){return value};var cmp=opts.cmp&&function(f){return function(node){return function(a,b){var aobj={key:a,value:node[a]};var bobj={key:b,value:node[b]};return f(aobj,bobj)}}}(opts.cmp);var seen=[];return function stringify(parent,key,node,level){var indent=space?"\n"+new Array(level+1).join(space):"";var colonSeparator=space?": ":":";if(node&&node.toJSON&&typeof node.toJSON==="function"){node=node.toJSON()}node=replacer.call(parent,key,node);if(node===undefined){return}if(typeof node!=="object"||node===null){return JSON.stringify(node)}if(isArray(node)){var out=[];for(var i=0;i<node.length;i++){var item=stringify(node,i,node[i],level+1)||JSON.stringify(null);out.push(indent+space+item)}return"["+out.join(",")+indent+"]"}else{if(seen.indexOf(node)!==-1){if(cycles)return JSON.stringify("__cycle__");throw new TypeError("Converting circular structure to JSON")}else seen.push(node);var keys=objectKeys(node).sort(cmp&&cmp(node));var out=[];for(var i=0;i<keys.length;i++){var key=keys[i];var value=stringify(node,key,node[key],level+1);if(!value)continue;var keyValue=JSON.stringify(key)+colonSeparator+value;out.push(indent+space+keyValue)}seen.splice(seen.indexOf(node),1);return"{"+out.join(",")+indent+"}"}}({"":obj},"",obj,0)};var isArray=Array.isArray||function(x){return{}.toString.call(x)==="[object Array]"};var objectKeys=Object.keys||function(obj){var has=Object.prototype.hasOwnProperty||function(){return true};var keys=[];for(var key in obj){if(has.call(obj,key))keys.push(key)}return keys};const js="JSSTR:";const pack=str=>{if(typeof str!=="string"){str=js+stringify(str)}return str};const unpack=str=>{if(str&&typeof str==="string"&&str.slice(0,js.length)===js){return JSON.parse(str.slice(js.length))}return str};const getAll$1=()=>{const all={};for(var key in localStorage){if(local[key]!==null){all[key]=local[key]}}return all};const local=new Proxy({},{get:(target,key)=>{if(key===Symbol.iterator){const all=Object.values(getAll$1());return function*(){while(all.length)yield all.shift()}}return unpack(localStorage.getItem(key))},set:(target,key,value)=>{localStorage.setItem(key,pack(value));subscriptions.filter(sub=>sub.key===key).forEach(({check:check})=>check());return true},deleteProperty:(target,key)=>{localStorage.removeItem(key);subscriptions.filter(sub=>sub.key===key).forEach(({check:check})=>check());return true},getOwnPropertyDescriptor(k){return{enumerable:true,configurable:true}},ownKeys(target){return Object.keys(getAll$1())}});const getAll$2=()=>{const all={};for(var key in sessionStorage){if(local$2[key]!==null){all[key]=local$2[key]}}return all};const local$2=new Proxy({},{get:(target,key)=>{if(key===Symbol.iterator){const all=Object.values(getAll$2());return function*(){while(all.length)yield all.shift()}}return unpack(sessionStorage.getItem(key))},set:(target,key,value)=>{sessionStorage.setItem(key,pack(value));subscriptions.filter(sub=>sub.key===key).forEach(({check:check})=>check());return true},deleteProperty:(target,key)=>{sessionStorage.removeItem(key);subscriptions.filter(sub=>sub.key===key).forEach(({check:check})=>check());return true},getOwnPropertyDescriptor(k){return{enumerable:true,configurable:true}},ownKeys(target){return Object.keys(getAll$2())}});class Store{constructor(dbName="keyval-store",storeName="keyval"){this.storeName=storeName;this._dbp=new Promise((resolve,reject)=>{const openreq=indexedDB.open(dbName,1);openreq.onerror=(()=>reject(openreq.error));openreq.onsuccess=(()=>resolve(openreq.result));openreq.onupgradeneeded=(()=>{openreq.result.createObjectStore(storeName)})})}_withIDBStore(type,callback){return this._dbp.then(db=>new Promise((resolve,reject)=>{const transaction=db.transaction(this.storeName,type);transaction.oncomplete=(()=>resolve());transaction.onabort=transaction.onerror=(()=>reject(transaction.error));callback(transaction.objectStore(this.storeName))}))}}let store;function getDefaultStore(){if(!store)store=new Store;return store}function get$1(key,store=getDefaultStore()){let req;return store._withIDBStore("readonly",store=>{req=store.get(key)}).then(()=>req.result)}function set$1(key,value,store=getDefaultStore()){return store._withIDBStore("readwrite",store=>{store.put(value,key)})}function del(key,store=getDefaultStore()){return store._withIDBStore("readwrite",store=>{store.delete(key)})}function keys(store=getDefaultStore()){const keys=[];return store._withIDBStore("readonly",store=>{(store.openKeyCursor||store.openCursor).call(store).onsuccess=function(){if(!this.result)return;keys.push(this.result.key);this.result.continue()}}).then(()=>keys)}const getAll$3=async()=>{const all={};const ks=await keys();await Promise.all(ks.map(async key=>{all[key]=await get$1(key)}));return all};const db=new Proxy(getAll$3,{get:async(target,key)=>{const value=await get$1(key);return typeof value==="undefined"?null:value},set:(target,key,value)=>{set$1(key,value);subscriptions.filter(sub=>sub.key===key).forEach(({check:check})=>check());return true},deleteProperty:(target,key)=>{del(key);subscriptions.filter(sub=>sub.key===key).forEach(({check:check})=>check());return true}});const isBasic=value=>!value||["boolean","number","string"].includes(typeof value);const clone=value=>{if(isBasic(value))return value;return JSON.parse(stringify(value))};var subscribe=(obj,key,cb)=>{let prev=obj[key]&&obj[key].then?obj[key].then(clone):clone(obj[key]);const check=()=>{const value=obj[key];if(prev&&prev.then||value&&value.then){return Promise.all([prev,value]).then(([previous,value])=>{if(stringify(previous)===stringify(value))return;cb(value,previous);prev=clone(value)})}if(stringify(prev)===stringify(value))return;cb(value,prev);prev=clone(value)};const id=setInterval(check,100);subscriptions.push({id:id,key:key,check:check,cb:cb});return id};var unsubscribe=id=>{if(typeof id==="number"){return clearInterval(id)}return subscriptions.filter(({cb:cb})=>cb===id).map(sub=>clearInterval(sub.id))};exports.cookies=cookies;exports.local=local;exports.session=local$2;exports.db=db;exports.options=options;exports.subscribe=subscribe;exports.unsubscribe=unsubscribe;Object.defineProperty(exports,"__esModule",{value:true})});