npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

math-helper-functions

v3.2.0

Published

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

Downloads

1,084

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

calcBuckets

calcBuckets(array, strict?, numOfBins?): IBucket[]

Calculate the buckets given a data array and an amount

Parameters

| 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 |

Returns

IBucket[]

The buckets

Export

Defined in

modules/distributions.ts:58


calcDiff

calcDiff(array, property?): number

Gets the absolute difference between the max and min value in an array

Parameters

| Name | Type | Description | | :---------- | :------- | :----------------- | | array | any[] | Input array | | property? | string | Property to map by |

Returns

number

Absolute difference between the max and min of an array

Export

Defined in

modules/domain.ts:48


calcDistribution

calcDistribution(array, strict?, numOfBins?): IDistribution

Calculates the distribution of an arrays values

Parameters

| Name | Type | Default value | Description | | :----------- | :--------- | :------------ | :-------------------- | | array | number[] | undefined | Input array | | strict | boolean | false | | | numOfBins? | number | undefined | Number of bins to use |

Returns

IDistribution

The distribution

Export

Defined in

modules/distributions.ts:105


calcDistributionAsArray

calcDistributionAsArray(array, binsStrict?, numOfBins?): IDistributionArrayItem[]

Calculates the distribution of an arrays values and outputs an array

Parameters

| 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 |

Returns

IDistributionArrayItem[]

The distribution as an array of objects

Export

Defined in

modules/distributions.ts:196


calcDistributionWithSeries

calcDistributionWithSeries(buckets, dataGrouped, distributionProp): ISerieDistribution

Calculates the distribution of an array of grouped objects

Parameters

| Name | Type | | :----------------- | :------------------------------- | | buckets | IBucket[] | | dataGrouped | Record<string, unknown[]> | | distributionProp | string |

Returns

ISerieDistribution

The distribution with labels and data

Export

Defined in

modules/distributions.ts:140


calcDomain

calcDomain(array, property?): [number, number] | [any, any]

Gets the [min, max] value in an array

Parameters

| Name | Type | Description | | :---------- | :------- | :----------------- | | array | any[] | Input array | | property? | string | Property to map by |

Returns

[number, number] | [any, any]

The domain

Export

Defined in

modules/domain.ts:36


calcHistogram

calcHistogram(array, numberOfBins?, property?): number[]

Calculates a histogram from array values

Parameters

| 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 |

Returns

number[]

The histogram

Export

Defined in

modules/distributions.ts:251


calcMax

calcMax(array, property?): number

Gets the max value in an array

Parameters

| Name | Type | Description | | :---------- | :------- | :----------------- | | array | any[] | Input array | | property? | string | Property to map by |

Returns

number

The maximum value in the array

Export

Defined in

modules/domain.ts:12


calcMean

calcMean(array, property?): number | undefined

Gets the mean value for an array

Parameters

| Name | Type | Description | | :---------- | :------- | :----------------- | | array | any[] | Input array | | property? | string | Property to map by |

Returns

number | undefined

The mean value

Export

Defined in

modules/averages.ts:100


calcMedian

calcMedian(array, property?): number | undefined

Gets an array median

Parameters

| Name | Type | Description | | :---------- | :------- | :--------------------- | | array | any[] | Input array | | property? | string | The property to map by |

Returns

number | undefined

The resulting median

Export

Defined in

modules/averages.ts:16


calcMin

calcMin(array, property?): number

Gets the min value in an array

Parameters

| Name | Type | Description | | :---------- | :------- | :-------------- | | array | any[] | Input array | | property? | string | Property to map |

Returns

number

The minimum value in the array

Export

Defined in

modules/domain.ts:24


calcPercent

calcPercent(toCalc, total): number

Calculates the percentage of a value, given a total

Parameters

| Name | Type | Description | | :------- | :------- | :-------------------------- | | toCalc | number | Number to get percentage of | | total | number | Total |

Returns

number

Percentage of the total

Export

Defined in

modules/percentages.ts:22


calcQuartiles

calcQuartiles(array, property?): [number, number, number]

Gets the quartiles of an array

Parameters

| Name | Type | Description | | :---------- | :------- | :----------------- | | array | any[] | Input array | | property? | string | Property to map by |

Returns

[number, number, number]

The quartiles

Export

Defined in

modules/distributions.ts:227


calcStdDeviation

calcStdDeviation<T>(array, property?): number | undefined

Calculates the standard deviation in an array of numbers

Type parameters

| Name | | :--- | | T |

Parameters

| Name | Type | | :---------- | :------- | | array | T[] | | property? | string |

Returns

number | undefined

Export

Defined in

modules/averages.ts:155


calcSum

calcSum(array, property?): number

Gets the sum of the values in an array

Parameters

| Name | Type | Description | | :---------- | :------- | :----------------- | | array | any[] | Input array | | property? | string | Property to map by |

Returns

number

The sum

Export

Defined in

modules/operations.ts:12


calcVariance

calcVariance<T>(array, property?): number | undefined

Calculates the variance in an array of numbers

Type parameters

| Name | | :--- | | T |

Parameters

| Name | Type | | :---------- | :------- | | array | T[] | | property? | string |

Returns

number | undefined

Export

Defined in

modules/averages.ts:142


calcWeightedMean

calcWeightedMean(array, valueProperty, weightProperty): number

Gets the weighted mean for an array

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

Export

Defined in

modules/averages.ts:113


calcWeightedMedian

calcWeightedMedian(array, valueProperty, weightProperty): number

Gets an array weighted median

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

Export

Defined in

modules/averages.ts:45


getMinMaxFromBucket

getMinMaxFromBucket(bucketLabel): number[]

Gets the min and max values for a calcDistribution bucket

Parameters

| Name | Type | Description | | :------------ | :------- | :--------------- | | bucketLabel | string | The bucket label |

Returns

number[]

[min, max]

Export

Defined in

modules/distributions.ts:125


getPercentile

getPercentile(array, percentile): number | undefined

Gets the percentile of an array of values

Parameters

| Name | Type | Description | | :----------- | :--------------------- | :-------------------------- | | array | (null | number)[] | Array to find percentile of | | percentile | number | Amount between 0 and 1 |

Returns

number | undefined

Percentile

Defined in

modules/percentiles.ts:11


ruleOfThree

ruleOfThree(ifThis, isThis, thenThat): number

Performs a simple rule of three

Parameters

| Name | Type | Description | | :--------- | :------- | :----------- | | ifThis | number | First param | | isThis | number | First result | | thenThat | number | Second param |

Returns

number

Second result

Export

Defined in

modules/percentages.ts:10