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
243 lines • 7.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.List = void 0;
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 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(toSimpleValue = null) {
const s = toSimpleValue == null ? ((a) => a.toString()) : toSimpleValue;
const copy = this.Clone();
copy.Array.sort((a, b) => (s(a) > s(b) ? 1 : (s(a) < s(b) ? -1 : 0)));
return copy;
}
Reverse() {
const copy = this.Clone();
copy.Array.reverse();
return copy;
}
Foreach(func) {
this.Array.map(item => func(item));
}
Any(func = null) {
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) {
if (this.Contains(item) === false) {
this.Add(item);
}
}
AddRange(items) {
if (items == undefined)
throw `@[List.AddRange(items)] NullArgumentException`;
if (items.constructor === Array) {
this._Array = this.Array.concat(items);
}
else {
this._Array = this.Array.concat(items.Array);
}
}
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) {
this.Array.splice(index, 1);
}
RemoveRange(index, lenght) {
if (index + lenght > this.Array.length)
throw new Error("Range to remove is out of the List range!");
this.Array.splice(index, lenght);
}
RemoveWhere(func) {
for (let x = 0; x < this.Array.length; x++) {
if (func(this.Array[x])) {
this.RemoveAt(x);
x--;
}
}
}
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;
}
FirstOrDefault(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 = null) {
let result = this.FirstOrDefault(func);
if (result == null) {
throw "No elements found";
}
else {
return result;
}
}
LastOrDefault(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.LastOrDefault(func);
if (result == null) {
throw "No elements found";
}
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) {
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) {
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.FirstOrDefault(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;
}
}
exports.List = List;
//# sourceMappingURL=List.js.map