calc-stats
Advanced tools
+33
-11
@@ -9,2 +9,3 @@ const { getOrCreateIterator } = require("iter-fun"); | ||
| noData = undefined, | ||
| filter = undefined, | ||
| calcHistogram = true, | ||
@@ -22,3 +23,3 @@ calcMax = true, | ||
| let needCount = calcMean || calcMedian; | ||
| let needCount = calcMean || calcMedian || typeof filter === "function"; | ||
| let needHistogram = calcHistogram || calcMedian || calcMode || calcModes; | ||
@@ -28,2 +29,3 @@ let needSum = calcSum || calcMean; | ||
| let count = 0; | ||
| let index = 0; | ||
| let min; | ||
@@ -34,15 +36,35 @@ let max; | ||
| const step = value => { | ||
| if (value !== noData) { | ||
| if (needCount) count++; | ||
| if (calcMin && (min === undefined || value < min)) min = value; | ||
| if (calcMax && (max === undefined || value > max)) max = value; | ||
| if (needSum) sum += value; | ||
| if (needHistogram) { | ||
| if (value in histogram) histogram[value].ct++; | ||
| else histogram[value] = { n: value, ct: 1 }; | ||
| } | ||
| // after it processes filtering | ||
| const process = value => { | ||
| if (needCount) count++; | ||
| if (calcMin && (min === undefined || value < min)) min = value; | ||
| if (calcMax && (max === undefined || value > max)) max = value; | ||
| if (needSum) sum += value; | ||
| if (needHistogram) { | ||
| if (value in histogram) histogram[value].ct++; | ||
| else histogram[value] = { n: value, ct: 1 }; | ||
| } | ||
| }; | ||
| let step; | ||
| if (typeof noData === "number" && typeof filter === "function") { | ||
| step = value => { | ||
| index++; | ||
| if (value !== noData && filter({ count, index, value }) === true) { | ||
| process(value); | ||
| } | ||
| }; | ||
| } else if (typeof noData === "number") { | ||
| step = value => value !== noData && process(value); | ||
| } else if (typeof filter === "function") { | ||
| step = value => { | ||
| index++; | ||
| if (filter({ count, index, value }) === true) { | ||
| process(value); | ||
| } | ||
| }; | ||
| } else { | ||
| step = process; | ||
| } | ||
| const finish = () => { | ||
@@ -49,0 +71,0 @@ const results = {}; |
+1
-1
| { | ||
| "name": "calc-stats", | ||
| "version": "0.3.2", | ||
| "version": "0.3.3", | ||
| "description": "Memory-Aware Statistical Calculations", | ||
@@ -5,0 +5,0 @@ "main": "calc-stats.js", |
+17
-0
@@ -65,2 +65,19 @@ # calc-stats | ||
| ## filtering | ||
| If you want to ignore some values, you can use a filter function: | ||
| ```js | ||
| const results = calcStats(data, { | ||
| filter: ({ index, value }) => { | ||
| // ignore the first 10 numbers | ||
| if (index < 10) return false; | ||
| // ignore any negative numbers | ||
| // or values greater than 100 | ||
| if (value < 0 && value > 100) return false; | ||
| return true; | ||
| } | ||
| }) | ||
| ``` | ||
| ## specify calculations | ||
@@ -67,0 +84,0 @@ If you only care about specific statistics, you can configure calcStats through an options object: |
13664
7.64%98
27.27%101
20.24%