@meanie/angular-storage
Version:
An Angular service for interacting with local storage, session storage and cookie storage
3 lines (2 loc) • 4.87 kB
JavaScript
!function(e,t,n){"use strict";t.module("Storage.Service",["Storage.MemoryStorage.Service","Storage.CookieStorage.Service","Storage.SessionStorage.Service","Storage.LocalStorage.Service"]).provider("$storage",function(){this.prefix="",this.enabledStorageEngines=["local","session","cookie","memory"],this.defaultStorageEngine="local",this.setPrefix=function(e){return e&&"."!==e.substr(-1)&&(e+="."),this.prefix=e,this},this.setEnabledStorageEngines=function(e){return t.isArray(e)&&(this.enabledStorageEngines=e),this},this.setDefaultStorageEngine=function(e){return this.defaultStorageEngine=e,this},this.$get=["$parse","$injector","$log",function(e,n,r){function o(e){return"$"+e+"Storage"}function i(e){if(m[e])return m[e];var a=o(e),u=n.get(a);if("memory"!==e&&!u.isSupported()){var c=void 0;t.isFunction(u.getFallbackEngine)&&(c=u.getFallbackEngine()),c=c||"memory",r.warn("Storage engine",e,"not supported in this browser.","Using fallback engine",c,"instead."),u=i(c)}return m[e]=u}function a(e){return e?f+e:""}function u(e){return/^-?\d+\.?\d*$/.test(e.replace(/["']/g,""))}function c(e){return"false"===e||"true"===e}function s(e){return"{"===e.charAt(0)||"["===e.charAt(0)}function g(e){return t.isUndefined(e)||null===e?null:t.isObject(e)||t.isArray(e)||"boolean"==typeof e||t.isNumber(Number(e)||e)?t.toJson(e):e}function l(e){if(!e||null===e||"null"===e)return null;if(s(e)||c(e)||u(e))try{return t.fromJson(e)}catch(e){return null}return e}var f=this.prefix,S=this.enabledStorageEngines,d=this.defaultStorageEngine,m={};if(-1===S.indexOf(d))throw new Error("Default storage engine",d,"is not enabled.");t.forEach(S,function(e){var t=o(e);if(!n.has(t))throw new Error("Storage engine",e,"does not exist.","Make sure the service",t,"is included as a dependency.")});var h=function(e){this.engine=i(e)};h.prototype.set=function(e,t){if(!(e=a(e)))return!1;t=g(t);try{return this.engine.set(e,t),!0}catch(e){return!1}},h.prototype.get=function(e,t){if(void 0===t&&(t=null),!(e=a(e)))return t;try{var n=this.engine.get(e);return null===n?t:l(n)}catch(e){return t}},h.prototype.remove=function(e){if(!(e=a(e)))return!1;try{return this.engine.remove(e),!0}catch(e){return!1}},h.prototype.clear=function(e){e=f+(e||""),this.engine.clear(e)};var v=new h(d),w={};return w[d]=v,t.forEach(S,function(e){Object.defineProperty(v,e,{get:function(){return w[e]?w[e]:w[e]=new h(e)}})}),v}]})}(window,window.angular),function(e,t,n){"use strict";t.module("Storage.CookieStorage.Service",[]).factory("$cookieStorage",["$injector","$log",function(e,t){var n=void 0;return e.has("$cookies")?n=e.get("$cookies"):t.warn("Cookie storage requires the `ngCookies` module as a dependency"),{isSupported:function(){return!!n},getFallbackEngine:function(){return"memory"},set:function(e,t){n.put(e,t)},get:function(e){return n.get(e)},remove:function(e){n.remove(e)},clear:function(e){for(var t=e?new RegExp("^"+e):null,r=n.getAll(),o=r?Object.keys(r):[],i=0;i<o.length;i++)e&&!t.test(o[i])||n.remove(o[i])}}}])}(window,window.angular),function(e,t,n){"use strict";t.module("Storage.LocalStorage.Service",[]).factory("$localStorage",["$window",function(e){return{isSupported:function(){var t="___"+Math.round(1e7*Math.random());try{return e.localStorage.setItem(t,""),e.localStorage.removeItem(t),!0}catch(e){return!1}},getFallbackEngine:function(){return"cookie"},set:function(t,n){e.localStorage.setItem(t,n)},get:function(t){return e.localStorage.getItem(t)},remove:function(t){e.localStorage.removeItem(t)},clear:function(t){for(var n=t?new RegExp("^"+t):null,r=Object.keys(e.localStorage),o=0;o<r.length;o++)t&&!n.test(r[o])||e.localStorage.removeItem(r[o])}}}])}(window,window.angular),function(e,t,n){"use strict";t.module("Storage.MemoryStorage.Service",[]).factory("$memoryStorage",function(){var e={};return{isSupported:function(){return!0},getFallbackEngine:function(){return""},set:function(t,n){e[t]=n},get:function(t){return void 0===e[t]?null:e[t]},remove:function(t){void 0!==e[t]&&delete e[t]},clear:function(t){for(var n=t?new RegExp("^"+t):null,r=Object.keys(e),o=0;o<r.length;o++)t&&!n.test(r[o])||delete e[r[o]]}}})}(window,window.angular),function(e,t,n){"use strict";t.module("Storage.SessionStorage.Service",[]).factory("$sessionStorage",["$window",function(e){return{isSupported:function(){var t="___"+Math.round(1e7*Math.random());try{return e.sessionStorage.setItem(t,""),e.sessionStorage.removeItem(t),!0}catch(e){return!1}},getFallbackEngine:function(){return"memory"},set:function(t,n){e.sessionStorage.setItem(t,n)},get:function(t){return e.sessionStorage.getItem(t)},remove:function(t){e.sessionStorage.removeItem(t)},clear:function(t){for(var n=t?new RegExp("^"+t):null,r=Object.keys(e.sessionStorage),o=0;o<r.length;o++)t&&!n.test(r[o])||e.sessionStorage.removeItem(r[o])}}}])}(window,window.angular);
//# sourceMappingURL=angular-storage.min.js.map