0.9.2 • Published 5 years ago

modern-linq v0.9.2

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

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

0.9.2

5 years ago

0.9.1

5 years ago

0.9.0

5 years ago

0.8.1

5 years ago

0.7.0

5 years ago

0.6.4

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago