math-helper-functions
Advanced tools
Comparing version 1.6.0 to 1.6.1
@@ -104,2 +104,5 @@ const {sum, max, min, mean, median, histogram} = require('d3-array'); | ||
// Prevent undefined problems | ||
if (!valueProperty || !weightProperty) { | ||
throw new Error('Both valueProperty and weightProperty params are required'); | ||
} | ||
if (array.length === 0) { | ||
@@ -136,5 +139,7 @@ return 0; | ||
if (weight === midpoint) { | ||
return (sortedArray[index - 1].value + sortedArray[index].value) / 2; | ||
const prevItem = get(sortedArray[index - 1], 'value', 0); | ||
const currItem = get(sortedArray[index], 'value', 0); | ||
return (prevItem + currItem) / 2; | ||
} else { | ||
return sortedArray[index - 1].value; | ||
return get(sortedArray[index - 1], 'value', 0); | ||
} | ||
@@ -141,0 +146,0 @@ } |
{ | ||
"name": "math-helper-functions", | ||
"version": "1.6.0", | ||
"version": "1.6.1", | ||
"description": "Helper with misc. math functions such as sums, averages, max, min, etc", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -148,2 +148,8 @@ const MathFunctions = require('../index'); | ||
}); | ||
test('If a value prop or weight prop is not provided for calcWeightedMean method, an error should be thrown', () => { | ||
expect(() => { | ||
MathFunctions.calcWeightedMean([], 'something'); | ||
}).toThrow(); | ||
}); | ||
}); | ||
@@ -160,2 +166,14 @@ | ||
test('The weighted median of an empty array should be 0', () => { | ||
expect(MathFunctions.calcWeightedMedian([], 'value', 'weight')).toStrictEqual(0); | ||
}); | ||
test('The weighted median of an array with one element should be that same value', () => { | ||
expect(MathFunctions.calcWeightedMedian( | ||
weightedSimpleArray.slice(0, 1), | ||
'value', | ||
'weight' | ||
)).toStrictEqual(weightedSimpleArray[0].value); | ||
}); | ||
test('The weighted median of elements with different weight should not be the median of the array values', () => { | ||
@@ -171,2 +189,8 @@ expect(MathFunctions.calcWeightedMedian( | ||
}); | ||
test('If a value prop or weight prop is not provided for calcWeightedMedian method, an error should be thrown', () => { | ||
expect(() => { | ||
MathFunctions.calcWeightedMedian([], 'something'); | ||
}).toThrow(); | ||
}); | ||
}); | ||
@@ -173,0 +197,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36389
748