math-helper-functions
Advanced tools
Comparing version 3.0.3 to 3.0.4
@@ -146,2 +146,3 @@ "use strict"; | ||
var import_d3_array4 = require("d3-array"); | ||
var import_number_helper_functions = require("number-helper-functions"); | ||
@@ -182,42 +183,31 @@ // src/modules/domain.ts | ||
buckets.forEach((b, currentIndex) => { | ||
const condition = currentIndex === buckets.length - 1 ? (d) => d >= b.from && d <= b.to : (d) => d >= b.from && d < b.to; | ||
data[currentIndex] = array.filter(condition).length; | ||
data[currentIndex] = array.filter((d) => b.inside(d)).length; | ||
}); | ||
return data; | ||
} | ||
function getMinMaxValuesForBuckets(diffData, index, minDom, strictBuckets, binsNumber) { | ||
const minDiff = diffData * (index - 1); | ||
const maxDiff = diffData * index; | ||
const maxWithoutRound = index === 1 ? minDom + diffData : minDom + maxDiff; | ||
const minWithoutRound = index === 1 ? minDom : minDom + minDiff; | ||
const minVal = strictBuckets ? minWithoutRound : Math.floor(minWithoutRound); | ||
const elseVal = index === binsNumber ? Math.ceil(maxWithoutRound) : Math.floor(maxWithoutRound); | ||
const maxVal = strictBuckets ? maxWithoutRound : elseVal; | ||
return { | ||
minVal, | ||
maxVal | ||
}; | ||
} | ||
function calcDistribution(array, strict = false, numOfBins) { | ||
const [minDom, maxDom] = calcDomain(array); | ||
let [minDom, maxDom] = calcDomain(array); | ||
if (strict === false) { | ||
minDom = Math.floor(minDom); | ||
maxDom = Math.ceil(maxDom); | ||
} | ||
const bins = numOfBins != null ? numOfBins : (0, import_d3_array4.thresholdSturges)(array); | ||
let diffData; | ||
if (Math.abs(maxDom) < Math.abs(minDom)) { | ||
diffData = (Math.abs(minDom) - Math.abs(maxDom)) / bins; | ||
} else { | ||
diffData = (Math.abs(maxDom) - Math.abs(minDom)) / bins; | ||
} | ||
const bucketSize = (maxDom - minDom) / bins; | ||
const buckets = []; | ||
const labels = []; | ||
for (let b = 1; b <= bins; b++) { | ||
const { | ||
minVal, | ||
maxVal | ||
} = getMinMaxValuesForBuckets(diffData, b, minDom, strict, bins); | ||
const label = `${minVal} - ${maxVal}`; | ||
for (let i = 0; i < bins; i++) { | ||
const bucketMin = minDom + i * bucketSize; | ||
const bucketMax = minDom + (i + 1) * bucketSize; | ||
const label = `${(0, import_number_helper_functions.processNumber)(bucketMin)} - ${(0, import_number_helper_functions.processNumber)(bucketMax)}`; | ||
labels.push(label); | ||
buckets.push({ | ||
label, | ||
from: minVal, | ||
to: maxVal | ||
from: bucketMin, | ||
to: bucketMax, | ||
inside(val) { | ||
if (i === bins - 1) { | ||
return val >= bucketMin && val <= bucketMax; | ||
} | ||
return val >= bucketMin && val < bucketMax; | ||
} | ||
}); | ||
@@ -224,0 +214,0 @@ } |
@@ -146,2 +146,3 @@ "use strict"; | ||
var import_d3_array4 = require("d3-array"); | ||
var import_number_helper_functions = require("number-helper-functions"); | ||
@@ -182,42 +183,31 @@ // src/modules/domain.ts | ||
buckets.forEach((b, currentIndex) => { | ||
const condition = currentIndex === buckets.length - 1 ? (d) => d >= b.from && d <= b.to : (d) => d >= b.from && d < b.to; | ||
data[currentIndex] = array.filter(condition).length; | ||
data[currentIndex] = array.filter((d) => b.inside(d)).length; | ||
}); | ||
return data; | ||
} | ||
function getMinMaxValuesForBuckets(diffData, index, minDom, strictBuckets, binsNumber) { | ||
const minDiff = diffData * (index - 1); | ||
const maxDiff = diffData * index; | ||
const maxWithoutRound = index === 1 ? minDom + diffData : minDom + maxDiff; | ||
const minWithoutRound = index === 1 ? minDom : minDom + minDiff; | ||
const minVal = strictBuckets ? minWithoutRound : Math.floor(minWithoutRound); | ||
const elseVal = index === binsNumber ? Math.ceil(maxWithoutRound) : Math.floor(maxWithoutRound); | ||
const maxVal = strictBuckets ? maxWithoutRound : elseVal; | ||
return { | ||
minVal, | ||
maxVal | ||
}; | ||
} | ||
function calcDistribution(array, strict = false, numOfBins) { | ||
const [minDom, maxDom] = calcDomain(array); | ||
let [minDom, maxDom] = calcDomain(array); | ||
if (strict === false) { | ||
minDom = Math.floor(minDom); | ||
maxDom = Math.ceil(maxDom); | ||
} | ||
const bins = numOfBins != null ? numOfBins : (0, import_d3_array4.thresholdSturges)(array); | ||
let diffData; | ||
if (Math.abs(maxDom) < Math.abs(minDom)) { | ||
diffData = (Math.abs(minDom) - Math.abs(maxDom)) / bins; | ||
} else { | ||
diffData = (Math.abs(maxDom) - Math.abs(minDom)) / bins; | ||
} | ||
const bucketSize = (maxDom - minDom) / bins; | ||
const buckets = []; | ||
const labels = []; | ||
for (let b = 1; b <= bins; b++) { | ||
const { | ||
minVal, | ||
maxVal | ||
} = getMinMaxValuesForBuckets(diffData, b, minDom, strict, bins); | ||
const label = `${minVal} - ${maxVal}`; | ||
for (let i = 0; i < bins; i++) { | ||
const bucketMin = minDom + i * bucketSize; | ||
const bucketMax = minDom + (i + 1) * bucketSize; | ||
const label = `${(0, import_number_helper_functions.processNumber)(bucketMin)} - ${(0, import_number_helper_functions.processNumber)(bucketMax)}`; | ||
labels.push(label); | ||
buckets.push({ | ||
label, | ||
from: minVal, | ||
to: maxVal | ||
from: bucketMin, | ||
to: bucketMax, | ||
inside(val) { | ||
if (i === bins - 1) { | ||
return val >= bucketMin && val <= bucketMax; | ||
} | ||
return val >= bucketMin && val < bucketMax; | ||
} | ||
}); | ||
@@ -224,0 +214,0 @@ } |
{ | ||
"name": "math-helper-functions", | ||
"version": "3.0.3", | ||
"version": "3.0.4", | ||
"description": "Helper with misc. math functions such as sums, averages, max, min, etc", | ||
@@ -54,3 +54,4 @@ "main": "./dist/index.js", | ||
"d3-array": "^2.0.3", | ||
"lodash": "^4.17.21" | ||
"lodash": "^4.17.21", | ||
"number-helper-functions": "^4.1.1" | ||
}, | ||
@@ -57,0 +58,0 @@ "prettier": { |
import { thresholdSturges } from 'd3-array'; | ||
import { processNumber } from 'number-helper-functions'; | ||
import { getSimpleArray } from './arrays'; | ||
@@ -24,2 +25,3 @@ import { calcDomain } from './domain'; | ||
to: number; | ||
inside: (val: number) => boolean; | ||
} | ||
@@ -31,7 +33,3 @@ | ||
buckets.forEach((b, currentIndex) => { | ||
const condition = currentIndex === buckets.length - 1 | ||
? (d: number): boolean => d >= b.from && d <= b.to | ||
: (d: number): boolean => d >= b.from && d < b.to; | ||
data[currentIndex] = array.filter(condition as () => boolean).length; | ||
data[currentIndex] = array.filter((d) => b.inside(d)).length; | ||
}); | ||
@@ -42,29 +40,2 @@ | ||
function getMinMaxValuesForBuckets( | ||
diffData: number, | ||
index: number, | ||
minDom: number, | ||
strictBuckets: boolean, | ||
binsNumber: number, | ||
): { | ||
minVal: number; | ||
maxVal: number; | ||
} { | ||
const minDiff = diffData * (index - 1); | ||
const maxDiff = diffData * index; | ||
const maxWithoutRound = index === 1 ? minDom + diffData : minDom + maxDiff; | ||
const minWithoutRound = index === 1 ? minDom : minDom + minDiff; | ||
const minVal = strictBuckets ? minWithoutRound : Math.floor(minWithoutRound); | ||
const elseVal = index === binsNumber ? Math.ceil(maxWithoutRound) : Math.floor(maxWithoutRound); | ||
const maxVal = strictBuckets ? maxWithoutRound : elseVal; | ||
return { | ||
minVal, | ||
maxVal, | ||
}; | ||
} | ||
/** | ||
@@ -84,14 +55,12 @@ * Calculates the distribution of an arrays values | ||
): IDistribution { | ||
const [minDom, maxDom] = calcDomain(array); | ||
let [minDom, maxDom] = calcDomain(array); | ||
if (strict === false) { | ||
minDom = Math.floor(minDom); | ||
maxDom = Math.ceil(maxDom); | ||
} | ||
const bins = numOfBins ?? thresholdSturges(array); | ||
const bucketSize = (maxDom - minDom) / bins; | ||
let diffData; | ||
if (Math.abs(maxDom) < Math.abs(minDom)) { | ||
diffData = (Math.abs(minDom) - Math.abs(maxDom)) / bins; | ||
} else { | ||
diffData = (Math.abs(maxDom) - Math.abs(minDom)) / bins; | ||
} | ||
const buckets: IBucket[] = []; | ||
@@ -101,9 +70,7 @@ | ||
for (let b = 1; b <= bins; b++) { | ||
const { | ||
minVal, | ||
maxVal, | ||
} = getMinMaxValuesForBuckets(diffData, b, minDom, strict, bins); | ||
for (let i = 0; i < bins; i++) { | ||
const bucketMin = minDom + i * bucketSize; | ||
const bucketMax = minDom + (i + 1) * bucketSize; | ||
const label = `${minVal} - ${maxVal}`; | ||
const label = `${processNumber(bucketMin)} - ${processNumber(bucketMax)}`; | ||
@@ -114,4 +81,10 @@ labels.push(label); | ||
label, | ||
from: minVal, | ||
to: maxVal, | ||
from: bucketMin, | ||
to: bucketMax, | ||
inside(val: number) { | ||
if (i === bins - 1) { | ||
return val >= bucketMin && val <= bucketMax; | ||
} | ||
return val >= bucketMin && val < bucketMax; | ||
}, | ||
}); | ||
@@ -179,3 +152,6 @@ } | ||
*/ | ||
export function calcQuartiles(array: any[], property?: string): [number, number, number] { | ||
export function calcQuartiles( | ||
array: any[], | ||
property?: string, | ||
): [number, number, number] { | ||
const len = array.length; | ||
@@ -201,3 +177,7 @@ const simpleArray = [...getSimpleArray(array, property)]; | ||
*/ | ||
export function calcHistogram(array: any[], numberOfBins = 4, property?: string): number[] { | ||
export function calcHistogram( | ||
array: any[], | ||
numberOfBins = 4, | ||
property?: string, | ||
): number[] { | ||
const dataArray = getSimpleArray(array, property); | ||
@@ -204,0 +184,0 @@ const [arrayMin, arrayMax] = calcDomain(dataArray); |
Sorry, the diff of this file is not supported yet
63437
3
1372
+ Addednumber-helper-functions@4.1.1(transitive)