0.9.2 • Published 5 years ago
modern-linq v0.9.2
Modern-Linq
Modern-Linq is a library that brings the C# linq functionality into JavaScript. It is based on the native Iterable funtionality in JavaScript.
Examples:
const query = fromIterable([1, 2, 3, 4, 5, 6, 7])
.where(_ => _ % 2 === 0)
.select(_ => _ * 2);
const query = range(0, 30)
.select(i => i * 3)
.where(i => i > 10)
.select(i =>
({
odd: i % 2 === 1,
even: i % 2 === 0,
num: i
})
)
.skip(1)
.take(4)
.groupBy(i => i.odd)
.select(_ => ({
key: _.key,
items: _.orderByDescending(_ => _.num).toArray()
}))
.orderBy(_ => _.key);
To consume the data we can use:
const arr = query.toArray();
const arr = Array.from(query);
for (const item of query) {
console.log(item); // Prints 4, 8, 12
}
Remarks:
- The data is processed in the moment when is requested.
- The sequence is immutable the output !== input
Some of the methods are using native Array implementation if the provided source is an Array Methods with native fallback:
select
: uses Array.prototype.mapwhere
: uses Array.prototype.filtertake
: uses Array.prototype.sliceskip
: uses Array.prototype.slicedistinct
: if no comparer is provided it uses native Set classcount
: returns Array.prototype.lengthorderBy
: Array.prototype.sortconcat
: uses spread operator
Methods implemented:
aggregate
any
all
concat
count
distinct
elementAt
first
firstOrDefault
groupJoin
join
intersect
last
lastOrDefault
max
min
ofType
orderBy
range
repeat
reverse
select
selectMany
isEqual
( sequenceEqual )single
skip
skipLast
skipWhile
sum
take
takeLast
takeWhile
union
where
toArray
( toList )toMap
( toDictionary )toSet
Waiting for implementation:
contains
except
zip
Extra methods
isElementsEqual
: checks if two sequences have same elements, no matter of the position.product
: get the product of sequence.join
(with string argument): join all elements of sequence and concat with separatorallAndEvery
: check a condition against all elements of sequence and sequence should not be empty.firstOrThrow
: returns first element if none throw error.lastOrThrow
: returns last element if none throw error.fistIndex
: return index of first element which is true for a predicatelastIndex
: return index of last element which is true for a predicate