math-helper-functions
Installation
Using npm, npm i math-helper-functions
.
Using yarn, yarn add math-helper-functions
.
Usage
Using import
import { calcSum } from 'math-helper-functions';
const input = [{ item: 'bookA', count: 3 }, { item: 'bookB', count: 4 }];
const totalBooks = calcSum(input, 'count');
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');
Table of contents
Functions
Functions
calcDistribution
▸ calcDistribution(array
: any[], numOfBins?
: number): IDistribution
Calculates the distribution of an arrays values
export
Parameters:
Name | Type | Description |
---|
array | any[] | Input array |
numOfBins? | number | - |
Returns: IDistribution
The distribution
calcDomain
▸ calcDomain(array
: any[], property?
: string): [number, number] | [any, any]
Gets the [min, max] value in an array
export
Parameters:
Name | Type | Description |
---|
array | any[] | Input array |
property? | string | - |
Returns: [number, number] | [any, any]
The domain
calcDiff
▸ calcDiff(array
: any[], property?
: string): number
Gets the absolute difference between the max and min value in an array
export
Parameters:
Name | Type | Description |
---|
array | any[] | Input array |
property? | string | - |
Returns: number
Absolute difference between the max and min of an array
calcHistogram
▸ calcHistogram(array
: any[], numberOfBins?
: number, property?
: string): number[]
Calculates a histogram from array values
export
Parameters:
Name | Type | Default value | Description |
---|
array | any[] | - | Input array |
numberOfBins | number | 4 | - |
property? | string | - | - |
Returns: number[]
The histogram
calcMax
▸ calcMax(array
: any[], property?
: string): number
Gets the max value in an array
export
Parameters:
Name | Type | Description |
---|
array | any[] | Input array |
property? | string | - |
Returns: number
The maximum value in the array
calcMean
▸ calcMean(array
: any[], property?
: string): number | undefined
Gets the mean value for an array
export
Parameters:
Name | Type | Description |
---|
array | any[] | Input array |
property? | string | - |
Returns: number | undefined
The mean value
calcMedian
▸ calcMedian(array
: any[], property?
: string): number | undefined
Gets an array median
export
Parameters:
Name | Type | Description |
---|
array | any[] | Input array |
property? | string | - |
Returns: number | undefined
The resulting median
calcMin
▸ calcMin(array
: any[], property?
: string): number
Gets the min value in an array
export
Parameters:
Name | Type | Description |
---|
array | any[] | Input array |
property? | string | - |
Returns: number
The minimum value in the array
calcPercent
▸ calcPercent(toCalc
: number, total
: number): number
Calculates the percentage of a value, given a total
export
Parameters:
Name | Type | Description |
---|
toCalc | number | Number to get percentage of |
total | number | Total |
Returns: number
Percentage of the total
calcQuartiles
▸ calcQuartiles(array
: any[], property
: string): [number, number, number]
Gets the quartiles of an array
export
Parameters:
Name | Type | Description |
---|
array | any[] | Input array |
property | string | Property to map by |
Returns: [number, number, number]
The quartiles
calcSum
▸ calcSum(array
: any[], property?
: string): number
Gets the sum of the values in an array
export
Parameters:
Name | Type | Description |
---|
array | any[] | Input array |
property? | string | - |
Returns: number
The sum
calcWeightedMean
▸ calcWeightedMean(array
: any[], valueProperty
: string, weightProperty
: string): number
Gets the weighted mean for an array
export
Parameters:
Name | Type | Description |
---|
array | any[] | Input array |
valueProperty | string | Property to use for value |
weightProperty | string | Property to use for weight |
Returns: number
The weighted mean
calcWeightedMedian
▸ calcWeightedMedian(array
: any[], valueProperty
: string, weightProperty
: string): number
Gets an array weighted median
export
Parameters:
Name | Type | Description |
---|
array | any[] | Input array |
valueProperty | string | The property to use as value |
weightProperty | string | The property to use as weight |
Returns: number
The resulting median
ruleOfThree
▸ ruleOfThree(ifThis
: number, isThis
: number, thenThat
: number): number
Performs a simple rule of three
export
Parameters:
Name | Type | Description |
---|
ifThis | number | First param |
isThis | number | First result |
thenThat | number | Second param |
Returns: number
Second result