A number that tends toward zero but can be nudged
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
test
.gitignore
.jshintrc
.travis.yml
DecayingAccumulator.js
README.md
package.json

README.md

A nudgeable number that tends toward zero

Build Status

In applications such as realtime crowd voting or an audio VU meter, we want a number which spikes upward but decays to zero over time. This module captures exactly this, with no extra frills.

Try a demo

// Create an accumulator that decays in one second
var DecayingAccumulator = require('DecayingAccumulator'),
  dac = new DecayingAccumulator({decaySpeed: 1000});

dac.nudge(1);
// now dac.currentValue() === 1

...

// some time later
// 0 <= dac.currentValue() < 1

Individual nudges begin to count for less if they "bury the needle." The currentValue method always returns within -1 and 1 inclusive. You can specify an initial scale when constructing this class.

new DecayingAccumulator({decaySpeed: 1000, currentScale: 4});
// now currentValue() === 0.25 after nudge(1)

If nudges have adjusted the scale you can have it reset after a cooldown period by providing a cooldownSpeed argument to the constructor.

Running tests locally

npm install
node_modules/.bin/mocha

(Or npm install -g mocha to add it to your path.)