decaying-accumulator

0.1.2 • Public • Published

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.)

Readme

Keywords

none

Package Sidebar

Install

npm i decaying-accumulator

Weekly Downloads

1

Version

0.1.2

License

MIT

Last publish

Collaborators

  • begriffs