math-helper-functions
Advanced tools
Comparing version 1.2.0 to 1.3.0
26
index.js
@@ -5,3 +5,3 @@ const {max, min, mean, median, histogram} = require('d3-array'); | ||
const get = require('lodash/get'); | ||
const NumberHelper = require('number-helper-functions'); | ||
const {processNumber} = require('number-helper-functions'); | ||
const {quartiles, histogram: jStatHistogram} = require('jstat'); | ||
@@ -90,3 +90,3 @@ | ||
/** | ||
* Returns mean value in array | ||
* Returns mean value of array | ||
* | ||
@@ -106,2 +106,21 @@ * @static | ||
/** | ||
* Returns weighted mean of array | ||
* | ||
* @static | ||
* @param {object[]} array Array to find weighted mean of | ||
* @param {string} valueProperty Property to use for array item value | ||
* @param {string} weightProperty Property to use for array item weight | ||
* @returns {number} Weighted mean | ||
* @memberof MathFunctions | ||
*/ | ||
static calcWeightedMean(array, valueProperty, weightProperty) { | ||
if (!valueProperty || !weightProperty) { | ||
throw new Error('Both valueProperty and weightProperty params are required'); | ||
} | ||
const totalWeight = MathFunctions.calcSum(array, weightProperty); | ||
const weightedValues = array.map((d) => d[valueProperty] * d[weightProperty]); | ||
return MathFunctions.calcSum(weightedValues) / totalWeight; | ||
} | ||
/** | ||
* Calculate percentage using rule of three | ||
@@ -129,4 +148,3 @@ * | ||
static calcDistribution(array, numOfBins) { | ||
const numHelper = new NumberHelper(); | ||
const numArray = array.map((d) => numHelper.processNumber(d)); | ||
const numArray = array.map((d) => processNumber(d)); | ||
const hist = numOfBins ? histogram().thresholds(numOfBins) : histogram(); | ||
@@ -133,0 +151,0 @@ const dist = hist(numArray); |
{ | ||
"name": "math-helper-functions", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Helper with misc. math functions such as sums, averages, max, min, etc", | ||
@@ -19,2 +19,4 @@ "main": "index.js", | ||
"max", | ||
"mean", | ||
"weighted", | ||
"percent", | ||
@@ -32,3 +34,3 @@ "domain", | ||
"lodash": "^4.17.15", | ||
"number-helper-functions": "^1.0.0" | ||
"number-helper-functions": "^2.1.0" | ||
}, | ||
@@ -35,0 +37,0 @@ "prettier": { |
@@ -15,2 +15,3 @@ <a name="MathFunctions"></a> | ||
- [.calcMean(array, [property])](#MathFunctions.calcMean) ⇒ <code>Number</code> | ||
- [.calcWeightedMean(array, valueProperty, weightProperty)](#MathFunctions.calcWeightedMean) ⇒ <code>number</code> | ||
- [.calcPercent(toCalc, total)](#MathFunctions.calcPercent) ⇒ <code>Number</code> | ||
@@ -103,3 +104,3 @@ - [.calcDistribution(array, [numOfBins])](#MathFunctions.calcDistribution) ⇒ <code>object</code> | ||
Returns mean value in array | ||
Returns mean value of array | ||
@@ -114,2 +115,17 @@ **Kind**: static method of [<code>MathFunctions</code>](#MathFunctions) | ||
<a name="MathFunctions.calcWeightedMean"></a> | ||
### MathFunctions.calcWeightedMean(array, valueProperty, weightProperty) ⇒ <code>number</code> | ||
Returns weighted mean of array | ||
**Kind**: static method of [<code>MathFunctions</code>](#MathFunctions) | ||
**Returns**: <code>number</code> - Weighted mean | ||
| Param | Type | Description | | ||
| -------------- | --------------------------------- | ------------------------------------- | | ||
| array | <code>Array.<object></code> | Array to find weighted mean of | | ||
| valueProperty | <code>string</code> | Property to use for array item value | | ||
| weightProperty | <code>string</code> | Property to use for array item weight | | ||
<a name="MathFunctions.calcPercent"></a> | ||
@@ -116,0 +132,0 @@ |
25086
440
199
+ Addednumber-helper-functions@2.1.1(transitive)
- Removednumber-helper-functions@1.0.1(transitive)