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 2.0.3 to 2.1.0

2

dist/index.d.ts
export { calcMean, calcWeightedMean, calcMedian, calcWeightedMedian } from './modules/averages';
export { calcDistribution, calcHistogram, calcQuartiles } from './modules/distributions';
export { calcDistribution, calcDistributionAsArray, calcHistogram, calcQuartiles, getMinMaxFromBucket } from './modules/distributions';
export { calcDomain, calcMax, calcMin } from './modules/domain';
export { calcSum } from './modules/operations';
export { ruleOfThree, calcPercent } from './modules/percentages';

@@ -173,2 +173,40 @@ 'use strict';

/**
* Gets the sum of the values in an array
*
* @export
* @param {any[]} array Input array
* @param {string} [property] Property to map by
* @return {number} The sum
*/
function calcSum(array, property) {
return d3Array.sum(getSimpleArray(array, property));
}
/**
* Performs a simple rule of three
*
* @export
* @param {number} ifThis First param
* @param {number} isThis First result
* @param {number} thenThat Second param
* @return {number} Second result
*/
function ruleOfThree(ifThis, isThis, thenThat) {
return isThis * thenThat / ifThis;
}
/**
* Calculates the percentage of a value, given a total
*
* @export
* @param {number} toCalc Number to get percentage of
* @param {number} total Total
* @return {number} Percentage of the total
*/
function calcPercent(toCalc, total) {
return ruleOfThree(total, 100, toCalc);
}
function isNullOrUndefined(element) {

@@ -203,2 +241,45 @@ return element === null || element === undefined;

/**
* Gets the min and max values for a calcDistribution bucket
*
* @export
* @param {string} bucketLabel The bucket label
* @return {number[]} [min, max]
*/
function getMinMaxFromBucket(bucketLabel) {
var _bucketLabel$split = bucketLabel.split(' - '),
min = _bucketLabel$split[0],
max = _bucketLabel$split[1];
return [Number(min.trim()), Number(max.trim())];
}
/**
* Calculates the distribution of an arrays values and outputs an array
*
* @export
* @param {number[]} array Array to calc distribution of
* @param {number} [numOfBins] Number of bins to use
* @return {IDistributionArrayItem[]} The distribution as an array of objects
*/
function calcDistributionAsArray(array, numOfBins) {
var distribution = calcDistribution(array, numOfBins);
var total = calcSum(distribution.data);
return distribution.labels.map(function (label, index) {
var indexValue = distribution.data[index];
var _getMinMaxFromBucket = getMinMaxFromBucket(label),
from = _getMinMaxFromBucket[0],
to = _getMinMaxFromBucket[1];
return {
label: label,
count: indexValue,
percentage: calcPercent(indexValue, total),
from: from,
to: to
};
});
}
/**
* Gets the quartiles of an array

@@ -253,41 +334,4 @@ *

/**
* Gets the sum of the values in an array
*
* @export
* @param {any[]} array Input array
* @param {string} [property] Property to map by
* @return {number} The sum
*/
function calcSum(array, property) {
return d3Array.sum(getSimpleArray(array, property));
}
/**
* Performs a simple rule of three
*
* @export
* @param {number} ifThis First param
* @param {number} isThis First result
* @param {number} thenThat Second param
* @return {number} Second result
*/
function ruleOfThree(ifThis, isThis, thenThat) {
return isThis * thenThat / ifThis;
}
/**
* Calculates the percentage of a value, given a total
*
* @export
* @param {number} toCalc Number to get percentage of
* @param {number} total Total
* @return {number} Percentage of the total
*/
function calcPercent(toCalc, total) {
return ruleOfThree(total, 100, toCalc);
}
exports.calcDistribution = calcDistribution;
exports.calcDistributionAsArray = calcDistributionAsArray;
exports.calcDomain = calcDomain;

@@ -304,3 +348,4 @@ exports.calcHistogram = calcHistogram;

exports.calcWeightedMedian = calcWeightedMedian;
exports.getMinMaxFromBucket = getMinMaxFromBucket;
exports.ruleOfThree = ruleOfThree;
//# sourceMappingURL=math-helper-functions.cjs.development.js.map

@@ -1,2 +0,2 @@

"use strict";function r(r){return r&&"object"==typeof r&&"default"in r?r.default:r}Object.defineProperty(exports,"__esModule",{value:!0});var e=require("d3-array"),t=r(require("lodash/get")),n=r(require("lodash/isEmpty"));function u(r,e){return!1===n(e)?r.map((function(r){return t(r,e)})):r}var a=function(r,e){this.value=r,this.weight=e};function o(r,t){return e.extent(u(r,t))}function i(r,e,t){return e*t/r}exports.calcDistribution=function(r,t){return(null==t?e.histogram():e.histogram().thresholds(t))(r).reduce((function(r,e){var t=e.length;return r.labels.push(e.x0+" - "+e.x1),r.data.push(t),r}),{labels:[],data:[]})},exports.calcDomain=o,exports.calcHistogram=function(r,e,t){void 0===e&&(e=4);for(var n=u(r,t),a=o(n),i=a[0],c=(a[1]-i)/e,l=n.length,s=new Array(e).fill(0),f=0;f<l;f++)s[Math.min(Math.floor((n[f]-i)/c),e-1)]+=1;return s},exports.calcMax=function(r,t){return e.max(u(r,t))},exports.calcMean=function(r,t){return e.mean(u(r,t))},exports.calcMedian=function(r,t){return e.median(u(r,t))},exports.calcMin=function(r,t){return e.min(u(r,t))},exports.calcPercent=function(r,e){return i(e,100,r)},exports.calcQuartiles=function(r,e){var t=r.length,n=[].concat(u(r,e));return n.sort((function(r,e){return r-e})),[n[Math.round(t/4)-1],n[Math.round(t/2)-1],n[Math.round(3*t/4)-1]]},exports.calcSum=function(r,t){return e.sum(u(r,t))},exports.calcWeightedMean=function(r,e,n){if(!e||!n)throw new Error("Both valueProperty and weightProperty params are required");var u=r.map((function(r){var u=t(r,n,0);return[t(r,e,0)*u,u]})).reduce((function(r,e){return[r[0]+e[0],r[1]+e[1]]}),[0,0]);return u[0]/u[1]},exports.calcWeightedMedian=function(r,e,u){if(!e||!u)throw new Error("Both valueProperty and weightProperty params are required");if(n(r))return 0;for(var o=r.reduce((function(r,t){return r.array.push(new a(t[e],t[u])),r.weightSum+=t[u],r}),{array:[],weightSum:0}),i=o.weightSum,c=o.array.sort((function(r,e){return r.value-e.value})),l=i/2,s=0,f=0;f<l;)f+=c[s].weight,s++;return f===l?(t(c[s-1],"value",0)+t(c[s],"value",0))/2:t(c[s-1],"value",0)},exports.ruleOfThree=i;
"use strict";function r(r){return r&&"object"==typeof r&&"default"in r?r.default:r}Object.defineProperty(exports,"__esModule",{value:!0});var t=require("d3-array"),e=r(require("lodash/get")),n=r(require("lodash/isEmpty"));function u(r,t){return!1===n(t)?r.map((function(r){return e(r,t)})):r}var a=function(r,t){this.value=r,this.weight=t};function o(r,e){return t.extent(u(r,e))}function i(r,e){return t.sum(u(r,e))}function c(r,t,e){return t*e/r}function l(r,t){return c(t,100,r)}function s(r,e){return(null==e?t.histogram():t.histogram().thresholds(e))(r).reduce((function(r,t){var e=t.length;return r.labels.push(t.x0+" - "+t.x1),r.data.push(e),r}),{labels:[],data:[]})}function f(r){var t=r.split(" - "),e=t[1];return[Number(t[0].trim()),Number(e.trim())]}exports.calcDistribution=s,exports.calcDistributionAsArray=function(r,t){var e=s(r,t),n=i(e.data);return e.labels.map((function(r,t){var u=e.data[t],a=f(r),o=a[0],i=a[1];return{label:r,count:u,percentage:l(u,n),from:o,to:i}}))},exports.calcDomain=o,exports.calcHistogram=function(r,t,e){void 0===t&&(t=4);for(var n=u(r,e),a=o(n),i=a[0],c=(a[1]-i)/t,l=n.length,s=new Array(t).fill(0),f=0;f<l;f++)s[Math.min(Math.floor((n[f]-i)/c),t-1)]+=1;return s},exports.calcMax=function(r,e){return t.max(u(r,e))},exports.calcMean=function(r,e){return t.mean(u(r,e))},exports.calcMedian=function(r,e){return t.median(u(r,e))},exports.calcMin=function(r,e){return t.min(u(r,e))},exports.calcPercent=l,exports.calcQuartiles=function(r,t){var e=r.length,n=[].concat(u(r,t));return n.sort((function(r,t){return r-t})),[n[Math.round(e/4)-1],n[Math.round(e/2)-1],n[Math.round(3*e/4)-1]]},exports.calcSum=i,exports.calcWeightedMean=function(r,t,n){if(!t||!n)throw new Error("Both valueProperty and weightProperty params are required");var u=r.map((function(r){var u=e(r,n,0);return[e(r,t,0)*u,u]})).reduce((function(r,t){return[r[0]+t[0],r[1]+t[1]]}),[0,0]);return u[0]/u[1]},exports.calcWeightedMedian=function(r,t,u){if(!t||!u)throw new Error("Both valueProperty and weightProperty params are required");if(n(r))return 0;for(var o=r.reduce((function(r,e){return r.array.push(new a(e[t],e[u])),r.weightSum+=e[u],r}),{array:[],weightSum:0}),i=o.weightSum,c=o.array.sort((function(r,t){return r.value-t.value})),l=i/2,s=0,f=0;f<l;)f+=c[s].weight,s++;return f===l?(e(c[s-1],"value",0)+e(c[s],"value",0))/2:e(c[s-1],"value",0)},exports.getMinMaxFromBucket=f,exports.ruleOfThree=c;
//# sourceMappingURL=math-helper-functions.cjs.production.min.js.map

@@ -1,2 +0,2 @@

import { median, mean, max, min, extent, histogram, sum } from 'd3-array';
import { median, mean, max, min, extent, sum, histogram } from 'd3-array';
import get from 'lodash/get';

@@ -167,2 +167,40 @@ import isEmpty from 'lodash/isEmpty';

/**
* Gets the sum of the values in an array
*
* @export
* @param {any[]} array Input array
* @param {string} [property] Property to map by
* @return {number} The sum
*/
function calcSum(array, property) {
return sum(getSimpleArray(array, property));
}
/**
* Performs a simple rule of three
*
* @export
* @param {number} ifThis First param
* @param {number} isThis First result
* @param {number} thenThat Second param
* @return {number} Second result
*/
function ruleOfThree(ifThis, isThis, thenThat) {
return isThis * thenThat / ifThis;
}
/**
* Calculates the percentage of a value, given a total
*
* @export
* @param {number} toCalc Number to get percentage of
* @param {number} total Total
* @return {number} Percentage of the total
*/
function calcPercent(toCalc, total) {
return ruleOfThree(total, 100, toCalc);
}
function isNullOrUndefined(element) {

@@ -197,2 +235,45 @@ return element === null || element === undefined;

/**
* Gets the min and max values for a calcDistribution bucket
*
* @export
* @param {string} bucketLabel The bucket label
* @return {number[]} [min, max]
*/
function getMinMaxFromBucket(bucketLabel) {
var _bucketLabel$split = bucketLabel.split(' - '),
min = _bucketLabel$split[0],
max = _bucketLabel$split[1];
return [Number(min.trim()), Number(max.trim())];
}
/**
* Calculates the distribution of an arrays values and outputs an array
*
* @export
* @param {number[]} array Array to calc distribution of
* @param {number} [numOfBins] Number of bins to use
* @return {IDistributionArrayItem[]} The distribution as an array of objects
*/
function calcDistributionAsArray(array, numOfBins) {
var distribution = calcDistribution(array, numOfBins);
var total = calcSum(distribution.data);
return distribution.labels.map(function (label, index) {
var indexValue = distribution.data[index];
var _getMinMaxFromBucket = getMinMaxFromBucket(label),
from = _getMinMaxFromBucket[0],
to = _getMinMaxFromBucket[1];
return {
label: label,
count: indexValue,
percentage: calcPercent(indexValue, total),
from: from,
to: to
};
});
}
/**
* Gets the quartiles of an array

@@ -247,41 +328,3 @@ *

/**
* Gets the sum of the values in an array
*
* @export
* @param {any[]} array Input array
* @param {string} [property] Property to map by
* @return {number} The sum
*/
function calcSum(array, property) {
return sum(getSimpleArray(array, property));
}
/**
* Performs a simple rule of three
*
* @export
* @param {number} ifThis First param
* @param {number} isThis First result
* @param {number} thenThat Second param
* @return {number} Second result
*/
function ruleOfThree(ifThis, isThis, thenThat) {
return isThis * thenThat / ifThis;
}
/**
* Calculates the percentage of a value, given a total
*
* @export
* @param {number} toCalc Number to get percentage of
* @param {number} total Total
* @return {number} Percentage of the total
*/
function calcPercent(toCalc, total) {
return ruleOfThree(total, 100, toCalc);
}
export { calcDistribution, calcDomain, calcHistogram, calcMax, calcMean, calcMedian, calcMin, calcPercent, calcQuartiles, calcSum, calcWeightedMean, calcWeightedMedian, ruleOfThree };
export { calcDistribution, calcDistributionAsArray, calcDomain, calcHistogram, calcMax, calcMean, calcMedian, calcMin, calcPercent, calcQuartiles, calcSum, calcWeightedMean, calcWeightedMedian, getMinMaxFromBucket, ruleOfThree };
//# sourceMappingURL=math-helper-functions.esm.js.map

@@ -5,2 +5,9 @@ interface IDistribution {

}
interface IDistributionArrayItem {
label: string;
count: number;
percentage: number;
from: number;
to: number;
}
/**

@@ -16,2 +23,19 @@ * Calculates the distribution of an arrays values

/**
* Gets the min and max values for a calcDistribution bucket
*
* @export
* @param {string} bucketLabel The bucket label
* @return {number[]} [min, max]
*/
export declare function getMinMaxFromBucket(bucketLabel: string): number[];
/**
* Calculates the distribution of an arrays values and outputs an array
*
* @export
* @param {number[]} array Array to calc distribution of
* @param {number} [numOfBins] Number of bins to use
* @return {IDistributionArrayItem[]} The distribution as an array of objects
*/
export declare function calcDistributionAsArray(array: number[], numOfBins?: number): IDistributionArrayItem[];
/**
* Gets the quartiles of an array

@@ -18,0 +42,0 @@ *

{
"name": "math-helper-functions",
"version": "2.0.3",
"version": "2.1.0",
"description": "Helper with misc. math functions such as sums, averages, max, min, etc",

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

@@ -74,3 +74,3 @@ # math-helper-functions

Defined in: [modules/distributions.ts:22](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/distributions.ts#L22)
Defined in: [modules/distributions.ts:32](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/distributions.ts#L32)

@@ -98,3 +98,3 @@ ---

Defined in: [modules/domain.ts:36](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/domain.ts#L36)
Defined in: [modules/domain.ts:36](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/domain.ts#L36)

@@ -123,3 +123,3 @@ ---

Defined in: [modules/distributions.ts:68](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/distributions.ts#L68)
Defined in: [modules/distributions.ts:118](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/distributions.ts#L118)

@@ -147,3 +147,3 @@ ---

Defined in: [modules/domain.ts:12](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/domain.ts#L12)
Defined in: [modules/domain.ts:12](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/domain.ts#L12)

@@ -171,3 +171,3 @@ ---

Defined in: [modules/averages.ts:93](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/averages.ts#L93)
Defined in: [modules/averages.ts:94](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/averages.ts#L94)

@@ -195,3 +195,3 @@ ---

Defined in: [modules/averages.ts:13](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/averages.ts#L13)
Defined in: [modules/averages.ts:14](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/averages.ts#L14)

@@ -219,3 +219,3 @@ ---

Defined in: [modules/domain.ts:24](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/domain.ts#L24)
Defined in: [modules/domain.ts:24](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/domain.ts#L24)

@@ -243,3 +243,3 @@ ---

Defined in: [modules/percentages.ts:22](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/percentages.ts#L22)
Defined in: [modules/percentages.ts:22](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/percentages.ts#L22)

@@ -267,3 +267,3 @@ ---

Defined in: [modules/distributions.ts:47](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/distributions.ts#L47)
Defined in: [modules/distributions.ts:97](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/distributions.ts#L97)

@@ -291,3 +291,3 @@ ---

Defined in: [modules/operations.ts:12](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/operations.ts#L12)
Defined in: [modules/operations.ts:12](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/operations.ts#L12)

@@ -316,3 +316,3 @@ ---

Defined in: [modules/averages.ts:106](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/averages.ts#L106)
Defined in: [modules/averages.ts:107](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/averages.ts#L107)

@@ -341,3 +341,3 @@ ---

Defined in: [modules/averages.ts:41](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/averages.ts#L41)
Defined in: [modules/averages.ts:42](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/averages.ts#L42)

@@ -366,2 +366,2 @@ ---

Defined in: [modules/percentages.ts:10](https://github.com/alrico88/math-helper-functions/blob/44aaebf/src/modules/percentages.ts#L10)
Defined in: [modules/percentages.ts:10](https://github.com/alrico88/math-helper-functions/blob/7ccd58b/src/modules/percentages.ts#L10)
export {calcMean, calcWeightedMean, calcMedian, calcWeightedMedian} from './modules/averages';
export {calcDistribution, calcHistogram, calcQuartiles} from './modules/distributions';
export {calcDistribution, calcDistributionAsArray, calcHistogram, calcQuartiles, getMinMaxFromBucket} from './modules/distributions';
export {calcDomain, calcMax, calcMin} from './modules/domain';
export {calcSum} from './modules/operations';
export {ruleOfThree, calcPercent} from './modules/percentages';
import {histogram} from 'd3-array';
import {getSimpleArray} from './arrays';
import {calcDomain} from './domain';
import {calcSum} from './operations';
import {calcPercent} from './percentages';

@@ -10,2 +12,10 @@ interface IDistribution {

interface IDistributionArrayItem {
label: string;
count: number;
percentage: number;
from: number;
to: number;
}
function isNullOrUndefined(element: any): boolean {

@@ -41,2 +51,42 @@ return element === null || element === undefined;

/**
* Gets the min and max values for a calcDistribution bucket
*
* @export
* @param {string} bucketLabel The bucket label
* @return {number[]} [min, max]
*/
export function getMinMaxFromBucket(bucketLabel: string): number[] {
const [min, max] = bucketLabel.split(' - ');
return [Number(min.trim()), Number(max.trim())];
}
/**
* Calculates the distribution of an arrays values and outputs an array
*
* @export
* @param {number[]} array Array to calc distribution of
* @param {number} [numOfBins] Number of bins to use
* @return {IDistributionArrayItem[]} The distribution as an array of objects
*/
export function calcDistributionAsArray(array: number[], numOfBins?: number): IDistributionArrayItem[] {
const distribution = calcDistribution(array, numOfBins);
const total = calcSum(distribution.data);
return distribution.labels.map((label, index) => {
const indexValue = distribution.data[index];
const [from, to] = getMinMaxFromBucket(label);
return {
label,
count: indexValue,
percentage: calcPercent(indexValue, total),
from,
to,
};
});
}
/**
* Gets the quartiles of an array

@@ -43,0 +93,0 @@ *

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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