JavaScript Integrated Query for generators and arrays. Inspired by LINQ
For vanillajs and nodejs
๐ Wiki
For nowโข use:
$ npm i github:A1rPun/jinq.js
And then import it like:
import { jinq } from 'jinq';
Use jinq.from
to construct an Enumerable from an iterator or an array.
function* generator() {
yield 1;
yield 2;
yield 3;
}
const numberText = jinq
.from(generator())
.skip(2)
.take(1)
.select((n) => `The number: ${n}`)
.single();
numberText === 'The number: 3'; // true
Use jinq.range
to construct a generated sequence of numbers.
const bigRange = jinq.range(0, Number.MAX_SAFE_INTEGER);
bigRange.any(); // true
bigRange.take(2).toList(); // [0, 1]
EqualityComparer
is not implemented
JavaScript has numerous built in methods to do operations on arrays, not so much for generators. This library focuses primarely on generators because of the lazy loading with yields.
Method | Returns value | Description | JS alternative |
---|---|---|---|
aggregate() | โ | Array.reduce() | |
all() | โ | Array.every() | |
any() | โ | Array.some() | |
append() | Array.push() | ||
asEnumerable() | yield* Iterator | ||
average() | โ | Array.reduce() | |
cast() | accepts a type as parameter e.g. String , Number or Boolean |
Array.map() | |
chunk() | - | ||
concat() | Array.concat() | ||
contains() | โ | Array.includes() | |
count() | โ | Array.length | |
defaultIfEmpty() | - | ||
distinct() | - | ||
distinctBy() | - | ||
elementAt() | โ | can throw an error | Array.at() ?? throw new Error() |
elementAtOrDefault() | โ | Array.at() ?? defaultValue | |
except() | Array.filter() | ||
exceptBy() | Array.filter() | ||
first() | โ | can throw an error | Array.at(0) ?? throw new Error() |
firstOrDefault() | โ | Array.at(0) ?? defaultValue | |
groupBy() | - | ||
groupJoin() | - | ||
intersect() | - | ||
intersectBy() | - | ||
join() | - | ||
last() | โ | can throw an error | Array.at(-1) ?? throw new Error() |
lastOrDefault() | โ | Array.at(-1) ?? defaultValue | |
longCount() | โ | Array.length | |
max() | โ | Array.reduce() | |
maxBy() | โ | Array.reduce() | |
min() | โ | Array.reduce() | |
minBy() | โ | Array.reduce() | |
ofType() | Array.filter() | ||
order() | doesn't return an IOrderedEnumerable | Array.sort() | |
orderBy() | doesn't return an IOrderedEnumerable | Array.sort() | |
orderByDescending() | doesn't return an IOrderedEnumerable | Array.sort() | |
prepend() | Array.unshift() | ||
reverse() | Array.reverse() | ||
select() | Array.map() | ||
selectMany() | Array.flatMap() | ||
sequenceEqual() | โ | Array.all() | |
single() | โ | can throw an error | - |
singleOrDefault() | Array.at(index) ?? defaultValue | ||
skip() | Array.slice() | ||
skipLast() | Array.slice() | ||
skipWhile() | Array.slice() | ||
sum() | โ | Array.reduce() | |
take() | Array.slice() | ||
takeLast() | Array.slice() | ||
takeWhile() | Array.slice() | ||
toArray() | โ | [...Iterator] | |
toDictionary() | โ | new Map(Array) | |
toHashSet() | โ | new Set(Array) | |
toList() | โ | same as toArray() | [...Iterator] |
toLookup() | โ | new Map(Array) | |
tryGetNonEnumeratedCount() | โ | returns the count if enumerated, otherwise undefined | Array.length |
union() | - | ||
unionBy() | - | ||
where() | Array.filter() | ||
zip() | - |
- jinq.empty()
- jinq.from()
- jinq.range()
- jinq.repeat()