JavaScript Integrated Query for generators and arrays. Inspired by LINQ
For JavaScript and NodeJs
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]
IEqualityComparer
is not implemented for most functions that use itIOrderedEnumerable
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() | |
aggregateBy() | ✅ | 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 | |
countBy() | ✅ | Array.reduce() | |
defaultIfEmpty() | Array.length ? Array : [defaultValue] | ||
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 | Array.at(index) ?? throw new 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()