all files / built/ index.js

87.93% Statements 51/58
73.68% Branches 28/38
85.71% Functions 6/7
86% Lines 43/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145                                                                                                                                                                                                           
"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; }
        Eif (toJson === void 0) { toJson = true; }
        Eif (throwOnNotFound === void 0) { throwOnNotFound = false; }
        var v;
        if (fromLocalStorage) {
            v = localStorage.getItem(key);
        }
        else {
            v = sessionStorage.getItem(key);
        }
        Iif (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;
        Eif (toJson && v !== null) {
            try {
                output = JSON.parse(v);
            }
            catch (ex) {
                console.log(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; }
        Eif (fromJson === void 0) { fromJson = true; }
        Eif (ignoreJsonException === void 0) { ignoreJsonException = true; }
        var input;
        Eif (fromJson && typeof (str) !== 'string') {
            try {
                input = JSON.stringify(str);
            }
            catch (ex) {
                if (!ignoreJsonException) {
                    throw ex;
                }
                else {
                    input = str.toString();
                }
            }
        }
        else {
            input = str; // put the input itself if its type is string
        }
        if (inLocalStorage) {
            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