CountJS
Count items and compare them against a reference
Installation
Simply install it via NPM (npm install countjs
) and require
it
in your code:
var Counter =var c =
Usage
countjs
is relatively simple -- just instantiate a counter and keep
adding stuff to it:
var c =ccc
Then you can get how many items are there in the counter:
c // 2c // 1c // {a: 2, b: 1}
You can also specify a custom quantity to add:
c // 2c // 3
And create a diff between counters:
var c1 = a: 1var c2 = b: 1c1 // {a: 1, b: 0}
You can also use "references": if an item has qty X
in the reference, then your counter will not be able to
add more than X
qty of that item:
var ref = a: 1 b: 2var c = {} refc // truec // falsec // falsec // truec // falsec // {a: 1, b: 2}
You can force the counter to accept the new value:
var ref = a: 1var c = {} refc // truec // falsec // truec // {a: 2}
When using references, you can get a diff between the current counter and the reference. For example:
var ref = a: 1var c = {} refc // truec // truec // {a: {mine: 2, other: 1, diff: 1}, b: {mine: 3, other: 0, diff: 3}}
Last but not least, you can get a full comparison between counters:
var c1 = a: 1 b: 1 d: 3var c2 = b: 2 c: 1 d: 3comparison = c1// {// a: mine 1, other 0// b: mine 1, other 2// c: mine 0, other 1// d: mine 3, other 3// }
compare
is, basically, a #diff()
that also includes same values
between counters.
Tests
Just run npm test
and welcome to Greenland!