UNPKG

data-validator-js

Version:
83 lines (82 loc) 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Dictionary = /** @class */ (function () { function Dictionary() { this.keys = []; this.values = []; } Dictionary.prototype.add = function (key, value) { if (!this.containsKey(key)) { this.keys.push(key); this.values.push(value); return true; } return false; }; Dictionary.prototype.upsert = function (key, value) { if (!this.update(key, value)) { this.add(key, value); } }; /** * Update the dictionary if the entry exists, else add it at the right place such that it is in the sorted order based on the key * @param key Key for the dictionary entry * @param value Value for the dictionary entry */ Dictionary.prototype.sortedUpsert = function (key, value) { if (!this.update(key, value)) { var insertIndex = 0; while (insertIndex < this.keys.length && this.keys[insertIndex] < key) { insertIndex++; } // Insert both key and value in their respective arrays at the identified index this.keys.splice(insertIndex, 0, key); this.values.splice(insertIndex, 0, value); } }; Dictionary.prototype.clear = function () { this.keys = []; this.values = []; }; Dictionary.prototype.containsKey = function (key) { return (this.keys.indexOf(key) !== -1); }; Dictionary.prototype.containsValue = function (value) { return (this.values.indexOf(value) !== -1); }; Dictionary.prototype.count = function () { return this.keys.length; }; Dictionary.prototype.getValue = function (key) { var keyIndex = this.keys.indexOf(key); if (keyIndex !== -1) { return this.values[keyIndex]; } return false; }; Dictionary.prototype.getKeys = function () { return this.keys.slice(); }; Dictionary.prototype.getValues = function () { return this.values.slice(); }; Dictionary.prototype.remove = function (key) { var keyIndex = this.keys.indexOf(key); if (keyIndex !== -1) { this.keys.splice(keyIndex, 1); this.values.splice(keyIndex, 1); return true; } return false; }; Dictionary.prototype.update = function (key, value) { var keyIndex = this.keys.indexOf(key); if (keyIndex !== -1) { this.values[keyIndex] = value; return true; } return false; }; return Dictionary; }()); exports.default = Dictionary;