UNPKG

decova-dotnet-developer

Version:

This package provides fundumentals that a .net developer may miss while working with Typescript, whether they are missing functinalities or funcionalities provided in a non-elegant design in javascript. Bad naming, bad design of optional parameters, non-c

87 lines 2.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Dictionary = exports.KeyValuePair = void 0; const Exceptions_1 = require("../Exceptions/Exceptions"); const List_1 = require("./List"); class KeyValuePair { constructor(key, value) { this.Key = key; this.Value = value; } } exports.KeyValuePair = KeyValuePair; class Dictionary { constructor() { this._map = new Map(); } Add(key, value) { if (this._map.has(key)) throw `The Dictionary aleardy has the specified key: ${key}`; this._map.set(key, value); } Ensure(key, value, overwriteIfExising = false) { if (this._map.has(key)) { if (overwriteIfExising) { this._map.set(key, value); return true; } else { return false; } } else { this._map.set(key, value); return true; } } Get(key) { return this._map.get(key); } Set(key, value) { this._map.set(key, value); } UpdateOnlyIfAny(key, value) { if (this._map.has(key)) { this._map.set(key, value); return true; } else { return false; } } Remove(key) { if (this._map.has(key) == false) throw new Exceptions_1.Exception(`Dictionary doesn't have key [${key}]`); this.EnsureRemoved(key); } EnsureRemoved(key) { this._map.delete(key); } get Keys() { return new List_1.List(this._map.keys()); } get Values() { return new List_1.List(this._map.values()); } Clear() { this._map.clear(); } Foreach(func) { for (let key of this._map.keys()) { func(new KeyValuePair(key, this._map.get(key))); } } Contains(key) { return this.Keys.Contains(key); } get Count() { return this._map.size; } static FromObjectProps(obj) { const output = new Dictionary(); output._map = new Map(Object.entries(obj)); return output; } } exports.Dictionary = Dictionary; //# sourceMappingURL=Dictionary.js.map