https://github.com/begriffs/decaying-accumulator
A number that tends toward zero but can be nudged
https://github.com/begriffs/decaying-accumulator
Last synced: 5 months ago
JSON representation
A number that tends toward zero but can be nudged
- Host: GitHub
- URL: https://github.com/begriffs/decaying-accumulator
- Owner: begriffs
- Created: 2013-05-07T00:03:19.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2013-05-17T15:50:07.000Z (almost 13 years ago)
- Last Synced: 2025-02-12T04:05:51.559Z (about 1 year ago)
- Language: JavaScript
- Size: 246 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## A nudgeable number that tends toward zero
[](https://travis-ci.org/begriffs/decaying-accumulator)
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](http://begriffs.github.io/decaying-accumulator/)
// 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.)