ng-storage-local
Version:
An Angular module that makes Web Storage working in the Angular Way
16 lines (14 loc) • 8.67 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core")):"function"==typeof define&&define.amd?define("ng-storage-local",["exports","@angular/core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)["ng-storage-local"]={},e.ng.core)}(this,(function(e,t){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function o(e,t,o,r){return new(o||(o=Promise))((function(n,s){function a(e){try{c(r.next(e))}catch(e){s(e)}}function i(e){try{c(r.throw(e))}catch(e){s(e)}}function c(e){var t;e.done?n(e.value):(t=e.value,t instanceof o?t:new o((function(e){e(t)}))).then(a,i)}c((r=r.apply(e,t||[])).next())}))}function r(e,t){var o,r,n,s,a={label:0,sent:function(){if(1&n[0])throw n[1];return n[1]},trys:[],ops:[]};return s={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function i(s){return function(i){return function(s){if(o)throw new TypeError("Generator is already executing.");for(;a;)try{if(o=1,r&&(n=2&s[0]?r.return:s[0]?r.throw||((n=r.return)&&n.call(r),0):r.next)&&!(n=n.call(r,s[1])).done)return n;switch(r=0,n&&(s=[2&s[0],n.value]),s[0]){case 0:case 1:n=s;break;case 4:return a.label++,{value:s[1],done:!1};case 5:a.label++,r=s[1],s=[0];continue;case 7:s=a.ops.pop(),a.trys.pop();continue;default:if(!(n=a.trys,(n=n.length>0&&n[n.length-1])||6!==s[0]&&2!==s[0])){a=0;continue}if(3===s[0]&&(!n||s[1]>n[0]&&s[1]<n[3])){a.label=s[1];break}if(6===s[0]&&a.label<n[1]){a.label=n[1],n=s;break}if(n&&a.label<n[2]){a.label=n[2],a.ops.push(s);break}n[2]&&a.ops.pop(),a.trys.pop();continue}s=t.call(e,a)}catch(e){s=[6,e],r=0}finally{o=n=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,i])}}}Object.create;Object.create;var n=function(){function e(){}return e.prototype.setLocalStorage=function(e){return o(this,void 0,void 0,(function(){var t=this;return r(this,(function(o){switch(o.label){case 0:return[4,new Promise((function(o,r){e.storageKey&&e.storageType&&e.storageData?("JSON"==e.storageType?(t.convertJsonToString(e.storageData),t.saveDataInLocalStorage(e.storageKey,t.convertJsonToString(e.storageData))):"STRING"==e.storageType&&t.saveDataInLocalStorage(e.storageKey,e.storageData),o({error:!1,message:"data store successfully"})):r({error:!0,message:"Please check parameters"})}))];case 1:return[2,o.sent()]}}))}))},e.prototype.getLocalStorage=function(e){return o(this,void 0,void 0,(function(){var t=this;return r(this,(function(o){switch(o.label){case 0:return[4,new Promise((function(o,r){window.localStorage.getItem(e.storageKey)?"JSON"==e.storageType?o(t.convertStringToJson(window.localStorage.getItem(e.storageKey))):"STRING"==e.storageType&&o(window.localStorage.getItem(e.storageKey)):r({error:!0,message:e.storageKey+" No data found"}),r(new TypeError("Something Wrong"))}))];case 1:return[2,o.sent()]}}))}))},e.prototype.removeLocalStorageItem=function(e){return o(this,void 0,void 0,(function(){return r(this,(function(t){switch(t.label){case 0:return[4,new Promise((function(t,o){e?(window.localStorage.removeItem(e),t({error:!1,message:e+" removed successfully"})):o({error:!0,message:"Please pass key to remove"})}))];case 1:return[2,t.sent()]}}))}))},e.prototype.getLocalStorageLength=function(){return o(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,new Promise((function(e,t){window.localStorage.length>0?e({error:!1,session:window.localStorage.length}):t({error:!0,message:" No local storage stored"})}))];case 1:return[2,e.sent()]}}))}))},e.prototype.getLocalStorageAllKey=function(){return o(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,new Promise((function(e,t){if(window.localStorage.length>0)for(var o=[],r=0;r<window.localStorage.length;r++)o.push({keyIndex:r,keyName:window.localStorage.key(r)}),e(o);else t({error:!0,message:" No local storage stored"})}))];case 1:return[2,e.sent()]}}))}))},e.prototype.clearAllLocalStorage=function(){return o(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,new Promise((function(e,t){window.localStorage.length>0?(window.localStorage.clear(),e({error:!1,message:"All local storage are cleared"})):t({error:!0,message:" No local storage stored"})}))];case 1:return[2,e.sent()]}}))}))},e.prototype.setSessionStorage=function(e){return o(this,void 0,void 0,(function(){var t=this;return r(this,(function(o){switch(o.label){case 0:return[4,new Promise((function(o,r){e.storageKey&&e.storageType&&e.storageData?("JSON"==e.storageType?(t.convertJsonToString(e.storageData),t.saveDataInSessionStorage(e.storageKey,t.convertJsonToString(e.storageData))):"STRING"==e.storageType&&t.saveDataInSessionStorage(e.storageKey,e.storageData),o({error:!1,message:"data store successfully"})):r({error:!0,message:"Please check parameters"})}))];case 1:return[2,o.sent()]}}))}))},e.prototype.getSessionStorage=function(e){return o(this,void 0,void 0,(function(){var t=this;return r(this,(function(o){switch(o.label){case 0:return[4,new Promise((function(o,r){window.sessionStorage.getItem(e.storageKey)?"JSON"==e.storageType?o(t.convertStringToJson(window.sessionStorage.getItem(e.storageKey))):"STRING"==e.storageType&&o(window.sessionStorage.getItem(e.storageKey)):r({error:!0,message:e.storageKey+" No data found"}),r(new TypeError("Something Wrong"))}))];case 1:return[2,o.sent()]}}))}))},e.prototype.getSessionStorageLength=function(){return o(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,new Promise((function(e,t){window.sessionStorage.length>0?e({error:!1,session:window.sessionStorage.length}):t({error:!0,message:" No session stored"})}))];case 1:return[2,e.sent()]}}))}))},e.prototype.getSessionStorageAllKey=function(){return o(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,new Promise((function(e,t){if(window.sessionStorage.length>0)for(var o=[],r=0;r<window.sessionStorage.length;r++)o.push({keyIndex:r,keyName:window.sessionStorage.key(r)}),e(o);else t({error:!0,message:" No session storage stored"})}))];case 1:return[2,e.sent()]}}))}))},e.prototype.removeSessionStorageItem=function(e){return o(this,void 0,void 0,(function(){return r(this,(function(t){switch(t.label){case 0:return[4,new Promise((function(t,o){e?(window.sessionStorage.removeItem(e),t({error:!1,message:e+" removed successfully"})):o({error:!0,message:"Please pass key to remove"})}))];case 1:return[2,t.sent()]}}))}))},e.prototype.clearAllSessionStorage=function(){return o(this,void 0,void 0,(function(){return r(this,(function(e){switch(e.label){case 0:return[4,new Promise((function(e,t){window.sessionStorage.length>0?(window.sessionStorage.clear(),e({error:!1,message:"All session storage are cleared"})):t({error:!0,message:" No session storage stored"})}))];case 1:return[2,e.sent()]}}))}))},e.prototype.convertJsonToString=function(e){return JSON.stringify(e)},e.prototype.convertStringToJson=function(e){return JSON.parse(e)},e.prototype.saveDataInLocalStorage=function(e,t){window.localStorage.setItem(e,t)},e.prototype.saveDataInSessionStorage=function(e,t){window.sessionStorage.setItem(e,t)},e}();n.ɵprov=t.ɵɵdefineInjectable({factory:function(){return new n},token:n,providedIn:"root"}),n.decorators=[{type:t.Injectable,args:[{providedIn:"root"}]}];var s=function(){function e(){}return e.prototype.ngOnInit=function(){},e}();s.decorators=[{type:t.Component,args:[{selector:"lib-storage",template:"\n <p>\n storage works!\n </p>\n "}]}],s.ctorParameters=function(){return[]};var a=function(){};a.decorators=[{type:t.NgModule,args:[{declarations:[s],imports:[],exports:[s]}]}],e.NgStorage=n,e.NgStorageModule=a,e.StorageComponent=s,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=ng-storage-local.umd.min.js.map