HomeAccounting
103 строки · 1.9 Кб
1function Storage(debug) {2
3if(debug === true)4{5this.debugMode = true;6}7
8this.storage = window.localStorage;9};10
11Storage.prototype.addObjectToStorage = function (key, object) {12this.storage.setItem(key, JSON.stringify(object));13};14
15Storage.prototype.addStringToStorage = function (key, value) {16this.storage.setItem(key, value);17};18
19Storage.prototype.get = function (key) {20return this.storage.getItem(key);21};22
23Storage.prototype.getObject = function (key) {24try25{26return JSON.parse(this.storage.getItem(key));27}28catch (e)29{30this._debug(e);31this._debug(key + ' = ' + this.storage.getItem(key));32return false;33}34};35
36Storage.prototype.add = function (key, value) {37try38{39if(typeof value === 'object') {40this.addObjectToStorage(key, value);41}42else if (typeof value === 'string' || typeof value === 'number') {43this.addStringToStorage(key, value);44}45else {46this._debug('2 parameter does not belong to a known type')47}48
49return this.storage;50
51}52catch (e)53{54if (e === QUOTA_EXCEEDED_ERR) {55this._debug('LocalStorage is exceeded the free space limit')56}57else58{59this._debug(e)60}61}62};63
64Storage.prototype.clear = function () {65try66{67this.storage.clear();68return true;69}70catch (e)71{72this._debug(e)73return false;74}75};76
77Storage.prototype.delete = function(key) {78try79{80this.storage.removeItem(key);81return true;82}83catch (e)84{85this._debug(e)86return false;87}88};89
90Storage.prototype.view = function () {91return this.storage;92};93
94
95Storage.prototype._debug = function(error) {96if(this.debugMode)97{98console.error(error);99}100return null;101};102
103module.exports = Storage;