marinehero / ts-generic-collections

TypeScript library provides generic, strongly-typed, queryable collections

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool


TypeScript library provides generic, strongly-typed, queryable collections

Build Status

The generic collections are:

  • List
  • Dictionary

All collections implement interface IEnumerable<T>

export interface IEnumerable<T> {
    elementAt(index: number) : T;
    any(predicate?: (item: T)=> boolean) : boolean;
    all(predicate?: (item: T)=> boolean) : boolean;
    single(predicate?: (item: T)=> boolean) : T;
    first(predicate?: (item: T)=> boolean) : T;
    last(predicate?: (item: T)=> boolean) : T;
    singleOrDefault(predicate: (item: T)=> boolean) : T;    
    firstOrDefault(predicate: (item: T)=> boolean) : T;
    lastOrDefault(predicate: (item: T)=> boolean) : T;
    where(predicate: (item: T)=> boolean) : IEnumerable<T>;
    select<TResult>(predicate: (item: T)=> TResult) : IEnumerable<TResult>;
    join<TOuter, TMatch, TResult>(outer: IEnumerable<TOuter>, conditionInner: (item: T)=> TMatch, 
                                    conditionOuter: (item: TOuter)=> TMatch, select: (x: T, y:TOuter)=> TResult, leftJoin?: boolean) : IEnumerable<TResult>; 
    groupBy(predicate: (item: T) => Array<any>) : IEnumerable<IGroup<T>>;
    orderBy(comparer: IComparer<T>) : IEnumerable<T>;
    distinct(comparer: IEqualityComparer<T>) : IEnumerable<T>;
    union(list: IEnumerable<T>) : IEnumerable<T>;
    skip(no: number) : IEnumerable<T>;
    take(no: number) : IEnumerable<T>;
    sum(predicate: (item: T)=> number) : number;
    avg(predicate: (item: T)=> number) : number;
    min(predicate: (item: T)=> number) : number;
    max(predicate: (item: T)=> number) : number;
    count(predicate?: (item: T)=> boolean) : number;
    forEach(predicate: (item: T)=> void) : void;
    length: number;
    toArray() : Array<T>;
    asEnumerable() : IEnumerable<T>;


List implements interface IList<T>

export interface IList<T> extends IEnumerable<T> {
    add(item: T) : void;
    addRange(items: T[]) : void;
    remove(predicate: (item:T) => boolean) : void;
    clear() : void;

You can create queries like below

Import the library (if using npm package):

import { List, Dictionary } from 'ts-generic-collections-linq/src/public_api'

Below query gets the owners by the sex of their pets.

    let owners = new List<Owner>();

    let owner = new Owner(); = 1; = "John Doe";

    owner = new Owner(); = 2; = "Jane Doe";

    let pets = new List<Pet>();

    let pet = new Pet();
    pet.ownerId = 2; = "Sam"; = Sex.M;


    pet = new Pet();
    pet.ownerId = 1; = "Jenny"; = Sex.F;


    //query to get owners by the sex/gender of their pets
    let ownersByPetSex = owners.join(pets, owner =>, pet => pet.ownerId, (x, y) => new OwnerPet(x,y))
                               .groupBy(x => [])
                               .select(x =>  new OwnersByPetSex(x.groups[0], => x.owner)));

    expect(ownersByPetSex.toArray().length === 2).toBeTruthy();

    expect(ownersByPetSex.toArray()[0].sex == Sex.F).toBeTruthy();
    expect(ownersByPetSex.toArray()[0].owners.length === 1).toBeTruthy();
    expect(ownersByPetSex.toArray()[0].owners.toArray()[0].name == "John Doe").toBeTruthy();

    expect(ownersByPetSex.toArray()[1].sex == Sex.M).toBeTruthy();
    expect(ownersByPetSex.toArray()[1].owners.length == 1).toBeTruthy();
    expect(ownersByPetSex.toArray()[1].owners.toArray()[0].name == "Jane Doe").toBeTruthy();                               

You can instantiate a List from JSON as shown below

    let jsonOwnerArray = '[{"id":1, "name": "John Doe"},{"id":2, "name": "Jane Doe"}]';

    let ownerArray: Owner[] = JSON.parse(jsonOwnerArray);

    let list = new List(ownerArray);

Entities for above example are

class Owner {
    id: number;
    name: string;

class Pet {
    ownerId: number;
    name: string;
    sex: Sex;

enum Sex {

class OwnerPet {
    owner: Owner;
    pet: Pet;

    constructor(owner: Owner, pet: Pet) {
        this.owner = owner; = pet;

class OwnersByPetSex {
    sex: Sex;
    owners: IEnumerable<Owner>;

    constructor(sex: Sex, owners: IEnumerable<Owner>) { = sex;
        this.owners = owners;


Dictionary implements interface IDictionary<TKey, TValue>

export interface IDictionary<TKey, TValue> extends IEnumerable<KeyValuePair<TKey, TValue>> {
    add(key: TKey, value: TValue) : void;
    addRange(items: KeyValuePair<TKey, TValue>[]) : void;
    remove(predicate: (item:KeyValuePair<TKey, TValue>) => boolean) : void;
    clear() : void;

    containsKey(key: TKey) : boolean;
    containsValue(value: TValue) : boolean;
    tryGetValue(key: TKey) : TValue;

You can browse more examples of queries below



This project was generated with Angular CLI version 7.2.2.


TypeScript library provides generic, strongly-typed, queryable collections

License:MIT License


Language:TypeScript 97.9%Language:JavaScript 2.1%