2 lines
2.6 KiB
JavaScript
2 lines
2.6 KiB
JavaScript
// // Copyright (c) 2012 Florian H., https://git.spacen.net/js-coder https://git.spacen.net/js-coder/lostorage.js
|
|
!function(e,t){var n={isArray:Array.isArray||function(e){return Object.prototype.toString.call(e)==="[object Array]"},isPlainObj:function(e){return e===Object(e)},toArray:function(e){return Array.prototype.slice.call(e)},prepareArgs:function(e,t){return e=n.toArray(e),e.unshift(t),e},getObjKeyByValue:function(e,t){for(var n in e)if(e.hasOwnProperty(n)&&e[n]===t)return n},prepareReturn:function(t){return e[n.getObjKeyByValue(o,t)]},retrieve:function(e,n){return e==t?n:e},serialize:function(e){return JSON.stringify(e)},unserialize:function(e){return e==t?t:JSON.parse(e)}},r=function(){return r.get.apply(this,arguments)},i={storage:function(){return storage.get.apply(storage,arguments)},session:function(){return session.get.apply(session,arguments)}};r.set=function(e,t,r){if(n.isPlainObj(t))for(var i in t)t.hasOwnProperty(i)&&e.setItem(i,n.serialize(t[i]));else e.setItem(t,n.serialize(r));return n.prepareReturn(e)},r.invert=function(e,t){return this.set(e,t,!this.get(e,t))},r.add=function(e,t,n){return this.set(e,t,this.get(e,t)+parseInt(n,10))},r.increase=function(e,t,r){return this.add(e,t,n.retrieve(r,1))},r.decrease=function(e,t,r){return this.add(e,t,-n.retrieve(r,1))},r.concat=function(e,t,n){return this.set(e,t,this.get(e,t)+n)},r.push=function(e,t,r){var i=n.toArray(arguments),s=this.get(e,t,[]);return i.splice(0,2),s.push.apply(s,i),this.set(e,t,s)},r.extend=function(e,t,r,i){var s=this.get(e,t,{});if(n.isPlainObj(r))for(var o in r)r.hasOwnProperty(o)&&(s[o]=r[o]);else s[r]=i;return this.set(e,t,s)},r.remove=function(e,t){t=n.isArray(t)?t:n.toArray(arguments);for(var r=0,i=t.length;r<i;r++)delete e[t[r]];return n.prepareReturn(e)},r.empty=function(e){return e.clear(),n.prepareReturn(e)},r.get=function(e,r,i){i=i||t;if(n.isArray(r)){var s={};for(var o=0,u=r.length;o<u;o++){var a=r[o];s[a]=this.get(e,a,i)}return s}return n.retrieve(n.unserialize(e.getItem(r)),i)},r.all=function(e){var t={};for(var r=0,i=e.length;r<i;r++){var s=e.key(r);t[s]=n.unserialize(e.getItem(s))}return t};var s="set invert add increase decrease concat push extend remove empty get all".split(" "),o={storage:localStorage,session:sessionStorage};for(var u=0,a=s.length;u<a;u++){var f=s[u];for(var l in o){if(!o.hasOwnProperty(l))continue;var c=o[l];i[l][f]=function(e,t){return function(){var i=n.prepareArgs(arguments,t);return r[e].apply(r,i)}}(f,c)}}var h={storage:i.storage,session:i.session};typeof define=="function"&&define.amd?define(function(){return h}):typeof exports!="undefined"?module.exports=h:(e.storage=i.storage,e.session=i.session)}(window); |