Skip to content

Hanumanth77/ts-collections

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ts-collections

An implementation of collections using TypeScript.

Description

The collections are implements basic functionality such as "find", "sort", "filter", "map", "decorate", etc.

Features

  1. Full compatibility with the Angular2 iterators, therefore you can use them inside the Angular2 templates.
  2. Full compatibility with the lodash.
  3. Full compatibility with the ES6 iterable protocol.
  4. Partial compatibility with the JavaScript Array (find/filter/forEach methods).

Installation

First you need to install the npm module:

npm install ts-collections --save

Use

let list:_.List<number> = Collections.emptyList<number>().add(100).add(50).add(200);
console.log(_(list).min()); // 50

ProductsStore.ts
You can make the ProductsStore singleton the inheritor of collection and bind them into Angular2 template directly, then use ngFor.

import {Injectable} from '@angular/core';

import {ArrayList} from 'ts-collections';
import {Product} from '../../model/Product';

@Injectable()
export class ProductsStore extends ArrayList<Product> {

    constructor() {
        super();
    }
}
*ngFor="let product of productsStore"

App.ts

import {
    ICollection,
    Collections,
} from 'ts-collections';

@Component({...})
export class App {
    
    private filteredDecoratedCollection:ICollection<string|number>;

    constructor() {
        const collection:ICollection<string> = Collections.emptyList<string>()
            .add("hello")
            .add("hello world");

        console.log('Collection size:', collection.getSize());                                                  // Collection size: 2

        this.filteredDecoratedCollection = Collections.makeDecoratedList<string, number>(collection, {
            decorate(item:string): number {
                return item.length;
            }
        }).filter({
            check: (wordLength:number) => {
                return wordLength > 5
            }
        });

        console.log('Filtered decorated collection size:', this.filteredDecoratedCollection.getSize());      // Filtered decorated collection size: 1
    }
}

app.html

Angular2 template

<div *ngFor="let decoratedItem of filteredDecoratedCollection">
{{ decoratedItem }}
</div>

Publish

npm deploy

License

Licensed under MIT.

About

[JOB][An implementation of collections using TypeScript]

Resources

Stars

Watchers

Forks

Packages

No packages published