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.1.0 to 2.2.0

2

dist/index.d.ts

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

export { calcMean, calcWeightedMean, calcMedian, calcWeightedMedian } from './modules/averages';
export { calcMean, calcWeightedMean, calcMedian, calcWeightedMedian, calcStdDeviation, calcVariance } from './modules/averages';
export { calcDistribution, calcDistributionAsArray, calcHistogram, calcQuartiles, getMinMaxFromBucket } from './modules/distributions';

@@ -3,0 +3,0 @@ export { calcDomain, calcMax, calcMin } from './modules/domain';

@@ -15,3 +15,4 @@ 'use strict';

* @export
* @param {any[]} array Input array
* @template T
* @param {T[]} array Input array
* @param {string} [property] The property to map by

@@ -22,3 +23,3 @@ * @return {any[]} The resulting array

function getSimpleArray(array, property) {
if (isEmpty(property) === false) {
if (!isEmpty(property)) {
return array.map(function (d) {

@@ -137,4 +138,30 @@ return get(d, property);

}
/**
* Calculates the variance in an array of numbers
*
* @export
* @template T
* @param {T[]} array
* @param {string} [property]
* @return {(number | undefined)}
*/
function calcVariance(array, property) {
return d3Array.variance(getSimpleArray(array, property));
}
/**
* Calculates the standard deviation in an array of numbers
*
* @export
* @template T
* @param {T[]} array
* @param {string} [property]
* @return {(number | undefined)}
*/
function calcStdDeviation(array, property) {
return d3Array.deviation(getSimpleArray(array, property));
}
/**
* Gets the max value in an array

@@ -345,3 +372,5 @@ *

exports.calcQuartiles = calcQuartiles;
exports.calcStdDeviation = calcStdDeviation;
exports.calcSum = calcSum;
exports.calcVariance = calcVariance;
exports.calcWeightedMean = calcWeightedMean;

@@ -348,0 +377,0 @@ exports.calcWeightedMedian = calcWeightedMedian;

@@ -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 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;
"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 a(r,t){return n(t)?r:r.map((function(r){return e(r,t)}))}var u=function(r,t){this.value=r,this.weight=t};function o(r,e){return t.extent(a(r,e))}function i(r,e){return t.sum(a(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 a=e.data[t],u=f(r),o=u[0],i=u[1];return{label:r,count:a,percentage:l(a,n),from:o,to:i}}))},exports.calcDomain=o,exports.calcHistogram=function(r,t,e){void 0===t&&(t=4);for(var n=a(r,e),u=o(n),i=u[0],c=(u[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(a(r,e))},exports.calcMean=function(r,e){return t.mean(a(r,e))},exports.calcMedian=function(r,e){return t.median(a(r,e))},exports.calcMin=function(r,e){return t.min(a(r,e))},exports.calcPercent=l,exports.calcQuartiles=function(r,t){var e=r.length,n=[].concat(a(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.calcStdDeviation=function(r,e){return t.deviation(a(r,e))},exports.calcSum=i,exports.calcVariance=function(r,e){return t.variance(a(r,e))},exports.calcWeightedMean=function(r,t,n){if(!t||!n)throw new Error("Both valueProperty and weightProperty params are required");var a=r.map((function(r){var a=e(r,n,0);return[e(r,t,0)*a,a]})).reduce((function(r,t){return[r[0]+t[0],r[1]+t[1]]}),[0,0]);return a[0]/a[1]},exports.calcWeightedMedian=function(r,t,a){if(!t||!a)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 u(e[t],e[a])),r.weightSum+=e[a],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, sum, histogram } from 'd3-array';
import { median, mean, variance, deviation, max, min, extent, sum, histogram } from 'd3-array';
import get from 'lodash/get';

@@ -9,3 +9,4 @@ import isEmpty from 'lodash/isEmpty';

* @export
* @param {any[]} array Input array
* @template T
* @param {T[]} array Input array
* @param {string} [property] The property to map by

@@ -16,3 +17,3 @@ * @return {any[]} The resulting array

function getSimpleArray(array, property) {
if (isEmpty(property) === false) {
if (!isEmpty(property)) {
return array.map(function (d) {

@@ -131,4 +132,30 @@ return get(d, property);

}
/**
* Calculates the variance in an array of numbers
*
* @export
* @template T
* @param {T[]} array
* @param {string} [property]
* @return {(number | undefined)}
*/
function calcVariance(array, property) {
return variance(getSimpleArray(array, property));
}
/**
* Calculates the standard deviation in an array of numbers
*
* @export
* @template T
* @param {T[]} array
* @param {string} [property]
* @return {(number | undefined)}
*/
function calcStdDeviation(array, property) {
return deviation(getSimpleArray(array, property));
}
/**
* Gets the max value in an array

@@ -329,3 +356,3 @@ *

export { calcDistribution, calcDistributionAsArray, calcDomain, calcHistogram, calcMax, calcMean, calcMedian, calcMin, calcPercent, calcQuartiles, calcSum, calcWeightedMean, calcWeightedMedian, getMinMaxFromBucket, ruleOfThree };
export { calcDistribution, calcDistributionAsArray, calcDomain, calcHistogram, calcMax, calcMean, calcMedian, calcMin, calcPercent, calcQuartiles, calcStdDeviation, calcSum, calcVariance, calcWeightedMean, calcWeightedMedian, getMinMaxFromBucket, ruleOfThree };
//# sourceMappingURL=math-helper-functions.esm.js.map

@@ -5,6 +5,7 @@ /**

* @export
* @param {any[]} array Input array
* @template T
* @param {T[]} array Input array
* @param {string} [property] The property to map by
* @return {any[]} The resulting array
*/
export declare function getSimpleArray(array: any[], property?: string): any[];
export declare function getSimpleArray<T>(array: T[], property?: string): any[];

@@ -39,1 +39,21 @@ /**

export declare function calcWeightedMean(array: any[], valueProperty: string, weightProperty: string): number;
/**
* Calculates the variance in an array of numbers
*
* @export
* @template T
* @param {T[]} array
* @param {string} [property]
* @return {(number | undefined)}
*/
export declare function calcVariance<T>(array: T[], property?: string): number | undefined;
/**
* Calculates the standard deviation in an array of numbers
*
* @export
* @template T
* @param {T[]} array
* @param {string} [property]
* @return {(number | undefined)}
*/
export declare function calcStdDeviation<T>(array: T[], property?: string): number | undefined;
{
"name": "math-helper-functions",
"version": "2.1.0",
"version": "2.2.0",
"description": "Helper with misc. math functions such as sums, averages, max, min, etc",

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

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

export {calcMean, calcWeightedMean, calcMedian, calcWeightedMedian} from './modules/averages';
export {calcMean, calcWeightedMean, calcMedian, calcWeightedMedian, calcStdDeviation, calcVariance} from './modules/averages';
export {calcDistribution, calcDistributionAsArray, calcHistogram, calcQuartiles, getMinMaxFromBucket} from './modules/distributions';

@@ -3,0 +3,0 @@ export {calcDomain, calcMax, calcMin} from './modules/domain';

@@ -8,8 +8,9 @@ import get from 'lodash/get';

* @export
* @param {any[]} array Input array
* @template T
* @param {T[]} array Input array
* @param {string} [property] The property to map by
* @return {any[]} The resulting array
*/
export function getSimpleArray(array: any[], property?: string): any[] {
if (isEmpty(property) === false) {
export function getSimpleArray<T>(array: T[], property?: string): any[] {
if (!isEmpty(property)) {
return array.map((d) => get(d, property as string));

@@ -16,0 +17,0 @@ } else {

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

import {mean, median} from 'd3-array';
import {deviation, mean, median, variance} from 'd3-array';
import {getSimpleArray} from './arrays';

@@ -122,1 +122,27 @@ import get from 'lodash/get';

}
/**
* Calculates the variance in an array of numbers
*
* @export
* @template T
* @param {T[]} array
* @param {string} [property]
* @return {(number | undefined)}
*/
export function calcVariance<T>(array: T[], property?: string): number | undefined {
return variance(getSimpleArray(array, property));
}
/**
* Calculates the standard deviation in an array of numbers
*
* @export
* @template T
* @param {T[]} array
* @param {string} [property]
* @return {(number | undefined)}
*/
export function calcStdDeviation<T>(array: T[], property?: string): number | undefined {
return deviation(getSimpleArray(array, property));
}

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