UNPKG

@itexpert-dev/key-value-storage

Version:

key-value storage like C# dictionary

51 lines 1.91 kB
"use strict"; var tiny_helpers_1 = require("@itexpert-dev/tiny-helpers"); var KeyValueStorage = (function () { function KeyValueStorage() { this._storage = {}; } KeyValueStorage.prototype.forceAppend = function (params) { this._storage[params.key] = params.value; }; KeyValueStorage.prototype.add = function () { var params = []; for (var _i = 0; _i < arguments.length; _i++) { params[_i] = arguments[_i]; } for (var _a = 0, params_1 = params; _a < params_1.length; _a++) { var param = params_1[_a]; if (tiny_helpers_1.isNull(param.key)) { throw new Error("can not put data with null key"); } if (tiny_helpers_1.isUndefined(param.key)) { throw new Error("can not put data with undefined key"); } if (this._storage.hasOwnProperty(param.key)) { if (param.force === true) { this.forceAppend(param); } else { throw new Error("data with key " + param.key + " already exist use force=true flag for override"); } } else { this.forceAppend(param); } } }; KeyValueStorage.prototype.remove = function (key) { delete this._storage[key]; }; KeyValueStorage.prototype.get = function (key) { if (!this._storage.hasOwnProperty(key)) { throw new Error("data with key: " + key + " is missing!"); } return this._storage[key]; }; KeyValueStorage.prototype.check = function (key) { return this._storage.hasOwnProperty(key); }; return KeyValueStorage; }()); exports.KeyValueStorage = KeyValueStorage; //# sourceMappingURL=KeyValueStorage.js.map