UNPKG

bi-directional-map

Version:

Simple bi directional map implementation using 2 es6 maps

88 lines 2.87 kB
var BiDirectionalMap = /** @class */ (function () { function BiDirectionalMap(map) { var _this = this; this.map = new Map(); this.reverse = new Map(); if (map) { if (map instanceof Map) { map .forEach(function (value, key) { _this.set(key, value); }); } else if (Array.isArray(map)) { map .forEach(function (entry) { _this.set(entry[0], entry[1]); }); } else { Object.keys(map) .forEach(function (key) { _this.set(key, map[key]); }); } } } Object.defineProperty(BiDirectionalMap.prototype, "size", { get: function () { return this.map.size; }, enumerable: true, configurable: true }); BiDirectionalMap.prototype.set = function (key, value) { if (this.map.has(key)) { var existingValue = this.map.get(key); this.reverse.delete(existingValue); } if (this.reverse.has(value)) { var existingKey = this.reverse.get(value); this.map.delete(existingKey); } this.map.set(key, value); this.reverse.set(value, key); return this; }; BiDirectionalMap.prototype.clear = function () { this.map.clear(); this.reverse.clear(); }; BiDirectionalMap.prototype.getValue = function (key) { return this.map.get(key); }; BiDirectionalMap.prototype.getKey = function (value) { return this.reverse.get(value); }; BiDirectionalMap.prototype.deleteKey = function (key) { var value = this.map.get(key); this.reverse.delete(value); return this.map.delete(key); }; BiDirectionalMap.prototype.deleteValue = function (value) { var key = this.reverse.get(value); this.map.delete(key); return this.reverse.delete(value); }; BiDirectionalMap.prototype.hasKey = function (key) { return this.map.has(key); }; BiDirectionalMap.prototype.hasValue = function (value) { return this.reverse.has(value); }; BiDirectionalMap.prototype.keys = function () { return this.map.keys(); }; BiDirectionalMap.prototype.values = function () { return this.reverse.keys(); }; BiDirectionalMap.prototype.entries = function () { return this.map.entries(); }; BiDirectionalMap.prototype.forEach = function (callbackfn, thisArg) { return this.map.forEach(callbackfn); }; return BiDirectionalMap; }()); export { BiDirectionalMap }; //# sourceMappingURL=/Users/haurhan/ts/bi-directional-map/src/index.js.map