ReApptor.TypeScript.Linq
It is a complete, fully tested analog of C# Language-Integrated Query (LINQ) written in TypeScript.
Install / Use
/learn @ReApptor/ReApptor.TypeScript.LinqREADME

ReApptor TypeScript LINQ
It is a complete, fully tested analog of C# Language-Integrated Query (LINQ) written in TypeScript.
LINQ package generally operates on the collection types and comes as extension methods serving a variety of purposes in working with collections of types.
It's a simple, lightweight, intuitive set of features with good
documentation and examples.
No complicated or superfluous interfaces, structures, or logic.
No additional or external types or objects.
The original idea behind this package is to make TypeScript syntax look like C# to ease the work for developers using both C# and TypeScript in their day-to-day work.
Installation
Install from the command line:
npm install @reapptor/ts-linq
Install via package.json:
"@reapptor/ts-linq": "^1.*"
Usage
Add import "@reapptor/ts-linq"; into the main project file (i.e. index.ts) to register extensions.
License
The ReApptor TypeScript LINQ package is licensed under the terms of the MIT license and is available for free.
Testing
The code is <b>100%</b> covered by the JEST tests.
The generated coverage result is here:
Coverage Summary
Links
- Overview
- Source code
- Package (GitHub)
- Package (NPM)
- Discussions
- About ReApptor
- ReApptor on GitHub
- ReApptor in LinkedIn
Other projects
- ReApptor.TypeScript.PagedList
<small>@reapptor/ts-paged-list</small>
<small>It is a complete, fully tested pagination library for splitting the array into pages and selecting a specific page by an index.</small>
IList functions
LINQ (IEnumerable) functions
AllAnyAverageChunkConcatCountDistinctDistinctExceptFirstOrDefaultLastLastOrDefaultMaxMinRepeatReverseSelectSingleSingleOrDefaultSelectManySkipSkipLastSkipWhileSumTakeTakeLastTakeWhileToDictionaryToHashSetGroupBy
Async functions
Remove
MSDN: System.Collections.Generic.IList.Remove
Removes the first occurrence of a specific object from the Array<T>.
/**
* @param item - The object(s) to remove from the Array<T>. The value can be null for reference types.
* @returns boolean - true if item is successfully removed; otherwise, false. This method also returns false if item was not found in the Array<T>.
*/
remove(item: T | readonly T[]): void;
Example #1
Removes a single occurrence of an item from the array.
const items: number[] = [1, 2, 3, 4, 5];
items.remove(3);
console.log(items);
Code produces the following output:
[1, 2, 4, 5]
Example #2
Removes all specified items from the array.
const items: number[] = [1, 2, 3, 4, 5];
items.remove([2, 4]);
console.log(items);
Code produces the following output:
[1, 3, 5]
Example #3
Removes nothing if the specified item is not in the array.
const items: number[] = [1, 2, 3, 4, 5];
items.remove(6);
console.log(items);
Code produces the following output:
[1, 2, 3, 4, 5]
RemoveAt
MSDN: System.Collections.Generic.IList.RemoveAt
Removes the element at the specified index of the Array<T>.
/**
* @param index - The zero-based index of the element to remove.
* @exception ArgumentOutOfRangeException - index is less than 0 -or- index is equal to or greater than Count.
*/
removeAt(index: number): void;
Example #1
Removes the item at index 2.
const items: string[] = ["a", "b", "c", "d", "e"];
items.removeAt(2);
console.log(items);
Code produces the following output:
["a", "b", "d", "e"]
Example #2
Removes an item at an index that is less than 0 or greater than or equal to the length of the array.
const items: string[] = ["a", "b", "c", "d", "e"];
try {
items.removeAt(-1)
} catch (error: any) {
console.log(error.message);
}
Code produces the following output:
"Array index \"-1\" out of range, can be in [0..5]."
Example #2
Removing an item at an index that is less than 0 or greater than or equal to the length of the array throws error.
const items: string[] = ["a", "b", "c", "d", "e"];
try {
items.removeAt(-1)
} catch (error: any) {
console.log(error.message);
}
Code produces the following output:
"Array index \"-1\" out of range, can be in [0..5]."
Example #3
Removing an item at index 0 on empty array throws error.
const items: string[] = [];
try {
items.removeAt(0)
} catch (error: any) {
console.log(error.message);
}
Code produces the following output:
"Array index "0" out of range, array is empty."
Insert
MSDN: System.Collections.Generic.IList.Insert
Inserts an element into the Array<T> at the specified index.
/**
* @param item - The object to insert.
* @param index - The zero-based index at which item should be inserted.
*/
insert(item: T | readonly T[], index?: number | null): void;
Example #1
Inserting one array to another array.
const items: number[] = [1];
items.insert([2, 3]);
Code produces the following output:
[2, 3, 1]
Example #2
Inserting an item into array.
const items: number[] = [1];
items.insert(2);
Code produces the following output:
[2, 1]
Example #3
Inserting an item 2 into a 1 index of an array.
const items: number[] = [1, 3];
items.insert(2, 1);
Code produces the following output:
[1, 2, 3]
Example #4
Inserting an array into a 1 index of another array.
const items: number[] = [1, 4];
items.insert([2, 3], 1);
Code produces the following output:
[1, 2, 3, 4]
All
MSDN: System.Linq.Enumerable.All
Determines whether all elements of a sequence satisfy a condition.
/**
* @param predicate - A function to test each element for a condition.
* @returns boolean - true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.
*/
all(predicate: (item: T, index: number) => boolean): boolean;
Example
The following code examples demonstrates how to use All to determine whether all the elements in a sequence satisfy a condition. The result is true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.
const numbers: number[] = [1, 2, 3];
const empty: number[] = [];
const allFive: number[] = [5, 5, 5];
const hasAllTwo: boolean = numbers.all(item => item == 2);
console.log("hasAllTwo = ", hasAllTwo);
const hasAllOnEmpty: boolean = empty.all(item => item == 1);
console.log("hasAllOnEmpty = ", hasAllOnEmpty);
const hasAllFive: boolean = allFive.all(item => item == 5);
console.log("hasAllFive = ", hasAllFive);
Code produces the following output:
hasAllTwo = false;
hasAllOnEmpty = true;
hasAllFive = true;
Any
MSDN: System.Linq.Enumerable.Any
Determines whether a sequence contains any elements in the collection, which satisfies an optional condition. If no condition is provided, the method just returns if the collection is empty or not.
/**
* @param predicate - A function to test each element for a condition.
* @returns boolean - true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.
*/
any(predicate?: (item: T, index: number) => boolean): boolean;
Example
The following code example demonstrates how to use Any to determine whether a sequence contains any elements.
const numbers: number[] = [1, 2, 3];
const empty: number[] = [];
const hasSecond: boolean = numbers.any(item => item == 2);
console.log("hasSecond = ", hasSecond);
c
