modern-linq
TypeScript icon, indicating that this package has built-in type declarations

0.9.2 • Public • Published

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:

  1. The data is processed in the moment when is requested.
  2. 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.map
  • where: uses Array.prototype.filter
  • take: uses Array.prototype.slice
  • skip: uses Array.prototype.slice
  • distinct: if no comparer is provided it uses native Set class
  • count: returns Array.prototype.length
  • orderBy: Array.prototype.sort
  • concat: 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 separator
  • allAndEvery: 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 predicate
  • lastIndex: return index of last element which is true for a predicate

Build status:
Build Status

Package Sidebar

Install

npm i modern-linq

Weekly Downloads

0

Version

0.9.2

License

MIT

Unpacked Size

127 kB

Total Files

43

Last publish

Collaborators

  • indomitable