0.0.16 • Published 8 years ago
ts-collections v0.0.16
ts-collections
An implementation of collections using TypeScript.
Description
The collections are implements basic functionality such as "find", "sort", "filter", "map", "decorate", etc.
Features
- 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 protocol (find/filter/forEach methods).
Dependencies
Installation
First you need to install the npm module:
npm install ts-collections --save
Use
import * as _ from 'lodash';
const list:_.List<number> = Collections.emptyList<number>().add(100).add(50).add(200);
expect(_(list).min()).toEqual(501);
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/index';
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/index';
@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.