Skip to content

hcstw/enumerable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Enumerable for Typescript/Javascript

base on es6 generator

use

npm install @hcstw/enumerable

add import '@hcstw/enumerable' to your entry file (like main.ts) to extend Array and NodeList

Enumerable from array

[1,2,3].asEnumerable();

Enumerable from NodeList

document.querySelectorAll('div').asEnumerable()

Enumerable from generator

import { Enumerable } from '@hcstw/enumerable';

function *gen(){
    yield 1;
    yield 2;
    yield 3;
}
Enumerable.from(gen);

or

import { Enumerable } from '@hcstw/enumerable';

Enumerable.from(function*() {
        yield 1;
        yield 2;
        yield 3;
});

use Enumerable

import { Enumerable } from '@hcstw/enumerable';

Enumerable.range(0, 5)
    .select(x => ({ id: x, name: `name-${x}` }))
    .where(x => x.id > 3)
    .toArray()
// output
// [{id: 4, name: "name-4"}]

var owners = [
    { id: 1, name: 'a' },
    { id: 2, name: 'b' },
    { id: 3, name: 'c' }
];
var pets = [
    { owner: 1, name: 'pet-a' },
    { owner: 1, name: 'pet-b' },
    { owner: 2, name: 'pet-c' }
];
owners.asEnumerable()
    .groupJoin(pets, x => x.id, x => x.owner)
    .toDictionary(x => x.key, x => x.toArray());
// output
/*
{
    "1": [
        { "owner": 1, "name": "pet-a" },
        { "owner": 1, "name": "pet-b" }
    ],
    "2": [
        { "owner": 2, "name": "pet-c" }
    ],
    "3": []
}
*/

see ./tests/*.test.ts for more detial examples

implemented function

staitc

empty
range
repeat

instnace

join
selectMany
first
firstOrDefault
last
lastOrDefault
single
singleOrDefault
aggregate
select
count
orderByDescending
oderBy
thenBy
thenByDescending
sum
where
contains
all
any
append
concat
defaultIfEmpty
distinct
elementAt
except
groupBy
groupJoin
intersect
max
min
average
prepend
reverse
sequenceEqual
skip
skipLast
skipWhile
take
takeLast
takeWhile
toDictionary
toArray
union
zip

About

Enumerable for Typescript/Javascript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors