HomeAccounting

Форк
0
103 строки · 1.9 Кб
1
function Storage(debug) {
2

3
    if(debug === true)
4
    {
5
        this.debugMode = true;
6
    }
7

8
    this.storage = window.localStorage;
9
};
10

11
Storage.prototype.addObjectToStorage = function (key, object) {
12
    this.storage.setItem(key, JSON.stringify(object));
13
};
14

15
Storage.prototype.addStringToStorage = function (key, value) {
16
    this.storage.setItem(key, value);
17
};
18

19
Storage.prototype.get = function (key) {
20
    return this.storage.getItem(key);
21
};
22

23
Storage.prototype.getObject = function (key) {
24
    try
25
    {
26
        return JSON.parse(this.storage.getItem(key));
27
    }
28
    catch (e)
29
    {
30
        this._debug(e);
31
        this._debug(key + ' = ' + this.storage.getItem(key));
32
        return false;
33
    }
34
};
35

36
Storage.prototype.add = function (key, value) {
37
    try
38
    {
39
        if(typeof value === 'object') {
40
            this.addObjectToStorage(key, value);
41
        }
42
        else if (typeof value === 'string' || typeof value === 'number') {
43
            this.addStringToStorage(key, value);
44
        }
45
        else {
46
            this._debug('2 parameter does not belong to a known type')
47
        }
48

49
        return this.storage;
50

51
    }
52
    catch (e)
53
    {
54
        if (e === QUOTA_EXCEEDED_ERR) {
55
            this._debug('LocalStorage is exceeded the free space limit')
56
        }
57
        else
58
        {
59
            this._debug(e)
60
        }
61
    }
62
};
63

64
Storage.prototype.clear = function () {
65
    try
66
    {
67
        this.storage.clear();
68
        return true;
69
    }
70
    catch (e)
71
    {
72
        this._debug(e)
73
        return false;
74
    }
75
};
76

77
Storage.prototype.delete = function(key) {
78
    try
79
    {
80
        this.storage.removeItem(key);
81
        return true;
82
    }
83
    catch (e)
84
    {
85
        this._debug(e)
86
        return false;
87
    }
88
};
89

90
Storage.prototype.view = function () {
91
    return this.storage;
92
};
93

94

95
Storage.prototype._debug = function(error) {
96
    if(this.debugMode)
97
    {
98
        console.error(error);
99
    }
100
    return null;
101
};
102

103
module.exports = Storage;

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.