Name: countjs
Owner: Namshi
Description: A small utility to count things
Created: 2016-01-04 08:37:38.0
Updated: 2016-03-04 10:15:28.0
Pushed: 2016-01-07 15:48:16.0
Homepage: null
Size: 17
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Count items and compare them against a reference
Simply install it via NPM (npm install countjs
) and require
it
in your code:
Counter = require('countjs')
c = new Counter()
countjs
is relatively simple – just instantiate a counter and keep
adding stuff to it:
c = new Counter()
d('a')
d('a')
d('b')
Then you can get how many items are there in the counter:
t('a') // 2
t('b') // 1
t() // {a: 2, b: 1}
You can also specify a custom quantity to add:
d('a', {qty: 3}) // 2
t('a') // 3
And create a diff between counters:
c1 = new Counter({a: 1})
c2 = new Counter({b: 1})
iff(c2)
: {
mine: 1,
other: 0,
diff: 1
},
b: {
mine: 0,
other: 1,
diff: -1
}
c3 = new Counter({a: 1, b: 1})
iff(c2)
: {
mine: 1,
other: 0,
diff: 1
}
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:
ref = {a: 1, b: 2}
c = new Counter({}, ref)
d('a') // true
d('a') // false
d('b', {qty: 3}) // false
d('b', {qty: 2}) // true
d('c') // false
t() // {a: 1, b: 2}
You can force the counter to accept the new value:
ref = {a: 1}
c = new Counter({}, ref)
d('a') // true
d('a') // false
d('a', {qty: 1, force: true}) // true
t() // {a: 2}
When using references, you can get a diff between the current counter and the reference. For example:
ref = {a: 1}
c = new Counter({}, ref)
d('a', {qty: 2, force: true}) // true
d('b', {qty: 3, force: true}) // true
ff() // {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:
c1 = new Counter({a: 1, b: 1, d: 3})
c2 = new Counter({b: 2, c: 1, d: 3})
arison = c1.compare(c2)
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.
Just run npm test
and welcome to Greenland!