Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/feast107/linqes
Aggressive extensions of LINQ in ES on prototype of Array
https://github.com/feast107/linqes
es extension fluent generator iterator javascript linq typescript
Last synced: about 3 hours ago
JSON representation
Aggressive extensions of LINQ in ES on prototype of Array
- Host: GitHub
- URL: https://github.com/feast107/linqes
- Owner: feast107
- License: mit
- Created: 2023-04-07T10:18:50.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-01-16T05:16:22.000Z (10 months ago)
- Last Synced: 2024-09-18T05:32:53.172Z (2 months ago)
- Topics: es, extension, fluent, generator, iterator, javascript, linq, typescript
- Language: TypeScript
- Homepage:
- Size: 76.2 KB
- Stars: 37
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
linqes
[LINQ](https://learn.microsoft.com/en-us/dotnet/csharp/linq/) in ES
---
## Installation
```shell
npm i linqes
```
or load through html```html
```
## Example
```ts
import 'linqes'let enumerable : IEnumerable = new List();
enumerable = enumerable
.where(x => x > 3)
.append(1)
.concat([1, 2, 3, 4])
.prepend(3)
.select(x => x * 2)
.reverse()
.chunk(3)
.selectMany(x => x)const list : List = enumerable.toArray()
for (const item of enumerable) {
console.log(item) // => 8,6,4,2,2,6
}let chars : IEnumerable = 'This is a string';
chars.select(x => x + ' ').toArray()```
## Iterable types
```ts
String
Array
Int8Array
Uint8Array
Uint8ClampedArray
Int16Array
Uint16Array
Int32Array
Uint32Array
Float32Array
Float64Array
BigInt64Array
BigUint64Array
Map
```## IEnumerable
```ts
interface IEnumerable {/**
* Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
* @param seed The initial accumulator value.
* @param accumulator An accumulator function to be invoked on each element.
*/
aggregate(
seed : TSeed,
accumulator : (seed : TSeed, item : T) => TReturn) : TReturn;/**
* Determines whether all elements of a sequence satisfy a condition.
* @param predicate
*/
all(predicate : (item : T) => boolean) : boolean;/**
* Determines whether a sequence contains any elements.
*/
any() : boolean;/**
* Determines whether any element of a sequence satisfies a condition.
* @param predicate A function to test each element for a condition.
*/
any(predicate : (item : T) => boolean) : boolean;/**
* Appends a value to the end of the sequence.
* @param element The value to append to
*/
append(element : T) : IEnumerable;/**
* Returns the input typed as enumerable.
*/
asEnumerable() : IEnumerable;/**
* Splits the elements of a sequence into chunks of size at most
* @param size The maximum size of each chunk.
*/
chunk(size : number) : IEnumerable>;/**
* Concatenates two sequences.
* @param source The sequence to concatenate to the current.
*/
concat(source : IEnumerable) : IEnumerable;/**
* Determines whether a sequence contains a specified element by using the default equality comparer.
* @param value The value to locate in the sequence.
*/
contains(value : T) : boolean;/**
* Determines whether a sequence contains a specified element by using a specified comparer.
* @param value The value to locate in the sequence.
* @param comparer An equality comparer to compare values.
*/
contains(
value : T,
comparer? : (left : T, right : T) => boolean) : boolean;/**
* Returns the number of elements in a sequence.
*/
count() : number;/**
* Returns a number that represents how many elements in the specified sequence satisfy a condition.
* @param predicate A function to test each element for a condition.
*/
count(predicate : (item : T) => boolean) : number;/**
* Returns distinct elements from a sequence by using the default equality comparer to compare values.
*/
distinct() : IEnumerable;/**
* Returns distinct elements from a sequence by using a specified comparer to compare values.
* @param comparer An comparer to compare values.
*/
distinct(comparer : (left : T, right : T) => boolean) : IEnumerable;/**
* Returns distinct elements from a sequence according to a specified key selector function.
* @param keySelector A function to extract the key for each element.
*/
distinctBy(keySelector : (item : T) => TKey) : IEnumerable;/**
* Returns distinct elements from a sequence according to a specified key selector function and using a specified
* comparer to compare keys.
* @param keySelector A function to extract the key for each element.
* @param comparer An comparer to compare keys.
*/
distinctBy(
keySelector : (item : T) => TKey,
comparer? : (left : TKey, right : TKey) => boolean) : IEnumerable;/**
* Returns the element at a specified index in a sequence.
* @param index The zero-based index of the element to retrieve.
*/
elementAt(index : number) : T;/**
* Returns the element at a specified index in a sequence.
* @param index The index of the element to retrieve, which is either from the beginning or the end of the sequence.
*/
elementAtOrDefault(index : number) : T | null;/**
* Produces the set difference of two sequences by using the default equality comparer to compare values.
* @param source An array whose elements that also occur in the first sequence will cause those elements to be
* removed from the returned sequence.
*/
except(source : Array | IEnumerable) : IEnumerable;/**
* Produces the set difference of two sequences by using the specified comparer to compare values.
* @param source An array whose elements that also occur in the first sequence will cause those elements to be
* removed from the returned sequence.
* @param comparer An comparer to compare values.
*/
except(
source : Array | IEnumerable,
comparer? : (left : T, right : T) => boolean) : IEnumerable;/**
* Produces the set difference of two sequences according to a specified key selector function.
* @param source An source whose keys that also occur in the first sequence will cause those elements to be removed
* from the returned sequence.
* @param keySelector A function to extract the key for each element.
*/
exceptBy(
source : Array | IEnumerable,
keySelector : (item : T) => TKey) : IEnumerable;/**
* Produces the set difference of two sequences according to a specified key selector function.
* @param source An source whose keys that also occur in the first sequence will cause those elements to be removed
* from the returned sequence.
* @param keySelector A function to extract the key for each element.
* @param comparer The comparer to compare values.
*/
exceptBy(
source : Array | IEnumerable,
keySelector : (item : T) => TKey,
comparer? : (left : TKey, right : TKey) => boolean) : IEnumerable;/**
* Returns the first element of a sequence.
*/
first() : T;/**
* Returns the first element in a sequence that satisfies a specified condition.
* @param predicate A function to test each element for a condition.
*/
first(predicate : (item : T) => boolean) : T;/**
* Returns the first element of a sequence, or a default value if the sequence contains no elements.
*/
firstOrDefault() : T | null;/**
* Returns the first element of a sequence, or a specified default value if the sequence contains no elements.
* @param defaultValue The default value to return if the sequence is empty.
*/
firstOrDefault(defaultValue : T) : T | null;/**
* Returns the first element of a sequence, or a default value if the sequence contains no elements.
* @param predicate A function to test each element for a condition.
*/
firstOrDefault(predicate : (item : T) => boolean) : T | null;/**
* Returns the first element of the sequence that satisfies a condition, or a specified default value if no such element is found.
* @param predicate A function to test each element for a condition.
* @param defaultValue The default value to return if the sequence is empty.
*/
firstOrDefault(
predicate : (item : T) => boolean,
defaultValue : T) : T | null;/**
* Groups the elements of a sequence according to a specified key selector function.
* @param keySelector A function to extract the key for each element.
*/
groupBy(keySelector : (item : T) => TKey) : IEnumerable & { key : TKey }>;/**
* Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer.
* @param keySelector A function to extract the key for each element.
* @param comparer An comparer to compare keys.
*/
groupBy(
keySelector : (item : T) => TKey,
comparer : (left : TKey, right : TKey) => boolean) : IEnumerable & { key : TKey }>;/**
* Groups the elements of a sequence according to a specified key selector function and projects the elements for
* each group by using a specified function.
* @param keySelector A function to extract the key for each element.
* @param elementSelector A function to map each source element to an element in the source.
*/
groupBy(
keySelector : (item : T) => TKey,
elementSelector : (item : T) => TElement) : IEnumerable & { key : TKey }>;/**
* Groups the elements of a sequence according to a key selector function. The keys are compared by using a comparer
* and each group's elements are projected by using a specified function.
* @param keySelector A function to extract the key for each element.
* @param elementSelector A function to map each source element to an element in the source.
* @param comparer An comparer to compare keys.
*/
groupBy(
keySelector : (item : T) => TKey,
elementSelector : (item : T) => TElement,
comparer : (left : TKey, right : TKey) => boolean) : IEnumerable & { key : TKey }>;/**
* Returns the last element of a sequence.
*/
last() : T;/**
* Returns the last element of a sequence that satisfies a specified condition.
* @param predicate A function to test each element for a condition.
*/
last(predicate : (item : T) => boolean) : T;/**
* Returns the last element of a sequence, or a default value if the sequence contains no elements.
*/
lastOrDefault() : T | null;/**
* Returns the last element of a sequence, or a specified default value if the sequence contains no elements.
* @param defaultValue The default value to return if the sequence is empty.
*/
lastOrDefault(defaultValue : T) : T | null;/**
* Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
* @param predicate A function to test each element for a condition.
*/
lastOrDefault(predicate : (item : T) => boolean) : T | null;/**
* Returns the last element of a sequence that satisfies a condition, or a specified default value if no such element is found.
* @param predicate A function to test each element for a condition.
* @param defaultValue The default value to return if the sequence is empty.
*/
lastOrDefault(
predicate : (item : T) => boolean,
defaultValue : T) : T | null;/**
* Sorts the elements of a sequence in ascending order.
*/
order() : IEnumerable;/**
* Sorts the elements of a sequence in ascending order.
* @param comparer An comparer to compare keys.
*/
order(comparer : (current : T, exist : T) => number) : IEnumerable;/**
* Sorts the elements of a sequence in ascending order according to a key.
* @param keySelector A function to extract a key from an element.
*/
orderBy(keySelector : (item : T) => TKey) : IEnumerable;/**
* Sorts the elements of a sequence in ascending order according to a key.
* @param keySelector A function to extract a key from an element.
* @param comparer An comparer to compare keys.
*/
orderBy(
keySelector : (item : T) => TKey,
comparer : (current : TKey, exist : TKey) => number) : IEnumerable;/**
* Sorts the elements of a sequence in descending order.
*/
orderDescending() : IEnumerable;/**
* Sorts the elements of a sequence in descending order.
* @param comparer An comparer to compare keys.
*/
orderDescending(comparer : (current : T, exist : T) => number) : IEnumerable;/**
* Sorts the elements of a sequence in descending order according to a key.
* @param keySelector A function to extract a key from an element.
*/
orderByDescending(keySelector : (item : T) => TKey) : IEnumerable;/**
* Sorts the elements of a sequence in descending order according to a key.
* @param keySelector A function to extract a key from an element.
* @param comparer An comparer to compare keys.
*/
orderByDescending(
keySelector : (item : T) => TKey,
comparer : (current : TKey, exist : TKey) => number) : IEnumerable;/**
* Adds a value to the beginning of the sequence.
* @param element The value to prepend to
*/
prepend(element : T) : IEnumerable;/**
* Inverts the order of the elements in a sequence.
*/
reverse() : IEnumerable;/**
* Projects each element of a sequence into a new form by incorporating the element's index.
* @param selector A transform function to apply to each element.
*/
select(selector : ((item : T) => TReturn) | ((item : T, index : number) => TReturn))
: IEnumerable;/**
* Projects each element of a sequence to an array and flattens the resulting sequences into one sequence.
* @param selector A transform function to apply to each element.
*/
selectMany(selector : (item : T) => IEnumerable) : IEnumerable;/**
* Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than
* one such element exists.
*/
single() : T;/**
* Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than
* one such element exists.
* @param predicate A function to test an element for a condition.
*/
single(predicate : (item : T) => boolean) : T;/**
* Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception
* if there is more than one element in the sequence.
*/
singleOrDefault() : T | null;/**
* Returns the only element of a sequence, or a specified default value if the sequence is empty; this method throws
* an exception if there is more than one element in the sequence.
* @param defaultValue The default value to return if the sequence is empty.
*/
singleOrDefault(defaultValue : T) : T | null;/**
* Returns the only element of a sequence, or a specified default value if the sequence is empty; this method throws
* an exception if there is more than one element in the sequence.
* @param predicate A function to test an element for a condition.
*/
singleOrDefault(predicate : (item : T) => boolean) : T | null;/**
* Returns the only element of a sequence that satisfies a specified condition, or a specified default value if no
* such element exists; this method throws an exception if more than one element satisfies the condition.
* @param predicate A function to test an element for a condition.
* @param defaultValue The default value to return if the sequence is empty.
*/
singleOrDefault(
predicate : (item : T) => boolean,
defaultValue : T) : T | null;/**
* Bypasses a specified number of elements in a sequence and then returns the remaining elements.
* @param count The number of elements to skip before returning the remaining elements.
*/
skip(count : number) : IEnumerable;/**
* Returns a new enumerable collection that contains the elements from source with the last count elements of the
* source collection omitted.
* @param count The number of elements to omit from the end of the collection.
*/
skipLast(count : number) : IEnumerable;/**
* Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.
* @param predicate A function to test each element for a condition.
*/
skipWhile(predicate : (item : T) => boolean) : IEnumerable;/**
* Returns a specified number of contiguous elements from the start of a sequence.
* @param count The number of elements to return
*/
take(count : number) : IEnumerable;/**
* Returns a specified number of contiguous elements from the start of a sequence.
* @param range The range of elements to return, which has start and end indexes either from the beginning or the
* end of the sequence.
*/
take(range : [start : number, end : number]) : IEnumerable;/**
* Returns a new enumerable collection that contains the last count elements from source.
* @param count The number of elements to take from the end of the collection.
*/
takeLast(count : number) : IEnumerable;/**
* Returns elements from a sequence as long as a specified condition is true.
* @param predicate A function to test each element for a condition.
*/
takeWhile(predicate : ((item : T) => boolean) | ((item : T, index : number) => boolean)) : IEnumerable;/**
* Creates an array from an enumerable.
*/
toArray() : Array;/**
* Creates a Map from an enumerable according to specified key selector and element selector functions.
* @param keySelector A function to extract a key from each element.
* @param valueSelector A transform function to produce a result element value from each element.
*/
toDictionary(keySelector : (item : T) => K, valueSelector : (item : T) => V) : Map;/**
* Produces the set union of two sequences by using a specified comparer.
* @param source An enumerable whose distinct elements form the second set for the union.
* @param comparer The comparer to compare values.
*/
union(source : Array, comparer? : (left : T, right : T) => boolean) : IEnumerable;/**
* Produces the set union of two sequences according to a specified key selector function.
* @param source An enumerable whose distinct elements form the second set for the union.
* @param keySelector A function to extract the key for each element.
* @param comparer The comparer to compare values.
*/
unionBy(
source : Array, keySelector : (item : T) => TKey,
comparer? : (left : TKey, right : TKey) => boolean) : IEnumerable;/**
* Filters a sequence of values based on a predicate. Each element's index is used in the logic of the predicate function.
* @param predicate A function to test each source element for a condition; the second parameter of the function
* represents the index of the source element.
*/
where(predicate : ((item : T) => boolean) | ((item : T, index : number) => boolean)) : IEnumerable;
}```