11.1.0 • Published 4 months ago

linq-to-typescript v11.1.0

Weekly downloads
317
License
MIT
Repository
github
Last release
4 months ago

LINQ To TypeScript

  • Implementation of LINQ for TypeScript
  • Targets TypeScript 4.7.X and ES 2022
await from([bing, google, quackQuackGo])
    .asParallel()
    .selectAsync(downloadHtml)
    .select(getTitle)
    .toArray()

Getting Started

npm i linq-to-typescript

npm npm bundle size License npm

tsconfig.json

"compilerOptions": {
    "target": "es2022",
    "lib": [
      "es2022"
    ]
}
  • The strict TS option is recommended.

Using the Library

With Wrappers

// 0. Import Module
import { from } from "linq-to-typescript"

// To Use With Wrappers
const evenNumbers = from([1, 2, 3, 4, 5, 6, 7, 8, 9]).where((x) => x % 2 === 0).toArray()

Without Wrappers

// 0. Import Module
import { initializeLinq, IEnumerable } from "linq-to-typescript"
// 1. Declare that the JS types implement the IEnumerable interface
declare global {
    interface Array<T> extends IEnumerable<T> { }
    interface Uint8Array extends IEnumerable<number> { }
    interface Uint8ClampedArray extends IEnumerable<number> { }
    interface Uint16Array extends IEnumerable<number> { }
    interface Uint32Array extends IEnumerable<number> { }
    interface Int8Array extends IEnumerable<number> { }
    interface Int16Array extends IEnumerable<number> { }
    interface Int32Array extends IEnumerable<number> { }
    interface Float32Array extends IEnumerable<number> { }
    interface Float64Array extends IEnumerable<number> { }
    interface Map<K, V> extends IEnumerable<[K, V]> { }
    interface Set<T> extends IEnumerable<T> { }
    interface String extends IEnumerable<string> { }
}
// 2. Bind Linq Functions to Array, Map, etc
initializeLinq()
// 3. Use without a wrapper type
const evenNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9].where((x) => x % 2 === 0).toArray()

Examples

Please refer to the examples folder

ES6 Modules (ESM)

To use library with ES6 modules make sure that you specify "type": "module" in package.json

API

TypeDoc API Surface Documentation

LinqToTypeScript implements the functionality of the IEnumerable interface

  • IEnumerable, IAsyncEnumerable, and IParallelEnumerable interfaces are based on,
  • IEnumerable<T> Interface
  • Some changes made due to conflics with existing method names
  • Some changes made due to limitations of JavaScript

IEnumerable

  • Inspired by LINQ API Surface
  • Has Async methods that return Promise or IAsyncEnumerable
  • Implements Iterable<T>
  • Use from to wrap your arrays

IAsyncEnumerable

  • Inspired by LINQ API Surface
  • Has Async methods that return Promise or IAsyncEnumerable
  • For asynchronous iteration
  • Implements AsyncIterable<T> interface
  • Use fromAsync to wrap your AsyncIterable type

IParallelEnumerable

  • Inspired by LINQ API Surface
  • Has Async methods that return Promise or IParallelEnumerable
  • For asynchronous iteration in parallel (where possible)
  • Implements AsyncIterable<T> interface
  • Use fromParallel to create a parallel enumeration

Shared Instance Methods

MethodAsync*Tests CoverageNotes
aggregateNoSync
allYesSync, Async
anyYesSync, Async
appendNoSync
averageYesSync, Async
chunkNoSync
concatenateNoSyncEquivalent to .Concat but renamed to avoid conflict with JS
containsYesSync, Async
countYesSync, Async
defaultIfEmptyNoSync
distinctYesSync, Async
elementAtNoSync
elementAtOrDefaultNoSync
exceptYesSync, Async
firstYesSync, Async
firstOrDefaultYesSync, Async
eachYesSync, AsyncFrom List<T>.ForEach
groupByYesSync, Async
groupByWithSelNoSync
groupJoinYesSync, Async
intersectYesSync, Async
joinByKeyNoSync
lastYesSync, Async
lastOrDefaultYesSync, Async
maxYesSync, Async
minYesSync, Async
ofTypeNoSync
orderNoSync
orderByYesSync, Async
orderByDescendingYesSync, Async
orderDescendingNoSync
partitionYesSync, Async
prependNoSync
reverseNoSync
selectYesSync, Async
selectManyYesSync, Async
sequenceEqualsYesSync, Async
singleYesSync, Async
singleOrDefaultYesSync, Async
skipNoSync
skipWhileYesSync, Async
sumYesSync, Async
takeNoSync
takeWhileYesSync, Async
toArrayNoSync
toMapYesSync, AsyncEquivalent to ToDictionary
toObjectYesSync, Async
toSetNoSyncEquivalent to ToHashSet. No comparer overload for JS.
unionYesSync
whereYesSync, Async
zipYesSync, Async

* Async methods take an async function

Static Methods

MethodAsyncParallelTests Coverage
emptyemptyAsyncemptyParallelTest
enumerateObjectenumerateObjectAsyncN/ATest
flattenflattenAsyncflattenParallelTest
rangerangeAsyncrangeParallelTest
repeatrepeatAsyncrepeatParallelTest

Index Methods

MethodNotes
bindArrayBinds IEnumerable methods to an ArrayLike Iterable type
bindLinqBinds IEnumerable methods to an Interable type
bindLinqAsyncBinds IAsyncEnumerable methods to an AsyncIterable type
isEnumerableDetermines if source implements IEnumerable
isAsyncEnumerableDetermines if source implements IAsyncEnumerable
isParallelEnumerableDetermines if source implements IParallelEnumerable
initializeLinqBinds to IEnumerable to Array Types, Map, Set, & String

Exception Types

ExceptionNotes
ArgumentOutOfRangeExceptionThrown when a passed in argument is invalid
InvalidOperationExceptionThrown when no elements or no predicate match

Design

Binding new APIs to Array Types

JavaScript doesn't have extension methods like in C#, therefore we extend the class itself with new methods. Call initializeLinq to bind library functions to default Array methods,

The following collections support IEnumerable,

  • Array
  • Map
  • Set
  • String
  • Int8Array
  • Int16Array
  • Int32Array
  • Uint8Array
  • Uint8ClampedArray
  • Uint16Array
  • Uint32Array
  • Float32Array
  • Float64Array

Using Wrappers

NOTE: Wrappers are safer as they won't interfere with other libraries.

// To Create an IEnumerable<T>
import { from } from "linq-to-typescript"
from(iterableIteratorOrArray)

// To Create an IAsyncEnumerable<T>
import { fromAsync } from "linq-to-typescript"
fromAsync(asyncIterableIteratorOrPromiseArray)

// To Create an IParallelEnumerable<T>
// You have to specify the parallel generator function type
import { fromParallel, ParallelGeneratorType } from "linq-to-typescript"
fromParallel(ParallelGeneratorType.PromiseToArray, asyncFuncThatReturnsAnArray)

Issues and Questions

Q1: How does this compare to other LINQ libraries?

Other libraries tend to use eager evaluation and work with arrays instead of iterables.

Q2: Can I use your code?

With attribution; the code is licensed under MIT.

Q3: Why should I use this instead of lodash or something similar?

The whole library is written in TypeScript first and avoids typechecking done by TypeScript Language Service.

Lazy evaluation. Not much happens until you iterate over the enumerable or conver it to an Array, Map, etc.

Q4: Is IE11 supported?

No.

Q5: Can I contribute?

Please do!

11.1.0

4 months ago

11.0.0

1 year ago

11.0.0-beta

2 years ago

10.0.0

2 years ago

10.0.0-beta2

2 years ago

10.0.0-beta1

2 years ago

9.0.0

3 years ago

9.0.0-beta2

3 years ago

9.0.0-beta

3 years ago

8.1.0

3 years ago

8.0.0

3 years ago

8.0.0-beta3

3 years ago

8.0.0-beta2

3 years ago

8.0.0-beta

3 years ago

7.0.0

4 years ago

7.0.0-beta

4 years ago

6.0.1

4 years ago

6.0.0

4 years ago

6.0.0-beta

4 years ago

5.0.0

5 years ago

5.0.0-beta

5 years ago

4.0.0

5 years ago

4.0.0-beta1

5 years ago

4.0.0-alpha5

5 years ago

4.0.0-alpha4

5 years ago

4.0.0-alpha2

5 years ago

4.0.0-alpha1

5 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.0

6 years ago

3.0.0-rc1

6 years ago

2.1.0

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

2.0.0-beta3

6 years ago

2.0.0-beta2

6 years ago

2.0.0-beta

6 years ago

2.0.0-alpha5

6 years ago

2.0.0-alpha4

6 years ago

2.0.0-alpha3

6 years ago

2.0.0-alpha2

6 years ago

1.0.0

6 years ago