Using npm, npm i math-helper-functions
.
Using yarn, yarn add math-helper-functions
.
Using import
import { calcSum } from 'math-helper-functions';
const input = [
{ item: 'bookA', count: 3 },
{ item: 'bookB', count: 4 },
];
const totalBooks = calcSum(input, 'count'); // totalBooks is 7
In a CommonJS environment
const { calcDomain } = require('math-helper-functions');
const input = [
{ item: 'bookA', count: 3 },
{ item: 'bookB', count: 10 },
{ item: 'bookC', count: 1 },
];
const domain = calcDomain(input, 'count'); // domain is [1, 10]
- calcBuckets
- calcDiff
- calcDistribution
- calcDistributionAsArray
- calcDistributionWithSeries
- calcDomain
- calcHistogram
- calcMax
- calcMean
- calcMedian
- calcMin
- calcPercent
- calcQuartiles
- calcStdDeviation
- calcSum
- calcVariance
- calcWeightedMean
- calcWeightedMedian
- getMinMaxFromBucket
- getPercentile
- ruleOfThree
▸ calcBuckets(array
, strict?
, numOfBins?
): IBucket
[]
Calculate the buckets given a data array and an amount
Name | Type | Default value | Description |
---|---|---|---|
array |
number [] |
undefined |
The data array |
strict? |
boolean |
false |
Whether to use real or pretty domain |
numOfBins? |
number |
undefined |
Amount of desired buckets |
IBucket
[]
The buckets
Export
▸ calcDiff(array
, property?
): number
Gets the absolute difference between the max and min value in an array
Name | Type | Description |
---|---|---|
array |
any [] |
Input array |
property? |
string |
Property to map by |
number
Absolute difference between the max and min of an array
Export
▸ calcDistribution(array
, strict?
, numOfBins?
): IDistribution
Calculates the distribution of an arrays values
Name | Type | Default value | Description |
---|---|---|---|
array |
number [] |
undefined |
Input array |
strict |
boolean |
false |
|
numOfBins? |
number |
undefined |
Number of bins to use |
IDistribution
The distribution
Export
▸ calcDistributionAsArray(array
, binsStrict?
, numOfBins?
): IDistributionArrayItem
[]
Calculates the distribution of an arrays values and outputs an array
Name | Type | Default value | Description |
---|---|---|---|
array |
number [] |
undefined |
Array to calc distribution of |
binsStrict? |
boolean |
false |
If false, buckets may be rounded [floor, ceil] |
numOfBins? |
number |
undefined |
Number of bins to use |
IDistributionArrayItem
[]
The distribution as an array of objects
Export
▸ calcDistributionWithSeries(buckets
, dataGrouped
, distributionProp
): ISerieDistribution
Calculates the distribution of an array of grouped objects
Name | Type |
---|---|
buckets |
IBucket [] |
dataGrouped |
Record <string , unknown []> |
distributionProp |
string |
ISerieDistribution
The distribution with labels and data
Export
▸ calcDomain(array
, property?
): [number
, number
] | [any
, any
]
Gets the [min, max] value in an array
Name | Type | Description |
---|---|---|
array |
any [] |
Input array |
property? |
string |
Property to map by |
[number
, number
] | [any
, any
]
The domain
Export
▸ calcHistogram(array
, numberOfBins?
, property?
): number
[]
Calculates a histogram from array values
Name | Type | Default value | Description |
---|---|---|---|
array |
any [] |
undefined |
Input array |
numberOfBins? |
number |
4 |
Number of bins to use |
property? |
string |
undefined |
Property to map by |
number
[]
The histogram
Export
▸ calcMax(array
, property?
): number
Gets the max value in an array
Name | Type | Description |
---|---|---|
array |
any [] |
Input array |
property? |
string |
Property to map by |
number
The maximum value in the array
Export
▸ calcMean(array
, property?
): number
| undefined
Gets the mean value for an array
Name | Type | Description |
---|---|---|
array |
any [] |
Input array |
property? |
string |
Property to map by |
number
| undefined
The mean value
Export
▸ calcMedian(array
, property?
): number
| undefined
Gets an array median
Name | Type | Description |
---|---|---|
array |
any [] |
Input array |
property? |
string |
The property to map by |
number
| undefined
The resulting median
Export
▸ calcMin(array
, property?
): number
Gets the min value in an array
Name | Type | Description |
---|---|---|
array |
any [] |
Input array |
property? |
string |
Property to map |
number
The minimum value in the array
Export
▸ calcPercent(toCalc
, total
): number
Calculates the percentage of a value, given a total
Name | Type | Description |
---|---|---|
toCalc |
number |
Number to get percentage of |
total |
number |
Total |
number
Percentage of the total
Export
▸ calcQuartiles(array
, property?
): [number
, number
, number
]
Gets the quartiles of an array
Name | Type | Description |
---|---|---|
array |
any [] |
Input array |
property? |
string |
Property to map by |
[number
, number
, number
]
The quartiles
Export
▸ calcStdDeviation<T
>(array
, property?
): number
| undefined
Calculates the standard deviation in an array of numbers
Name |
---|
T |
Name | Type |
---|---|
array |
T [] |
property? |
string |
number
| undefined
Export
▸ calcSum(array
, property?
): number
Gets the sum of the values in an array
Name | Type | Description |
---|---|---|
array |
any [] |
Input array |
property? |
string |
Property to map by |
number
The sum
Export
▸ calcVariance<T
>(array
, property?
): number
| undefined
Calculates the variance in an array of numbers
Name |
---|
T |
Name | Type |
---|---|
array |
T [] |
property? |
string |
number
| undefined
Export
▸ calcWeightedMean(array
, valueProperty
, weightProperty
): number
Gets the weighted mean for an array
Name | Type | Description |
---|---|---|
array |
any [] |
Input array |
valueProperty |
string |
Property to use for value |
weightProperty |
string |
Property to use for weight |
number
The weighted mean
Export
▸ calcWeightedMedian(array
, valueProperty
, weightProperty
): number
Gets an array weighted median
Name | Type | Description |
---|---|---|
array |
any [] |
Input array |
valueProperty |
string |
The property to use as value |
weightProperty |
string |
The property to use as weight |
number
The resulting median
Export
▸ getMinMaxFromBucket(bucketLabel
): number
[]
Gets the min and max values for a calcDistribution bucket
Name | Type | Description |
---|---|---|
bucketLabel |
string |
The bucket label |
number
[]
[min, max]
Export
▸ getPercentile(array
, percentile
): number
| undefined
Gets the percentile of an array of values
Name | Type | Description |
---|---|---|
array |
(null | number )[] |
Array to find percentile of |
percentile |
number |
Amount between 0 and 1 |
number
| undefined
Percentile
▸ ruleOfThree(ifThis
, isThis
, thenThat
): number
Performs a simple rule of three
Name | Type | Description |
---|---|---|
ifThis |
number |
First param |
isThis |
number |
First result |
thenThat |
number |
Second param |
number
Second result
Export