UNPKG

simple-storage-tools

Version:

a library for projects that allow you to work with local and session storage in typescript

144 lines 5.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Storage = /** @class */ (function () { function Storage() { } /** * this method will look for a storage with the key name of storage. * @author Seyed Ali Roshan * @param key the string name of your storage. * @param fromLocalStorage default value is `false`. if true, will look * in localStorage instead of SessionStorage. * by default it's false because the main setter method (next method) * store the values in `sessionStorage` by default. * @param toJson default value is `true`. if true, automatically cast * and return JSON object. if put it to true and your storage is not * in JSON shape, it will return what it finds as an string. * @param throwOnNotFound default value is `false`. if true, it will * throw an Exception with `-1` message if it wont found the storage * with given name and enviroment (local or session Storage). * @returns an string or a JSON Object depend on `toJson` parameter. * it will return null or an Exception with `-1` message depend on * `throwOnNotFound` parameter if there is no localStorage founds. */ Storage.get = function (key, fromLocalStorage, toJson, throwOnNotFound) { if (fromLocalStorage === void 0) { fromLocalStorage = false; } if (toJson === void 0) { toJson = true; } if (throwOnNotFound === void 0) { throwOnNotFound = false; } var v; if (fromLocalStorage) { // find storage with the input key v = localStorage.getItem(key); } else { v = sessionStorage.getItem(key); } if (v === null && throwOnNotFound) { throw -1; // throw if not founding anything } // if it can't cast to JSON for any reasons it will put the string as result var output = v; if (toJson && v !== null) { try { output = JSON.parse(v); } catch (ex) { var output_1 = v; } } return output; }; /** * this method will store an input value in a storage with the given * name as its key. * @author Seyed Ali Roshan * @param key the string name that you want your storage created with. * @param str the Object you want it to be stored. * it should be in string or JSON Object. if you don't put JSON Object * (nor string), it will put the string of your Object by `toString()` * method. * @param inLocalStorage default value is `false`. if true, your input * will store in a localStorage instead of SessionStorage. * by default it's false because `localStorage` has store limits and * you should store things you want access to in all the tabs of your * application for example user Token whcih needed by API every time. * @param fromJson default value is `true`. if true, you are telling it, * my input Object is a JSON Object and it will automatically cast your * input Object to string. if put it on true and your input is not a * JSON Object (for example a number), it throws an Exception. * @param ignoreJsonException default value is `true`. if true it will * ignore any error that casting to JSON is caused and store the string * of your Object by using `toString()` method. */ Storage.set = function (key, str, inLocalStorage, fromJson, ignoreJsonException) { if (inLocalStorage === void 0) { inLocalStorage = false; } if (fromJson === void 0) { fromJson = true; } if (ignoreJsonException === void 0) { ignoreJsonException = true; } var input; if (fromJson && typeof (str) !== 'string') { // cast from JSON to string try { input = JSON.stringify(str); } catch (ex) { if (!ignoreJsonException) { // ignore errors if `ignoreJsonException` is true throw ex; } else { input = str.toString(); } } } else { input = str; // put the input itself if its type is string } if (inLocalStorage) { // store the input value localStorage.setItem(key, input); } else { sessionStorage.setItem(key, input); } }; /** * this method will remove a storage. * @author Seyed Ali Roshan * @param key the string name of your storage. * @param fromLocalStorage default value is `false`. if true, will look * in localStorage instead of SessionStorage. */ Storage.remove = function (key, fromLocalStorage) { if (fromLocalStorage === void 0) { fromLocalStorage = false; } if (fromLocalStorage) { localStorage.removeItem(key); } else { sessionStorage.removeItem(key); } }; /** * this method will remove all storages in an storage enviroments * (local or session storage) * @author Seyed Ali Roshan * @param local default value is `false`. if true, it's clear localStorage * enviroment instead of sessionStorage. */ Storage.clear = function (local) { if (local === void 0) { local = false; } if (local) { localStorage.clear(); } else { sessionStorage.clear(); } }; /** * this method will remove all storages from all storage enviroments * (both local and session Storage). * @author Seyed Ali Roshan */ Storage.clearAll = function () { localStorage.clear(); sessionStorage.clear(); }; return Storage; }()); exports.Storage = Storage; //# sourceMappingURL=index.js.map