An implementation of collections using TypeScript.
The collections are implements basic functionality such as "find", "sort", "filter", "map", "decorate", etc.
- Full compatibility with the Angular2 iterators, therefore you can use them inside the Angular2 templates.
- Full compatibility with the lodash.
- Full compatibility with the ES6 iterable protocol.
- Partial compatibility with the JavaScript Array (find/filter/forEach methods).
First you need to install the npm module:
npm install ts-collections --save
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>
npm deploy
Licensed under MIT.