UNPKG

decova-dotnet

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

291 lines 8.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.List = void 0; require("../index"); class List { constructor(array = null) { if (array == null) { this._Array = new Array(); } else if (Object.getOwnPropertyDescriptor(array, "iterator") != null) { this._Array = [...array]; } else { this._Array = array; } } get Items() { return this._Array; } get Array() { return this._Array; } get Count() { return this.Array.length; } Select(func) { var newArr = this.Array.map(item => func(item)); return new List(newArr); } SelectMany(func) { let result = new List([]); for (let x = 0; x < this.Array.length; x++) { let itemResult = func((this.Array[x])); if (itemResult != null) { result.AddRange(itemResult); } } return result; } Sort(compareFun) { const copy = this.Clone(); copy.Array.sort(compareFun); return copy; } Reverse() { const copy = this.Clone(); copy.Array.reverse(); return copy; } Foreach(func) { this.Array.map(item => func(item)); } Any(func) { if (func == null) return this.Array.length > 0; for (let x = 0; x < this.Array.length; x++) { if (func(this.Array[x]) == true) { return true; } } return false; } All(func) { for (let item of this._Array) { if (func(item) == false) return false; } return true; } get IsEmpty() { return this._Array.length == 0; } Add(item) { this.Array.push(item); } EnsureItem(item, comparer) { comparer = comparer !== null && comparer !== void 0 ? comparer : ((obj1, obj2) => obj1 == obj2); let existing = this.FirstOrNull(exst => comparer(exst, item)); if (!existing) { this.Add(item); return item; } return existing; } AddRange(items) { if (items == undefined) throw `@[List.AddRange(items)] NullArgumentException`; if (items.constructor === Array) { this._Array.push(...items); } else { this._Array.push(...items.Array); } } Union(another) { const newOne = another.constructor == List ? another : new List(another); const output = this.Items.concat(newOne.Items); return new List(output).Distinct(); } Where(func) { let result = this.Array.filter(e => func(e)); return new List(result); } ItemAt(index) { return this.Array[index]; } IndexOf(item) { for (let i = 0; i < this.Array.length; i++) { if (this.Array[i] === item) return i; } return -1; } RemoveAt(index) { const removedItem = this.Array[index]; this.Array.splice(index, 1); return { removedItem }; } RemoveRange(index, length) { if (index + length > this.Array.length) throw new Error("Range to remove is out of the List range!"); const removed = this.Skip(index).Take(length); this.Array.splice(index, length); return { removed: removed._Array }; } RemoveWhere(func) { const removed = []; for (let x = 0; x < this.Array.length; x++) { if (func(this.Array[x])) { removed.push(this.Array[x]); this.RemoveAt(x); x--; } } return { removedItems: removed }; } GetRange(index, length = undefined) { if (length === undefined) { length = this.Array.length - index; } if (index + length > this.Array.length) throw new Error("range is out of List's range"); return new List(this.Array.slice(index, index + length)); } Insert(index, item) { this.Array.splice(index, 0, item); } InsertRange(index, items) { let inputArr; if (items.constructor == List) { inputArr = items.Array; } else { inputArr = items; } let before = this.GetRange(0, index); let after = this.GetRange(index); let output = before.Clone(); output.AddRange(new List(inputArr)); output.AddRange(after); this._Array = output.Array; } FirstOrNull(func) { if (func == null) { if (this.Array.length == 0) { return null; } else { return this.Array[0]; } } for (let x = 0; x < this.Array.length; x++) { if (func(this.Array[x]) === true) { return this.Array[x]; } } return null; } First(func) { let result = this.FirstOrNull(func); if (result == null) { throw "Array.xFirst() didn't find any matching element. If you're not sure of returning an elememt, use FirstOrNull() instead."; } else { return result; } } LastOrNull(func = null) { if (this.Array.length == 0) return null; if (func == null) return this.Array[this.Array.length - 1]; for (let x = this.Array.length - 1; x >= 0; x--) { if (func(this.Array[x]) == true) { return this.Array[x]; } } return null; } Last(func = null) { let result = this.LastOrNull(func); if (result == null) { throw "Array.xLast() didn't find any matching element. If you're not sure of returning an elememt, use FirstOrNull() instead."; } else { return result; } } Skip(countToSkip) { if (countToSkip > this.Array.length) throw new Error("countToSkip is out of range!"); return new List(this.Array.slice(countToSkip)); } Take(countToTake) { if (countToTake > this.Array.length) throw new Error("countToTake is out of range!"); return new List(this.Array.slice(0, countToTake)); } Contains(obj) { return this.Any(o => o === obj); } Distinct() { function onlyUnique(value, index, self) { return self.indexOf(value) === index; } return new List(this.Array.filter(onlyUnique)); } Except(excepted) { if (excepted.constructor == Array) excepted = new List(excepted); let result = new List(new Array()); for (let x = 0; x < this.Array.length; x++) { if (!excepted.Contains(this.Array[x])) { result.Add(this.Array[x]); } } return result; } Clone() { let result = new List(new Array()); result.AddRange(this.Array); return result; } Intersect(otherList) { if (otherList.constructor == Array) otherList = new List(otherList); let result = new List(new Array()); let otherListCopy = otherList.Clone(); let thisListCopy = this.Clone(); while (thisListCopy.Any() && otherListCopy.Any()) { let item = thisListCopy.Array[0]; let common = otherListCopy.FirstOrNull(i => i === item); if (common == null) { thisListCopy.RemoveWhere((i) => i == item); } else { result.Add(item); thisListCopy.RemoveWhere((i) => i === item); otherListCopy.RemoveWhere((i) => i === item); } } return result; } Reset() { let original; this.Array.length = 0; } ToMap(keySelector, distinctItems) { const map = new Map(); for (let item of this.Array) { const key = keySelector(item); const arr = map.xEnsure(key, [], false); if (distinctItems) { new List(arr).EnsureItem(item); } else { new List(arr).Add(item); } } return map; } _EnsureObservers() { if (!this.Array[FN_NAME_onChangedHandlers]) { this.Array[FN_NAME_onChangedHandlers] = []; } return this.Array[FN_NAME_onChangedHandlers]; } } exports.List = List; const FN_NAME_onChangedHandlers = "_onChangedHandlers"; //# sourceMappingURL=List.js.map