Permalink
Browse files

Support mean value

  • Loading branch information...
1 parent 67d428b commit acc209d38aca11b93b612e8805462c5dd3e6c23b @kesla committed Dec 6, 2012
Showing with 21 additions and 23 deletions.
  1. +8 −18 README.md
  2. +10 −4 index.js
  3. +3 −1 package.json
View
@@ -1,25 +1,15 @@
node-minmax[![build status](https://secure.travis-ci.org/kesla/node-minmax.png)](http://travis-ci.org/kesla/node-minmax)
===========
-Calculate the minimum and maximum value.
+Calculate the minimum, maximum value and more.
## Example
```javascript
- var r = minmax();
- r(100);
- assert(r.min === 100);
- assert(r.max === 100);
- assert(r.diff === 0);
-
- r([200, [-100]]);
- assert(r.max === 200);
- assert(r.min === -100);
- assert(r.diff === 300);
-
- r = minmax({ min: 0}); // or max
- r(100);
- assert(r.min === 0);
- assert(r.max === 100);
- r(-100);
- assert(t.min === -100);
+ var mm = minmax();
+ mm(0);
+ mm(-4712);
+ assert.strictEqual(mm.max, 0);
+ assert.strictEqual(mm.min, -4712);
+ assert.strictEqual(mm.diff, 4712);
+ assert.strictEqual(mm.mean, -2356);
```
View
@@ -1,10 +1,14 @@
var assert = require('assert');
+var mean = require('running-mean');
var minmax = module.exports = function() {
+ var r = mean();
function minmax(val) {
if (Array.isArray(val)) {
- val.forEach(function(v) { minmax(v); });
+ return val.forEach(function(v) { minmax(v); });
};
+ r.push(val);
+ minmax.mean = r.mean;
if (minmax.min === null || val < minmax.min) {
minmax.min = val;
minmax.diff = minmax.max - minmax.min;
@@ -25,13 +29,15 @@ mm(4711);
assert.strictEqual(mm.min, 4711);
assert.strictEqual(mm.max, 4711);
assert.strictEqual(mm.diff, 0);
+assert.strictEqual(mm.mean, 4711);
mm = minmax();
mm(0);
-mm(-4711);
+mm(-4712);
assert.strictEqual(mm.max, 0);
-assert.strictEqual(mm.min, -4711);
-assert.strictEqual(mm.diff,4711);
+assert.strictEqual(mm.min, -4712);
+assert.strictEqual(mm.diff, 4712);
+assert.strictEqual(mm.mean, -2356);
mm = minmax();
mm(new Date(0));
View
@@ -10,7 +10,9 @@
"scripts": {
"test": "node index.js"
},
- "dependencies": {},
+ "dependencies": {
+ "running-mean": "0.0.0"
+ },
"devDependencies": {},
"optionalDependencies": {},
"engines": {

0 comments on commit acc209d

Please sign in to comment.