math-helper-functions

Helper with misc. math functions such as sums, averages, max, min, etc

Usage no npm install needed!

<script type="module">
  import mathHelperFunctions from 'https://cdn.skypack.dev/math-helper-functions';
</script>

README

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'); // 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]

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

Defined in: modules/distributions.ts:32


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

Defined in: modules/domain.ts:36


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

Defined in: modules/distributions.ts:118


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

Defined in: modules/domain.ts:12


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

Defined in: modules/averages.ts:94


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

Defined in: modules/averages.ts:14


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

Defined in: modules/domain.ts:24


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

Defined in: modules/percentages.ts:22


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

Defined in: modules/distributions.ts:97


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

Defined in: modules/operations.ts:12


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

Defined in: modules/averages.ts:107


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

Defined in: modules/averages.ts:42


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

Defined in: modules/percentages.ts:10