1.0.24 • Published 7 years ago

tslinq v1.0.24

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

TSLinq

Version Build Status dependencies Status devDependencies Status Downloads

Details

TSLinq is an ES5 compatible port of .NET's LINQ library which tries to be as true to the original as possible.

TSLinq utilises lazily evaluated Enumerable<T>'s, rather than eager evaluation found in other libraries. In addition, it supports ES6 generators allowing for powerful data manipulation.

Ships with a fully functional implementation of Dictionary<TKey, TValue>() which supports collision handling, proper identity hashing by default and custom equality comparers.

Install

npm install tslinq

Usage

import { Enumerable } from 'tslinq'

let things = Enumerable.Of([1, 2, 3])
    .Select(a => a + 2)
    .Where(a => a < 5)
    .Distinct()
    .ToArray();
    
console.log(things);

// Outputs [ 3, 4 ]

Using ES6 generator functions

function* GetNumbers() {
    let i = 0;
    while(true) {
        yield i++;
    }
}

let FirstTenNumbers = Enumerable.Of(GetNumbers)
    .Take(10)
    .ToArray();
    
console.log(FirstTenNumbers);

// Outputs [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Using manually created generator functions

let i = 0;
const generator = () => {
    return {
        next: function () {
            if (i >= 3) {
                throw new Error('Generator should not be invoked when the enumerable hasn\'t been materialized');
            }
            return { done: false, value: i++ };
        }
    };
};

const result =
    Enumerable.Of(generator)
        .Take(3)
        .ToArray();

console.log(result);

// Outputs [ 0, 1, 2 ]

Using Dictionary<TKey, TValue>

Basic implementation

const objectA: any = {};
const objectB: any = {};

const dictionary = new Dictionary<any, number>();
dictionary.Add(objectA, 5);
dictionary.Add(objectB, 10);

dictionary.Get(objectA); // Returns 5
dictionary.Get(objectB); // Returns 10

Using a custom equality comparer

const objectA: any = {};
const objectB: any = {};

const equalityComparer = {
    Equals: (left: any, right: any) => true,
    GetHashCode: (item: any) => JSON.stringify(item)
};

const dictionary = new Dictionary<any, number>(equalityComparer);
dictionary.Add(objectA, 5);
dictionary.Add(objectB, 10); // Throws an exception, key already exists, as the JSON strings match, 
                             // and we always return true when comparing
const objectA: any = {};
const objectB: any = {};

const equalityComparer = {
    Equals: (left: any, right: any) => left === right,
    GetHashCode: (item: any) => JSON.stringify(item)
};

const dictionary = new Dictionary<any, number>(equalityComparer);
dictionary.Add(objectA, 5);
dictionary.Add(objectB, 10); // Does not throw - collisions are properly handled,
                             // And we then check for identity equality
1.0.24

7 years ago

1.0.22

7 years ago

1.0.21

7 years ago

1.0.20

7 years ago

1.0.19

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.1-3.1

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago