A lightweight object for array pipelining operations.
var array = 1 2 3;array;
which can be thought of as a pipeline of operations. however there are some functions that do not exist or considered as an edge case which can be fairly simple to implement, but annoying to reuse.
CollectionJS is an extensible object that acts as a wrapper around the Array object, exposing API methods and utilities, and simplifying their use. and it always returns a Collection (except for some examples) meaning you will be able to keep chaining until you get the collection you want to work with.
Working with The PHP framework Laravel, I came across the collection object. I didn't realize how powerful the combination of immutable objects are until I read the Refactoring to collections which is a great read for any developer.
And I had a free week at the time.
npm install collectionsjs --save
bower install collectionjs --save
Download it manually and add the script tag at the bottom of your body, it doesn't matter in which order because there are no dependencies.
However if you have a more complex setup, Then in your code you can do:
var Collection = ;
I have chosen to allow different types to be used for the same parameter, some may see this as some violation of some pattern out there or something, while I like to respect all best practices I think the trade of having less methods to memorize and the versatility of one method is better than writing over 2x the same amount of functions for slightly different behaviors.
- Most of the methods just defer to the existing array functions like filter and map.
- Many methods accept a different argument types for the same parameter, which will dictate its behavior like
- Few methods are just an alias.
sortBy()sorting doesn't change the original collection order.
There are just over 25+ unique methods for most of your needs I will be adding more useful ones in time.
Here is a brief summary, read the docs for different behaviors and usage:
- reduce(callback, initial)
- slice(start, end)
- sortBy(property, order)
npm run test
and you can lint with
npm run lint
Go a head and you will be fully credited. Don't forget to follow the code style and always add a test file for any additions.
Clone the repo:
git clone email@example.com:logaretm/collectionsjs.git
Install the dev dependencies:
Write code better than mine.
Build the library:
npm run build
which will build both minified and unminified versions.
I can always use some help with optimizing things. however most of the methods are based on the underlying Array API.