smallorange-collection-handler
v1.0.0
Published
Lightweight collection handler based on lodash.
Downloads
3
Readme
Small Orange Collection Handler
Lightweight collection handler based on lodash.
API
constructor(source: Array<string | object>, indexAttr?: string);
before(item: string | number): instance;
after(item: string | number): instance;
limit(n: number): instance;
desc(): instance;
stats: {
count: number,
total: number,
before: string | number,
after: string | number
};
items: Array<string | object>;
Sample
const CollectionHandler = require('smallorange-collection-handler');
const array = new CollectionHandler(['a', 'b', 'c', 'd', 'e', 'f']);
const collection = new CollectionHandler([{
id: 1,
value: 'a'
}, {
id: 2,
value: 'b'
}, {
id: 3,
value: 'c'
}, {
id: 4,
value: 'd'
}, {
id: 5,
value: 'e'
}, {
id: 6,
value: 'f'
}], 'id');
const arrayResult = array
.before('d')
.limit(2);
const collectionResult = array
.before(4)
.limit(2);
console.log(arrayResult.items); // ['b', 'c']
console.log(arrayResult.stats); // {count: 2, before: 'b', after: 'c', total: 6}
console.log(arrayResult.items); // [{id: 2, value: 'b'}, {id: 3, value: 'c'}]
console.log(arrayResult.stats); // {count: 2, before: 'b', after: 'c', total: 6}
For more info see the specs.