cell-multiset
v0.0.3
Published
Fast JS MultiSet implementation.
Downloads
8
Readme
Fast & consistent JavaScript MultiSet (AKA bag, bunch) implementation. Implemented as part of my upcoming(...loading...) cell game engine, but perfectly usable as a standalone lib.
Features
- Fast!
- Fully tested
- Fully documented
- Pascal case operands can be used to output new multisets
- Chaining
- Lots of aliases
- Prototypal & Classical inheritance ready
- Supports amd, node, globals, es6 modules
- Lots of output options
- ES6 & ES5 binaries (as well as intermediate version)
- Made with bits of love!
Installation
bower install cell-multiset
npm install cell-multiset
Usage
var obj = {iam: 'object'};
var ms1 = MultiSet.create([7, 67, 7, 7, 'text', obj, 'text', obj]); // use an iterable for initialization.
var ms2 = Object.create(MultiSet).init([7, 67, 7, 'text', obj, 'text2', obj, 99]);
var ms3 = new MultiSet.constructor([7, 67, 7, 7, 'text', obj, 'text', obj]); // or use the constructor function. Preferably you'd want to use the CMultiSet export for this.
expect(ms1.size).to.eql(8); // cardinality (@alias) of the multiset.
expect(ms1.toString()).to.eql('{7 => 3, 67 => 1, text => 2, [object Object] => 2}');
expect(ms1.has(7)).to.be.true;
expect(ms1.multiplicity(obj)).to.eql(2);
expect(ms1.multiplicity(undefined)).to.eql(0); // non existent elements have multiplicity 0.
expect(ms1.equals(ms3)).to.be.true; // checks equality between 2 multisets.
ms1.union(ms2);
expect(ms1.toString()).to.eql('{7 => 3, 67 => 1, text => 2, [object Object] => 2, text2 => 1, 99 => 1}');
expect(ms1.size).to.eql(10);
var ms4 = ms3.Union(ms2); // use Pascal case Union to output a new MultiSet and leave ms3 unchanged.
expect(ms3.toString(1)).to.eql('[7, 7, 7, 67, text, text, [object Object], [object Object]]'); // use mode=1 to output single dimension array-like string.
expect(ms4.toString()).to.eql('{7 => 3, 67 => 1, text => 2, [object Object] => 2, text2 => 1, 99 => 1}');
For more usage example see the unit tests @ /test/unit/MultiSet-spec.js
Prototypal(OLOO) vs 'Classical' inheritance
By default prototypal(OLOO) inheritance is supported. Practically this means that the default export will be the MultiSet prototype. Also all static properties will be directly available on the prototype (as well as on the constructor function). If you prefer 'classical' inheritance a CMultiSet export is also provided.
Documentation
Documentation can be generated by running the command below and is outputted @ /doc.
npm run docs
Make sure you'll run a npm install first.