Package Exports
- simple-average
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (simple-average) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
simple-average
Average module. An efficient way to calculate average.
Why?
- Abstract the process of calculate the average in a independent module.
- Calculate it using a efficient memory way.
- Simple and Chainable API.
I was looking for a better solution than store all values in an array, sum all and divide for the length. This module is inspired in Daniel Bernier blog that's provide a memory efficient solution for the problem.
Install
npm install simple-average --save
If you want to use in the browser (powered by Browserify):
bower install simple-average --save
and later link in your HTML:
<script src="bower_components/simple-average/dist/simple-average.js"></script>
Usage
First, load the library:
var Average = require('simple-average');
Then create a new instance to use:
var average = new Average();
Now you can add new samples to calculate the average:
average.add(2);
Also you can provide a n
number of values to add:
average.add(2, 3, 4, 5);
or provide an array of values:
var values = [2, 3, 4, 5];
average.add(values);
Methods are chainable as well:
console.log(average.add(2).add(3).add(4).add(5).resume())
// => 3.5
API
.new([Options] {Object})
Create a new instance. Options are based in pretty-ms#options.
.reset()
Reset the current counter.
.add(values... {Number})
Add values to calculate the average. You can provide a n
number of values or an array of values.
.count()
Get the current number of values added for calculate the average.
.resume()
Get an output with the average.
.round([newRound] {Number})
Get the current number of decimals to fix in the ouptut or set a new value.
.unit([newUnit] {String})
Get the current unit to output or set a new value.
Examples
see examples.js
License
MIT © Kiko Beats