collectionjs

0.3.4 • Public • Published

collectionjs

How to use?

// create new instance
var CL = new Collection('Collection Name');
 
// add objects to collection
var a = {x: 1};
var b = {x: 2};
 
CL.add(a); // returns true
CL.add(a); // returns fase, item already exists
 
// remove from collection
CL.remove(a); // returns true
CL.remove(b); // returns false
 
// check existing
CL.exists(b); // returns false
 
CL.add(b); // returns true
CL.exists(b); // returns true
 
// get items list
var CL1 = new Collection('Collection 1 Name');
var CL2 = new Collection('Collection 2 Name');
 
CL1.add(a); // returns true
CL1.add(b); // returns true
 
CL2.add(b); // returns true
 
CL1.list(); // returns [{x: 1}, {x: 2}]
CL2.list(); // returns [{x: 2}]

Methods

  • indexed(key): return array of objects indexed by key
  • each(callback): for each object
  • add(object): add object to collection, return boolean
  • remove(object): remove object from collection, return deleted object
  • get(): get some object from collection
  • exists(object): check object existing in collection
  • contains(object): alias for exists method
  • list(): return array of objects
  • count(): return count of collection objects
  • on(eventName, callback): add event listner

Events

  • Event add (obj)
  • Event remove (obj)
  • Event change (obj, boolean isAdd)
CL.on('add', function (obj) {
    console.log(obj);
});

Readme

Keywords

none

Package Sidebar

Install

npm i collectionjs

Weekly Downloads

6

Version

0.3.4

License

MIT

Last publish

Collaborators

  • redfiber