Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

math-helper-functions

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

math-helper-functions - npm Package Compare versions

Comparing version 1.5.0 to 1.6.0

73

index.js

@@ -13,2 +13,16 @@ const {sum, max, min, mean, median, histogram} = require('d3-array');

/**
* Gets args for functions
*
* @static
* @private
* @param {Array} array
* @param {string} property
* @returns {Array}
* @memberof MathFunctions
*/
static getArgs(array, property) {
return property ? [array, (d) => get(d, property)] : [array];
}
/**
* Returns array max value

@@ -22,3 +36,3 @@ *

static calcMax(array, property = null) {
const args = property ? [array, (d) => get(d, property)] : [array];
const args = MathFunctions.getArgs(array, property);
return max(...args);

@@ -36,3 +50,3 @@ }

static calcSum(array, property = null) {
const args = property ? [array, (d) => get(d, property)] : [array];
const args = MathFunctions.getArgs(array, property);
return sum(...args);

@@ -49,3 +63,3 @@ }

static calcMin(array, property = null) {
const args = property ? [array, (d) => get(d, property)] : [array];
const args = MathFunctions.getArgs(array, property);
return min(...args);

@@ -78,3 +92,3 @@ }

static calcMedian(array, property = null) {
const args = property ? [array, (d) => get(d, property)] : [array];
const args = MathFunctions.getArgs(array, property);
return median(...args);

@@ -84,2 +98,51 @@ }

/**
* Returns weighted median of array
*
* @static
* @param {object[]} array Array to find weighted median of
* @param {string} valueProperty Property to use for array item value
* @param {string} weightProperty Property to use for array item weight
* @returns {number} Weighted median
* @memberof MathFunctions
*/
static calcWeightedMedian(array, valueProperty, weightProperty) {
// Prevent undefined problems
if (array.length === 0) {
return 0;
}
const {array: arrayToSort, weightSum} = array.reduce(
(acc, item) => {
acc.array.push({
value: item[valueProperty],
weight: item[weightProperty],
});
acc.weightSum += item[weightProperty];
return acc;
},
{
array: [],
weightSum: 0,
}
);
const sortedArray = arrayToSort.sort((a, b) => a.value - b.value);
const midpoint = weightSum / 2;
let index = 0;
let weight = 0;
while (weight < midpoint) {
weight += sortedArray[index].weight;
index++;
}
if (weight === midpoint) {
return (sortedArray[index - 1].value + sortedArray[index].value) / 2;
} else {
return sortedArray[index - 1].value;
}
}
/**
* Returns mean value of array

@@ -94,3 +157,3 @@ *

static calcMean(array, property = null) {
const args = property ? [array, (d) => get(d, property)] : [array];
const args = MathFunctions.getArgs(array, property);
return mean(...args);

@@ -97,0 +160,0 @@ }

2

package.json
{
"name": "math-helper-functions",
"version": "1.5.0",
"version": "1.6.0",
"description": "Helper with misc. math functions such as sums, averages, max, min, etc",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -14,2 +14,3 @@ <a name="MathFunctions"></a>

- [.calcMedian(array, [property])](#MathFunctions.calcMedian) ⇒ <code>number</code>
- [.calcWeightedMedian(array, valueProperty, weightProperty)](#MathFunctions.calcWeightedMedian) ⇒ <code>number</code>
- [.calcMean(array, [property])](#MathFunctions.calcMean) ⇒ <code>number</code>

@@ -100,2 +101,17 @@ - [.calcWeightedMean(array, valueProperty, weightProperty)](#MathFunctions.calcWeightedMean) ⇒ <code>number</code>

<a name="MathFunctions.calcWeightedMedian"></a>
### MathFunctions.calcWeightedMedian(array, valueProperty, weightProperty) ⇒ <code>number</code>
Returns weighted median of array
**Kind**: static method of [<code>MathFunctions</code>](#MathFunctions)
**Returns**: <code>number</code> - Weighted median
| Param | Type | Description |
| -------------- | --------------------------------- | ------------------------------------- |
| array | <code>Array.&lt;object&gt;</code> | Array to find weighted median 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.calcMean"></a>

@@ -102,0 +118,0 @@

@@ -150,2 +150,23 @@ const MathFunctions = require('../index');

describe('Testing WEIGHTED MEDIAN and MEDIAN methods', () => {
const weightedSimpleArray = TEST_ARRAY.map((d) => ({
value: d,
weight: 1,
}));
test('The weighted median of elements with the same weight should be the median of the array values', () => {
expect(MathFunctions.calcWeightedMedian(weightedSimpleArray, 'value', 'weight')).toStrictEqual(MathFunctions.calcMedian(weightedSimpleArray, 'value'));
});
test('The weighted median of elements with different weight should not be the median of the array values', () => {
expect(MathFunctions.calcWeightedMedian(
weightedSimpleArray.map((d, index) => ({
value: d.value,
weight: index + 1,
})),
'value',
'weight'
)).not.toEqual(MathFunctions.calcMedian(weightedSimpleArray, 'value'));
});
});
describe('Testing PERCENTAGE methods', () => {

@@ -152,0 +173,0 @@ test('Percentages should work correctly', () => {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc