A collection of useful functions
npm install @basementuniverse/utilsA small library of useful functions
Node:
``javascript`
const utils = require('@basementuniverse/utils');
Browser:
`html`
Typescript:
`typescript`
import * as utils from '@basementuniverse/utils';
functionMemoize a function
booleanCheck if two numbers are approximately equal
numberClamp a number between min and max
numberGet the fractional part of a number
numberRound n to d decimal places
numberDo a linear interpolation between a and b
numberGet the position of i between a and b
numberDo a bilinear interpolation
numberRe-map a number i from range a1...a2 to b1...b2
numberDo a smooth interpolation between a and b
numberGet an angle in radians
numberGet an angle in degrees
numberGet a random float in the interval [min, max)
numberGet a random integer in the interval [min, max]
numberGet a normally-distributed random number
numberGet a normally-distributed random integer in the interval [min, max]
numberReturn a weighted random integer
numberReturn an interpolated value from an array
numberGet the dot product of two vectors
numberGet the factorial of a number
numberGet the number of permutations of r elements from a set of n elements
numberGet the number of combinations of r elements from a set of n elements
Array.<Array.<*>>Generate all permutations of r elements from an array
Array.<Array.<*>>Generate all combinations of r elements from an array
Get a cartesian product of arrays
Array.<*>Return a new array with length n by calling function f(i) on each element
Array.<number>Return an array containing numbers 0->(n - 1)
Array.<Array.<*>>Zip multiple arrays together, i.e. ([1, 2, 3], [a, b, c]) => [[1, a], [2, b], [3, c]]
*Return array[i] with positive and negative wrapping
*Return the last element of an array without removing it
numberReturn the index for a given position in an unrolled 2d array
Array.<number>Return the position for a given index in an unrolled 2d array
Array.<Array.<*>>Chop an array into chunks of size n
Array.<*>Randomly shuffle a shallow copy of an array
objectFlatten an object
objectUnflatten an object
Array.<Array.<*>>Split an array into sub-arrays based on a predicate
objectPluck keys from an object
objectExclude keys from an object
objectRecursively transform the keys and values of an object
numberAn interpolation function
*A function for generating array values
booleanA split predicate
string | nullA key transform predicate
anyA value transform predicate
functionKind: global function
Returns: function - A memoized version of the function
| Param | Type | Description |
| --- | --- | --- |
| f | function | The function to memoize |
booleanKind: global function
Returns: boolean - True if numbers a and b are approximately equal
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| a | number | | Number a |
| b | number | | Number b |
| [p] | number | Number.EPSILON | The precision value |
numberKind: global function
Returns: number - A clamped number
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| a | number | | The number to clamp |
| [min] | number | 0 | The minimum value |
| [max] | number | 1 | The maximum value |
numberKind: global function
Returns: number - The fractional part of the number
| Param | Type | Description |
| --- | --- | --- |
| a | number | The number from which to get the fractional part |
numberKind: global function
Returns: number - A rounded number
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| n | number | | The number to round |
| [d] | number | 0 | The number of decimal places to round to |
numberKind: global function
Returns: number - An interpolated value in the interval [a, b]
| Param | Type | Description |
| --- | --- | --- |
| a | number | The minimum number |
| b | number | The maximum number |
| i | number | The interpolation value, should be in the interval [0, 1] |
numberKind: global function
Returns: number - The position of i between a and b
| Param | Type | Description |
| --- | --- | --- |
| a | number | The minimum number |
| b | number | The maximum number |
| i | number | The interpolated value in the interval [a, b] |
numberKind: global function
Returns: number - A bilinear interpolated value
| Param | Type | Description |
| --- | --- | --- |
| c00 | number | Top-left value |
| c10 | number | Top-right value |
| c01 | number | Bottom-left value |
| c11 | number | Bottom-right value |
| ix | number | Interpolation value along x |
| iy | number | Interpolation value along y |
numberKind: global function
| Param | Type | Description |
| --- | --- | --- |
| i | number | The number to re-map |
| a1 | number | |
| a2 | number | |
| b1 | number | |
| b2 | number | |
numberKind: global function
Returns: number - An interpolated value in the interval [a, b]
| Param | Type | Description |
| --- | --- | --- |
| a | number | The minimum number |
| b | number | The maximum number |
| i | number | The interpolation value |
numberKind: global function
Returns: number - The angle in radians
| Param | Type | Description |
| --- | --- | --- |
| degrees | number | The angle in degrees |
numberKind: global function
Returns: number - The angle in degrees
| Param | Type | Description |
| --- | --- | --- |
| radians | number | The angle in radians |
numberKind: global function
Returns: number - A random float in the interval [min, max)
| Param | Type | Description |
| --- | --- | --- |
| min | number | Inclusive min |
| max | number | Exclusive max |
numberKind: global function
Returns: number - A random integer in the interval [min, max]
| Param | Type | Description |
| --- | --- | --- |
| min | number | Inclusive min |
| max | number | Inclusive max |
numberKind: global function
Returns: number - A normally-distributed random number
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [mu] | number | 0.5 | The mean value |
| [sigma] | number | 0.5 | The standard deviation |
| [samples] | number | 2 | The number of samples |
numberKind: global function
Returns: number - A normally-distributed random integer
| Param | Type | Description |
| --- | --- | --- |
| min | number | Inclusive min |
| max | number | Inclusive max |
numberKind: global function
Returns: number - An index from w
| Param | Type | Description |
| --- | --- | --- |
| w | Array.<number> | An array of weights |
numberKind: global function
Returns: number - An interpolated value in the interval [min(a), max(a)]
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| a | Array.<number> | | An array of values interpolate |
| i | number | | A number in the interval [0, 1] |
| [f] | InterpolationFunction | Math.lerp | The interpolation function to use |
numberKind: global function
Returns: number - a ∙ b
| Param | Type | Description |
| --- | --- | --- |
| a | Array.<number> | Vector a |
| b | Array.<number> | Vector b |
numberKind: global function
Returns: number - a!
| Param | Type |
| --- | --- |
| a | number |
numberKind: global function
Returns: number - nPr
| Param | Type |
| --- | --- |
| n | number |
| r | number |
numberKind: global function
Returns: number - nCr
| Param | Type |
| --- | --- |
| n | number |
| r | number |
Array.<Array.<\*>>Kind: global function
Returns: Array.<Array.<\*>> - An array of permutation arrays
| Param | Type | Description |
| --- | --- | --- |
| a | Array.<\*> | |
| r | number | The number of elements to choose in each permutation |
Example
`js`
permutations([1, 2, 3], 2);
Output:
`json`
[
[1, 2],
[1, 3],
[2, 1],
[2, 3],
[3, 1],
[3, 2]
]
Array.<Array.<\*>>Kind: global function
Returns: Array.<Array.<\*>> - An array of combination arrays
| Param | Type | Description |
| --- | --- | --- |
| a | Array.<\*> | |
| r | number | The number of elements to choose in each combination |
Example
`js`
combinations([1, 2, 3], 2);
Output:
`json`
[
[1, 2],
[1, 3],
[2, 3]
]
Kind: global function
Example
`js`
cartesian([1, 2, 3], ['a', 'b']);
Output:
`json``
[
[1, "a"],
[1, "b"],
[2, "a"],
[2, "b"],
[3, "a"],
[3, "b"]
]
Array.<\*>Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| f | TimesFunction | |
| n | number | The size of the array |
Array.<number>Kind: global function
Returns: Array.<number> - An array of integers 0->(n - 1)
| Param | Type | Description |
| --- | --- | --- |
| n | number | The size of the array |
Array.<Array.<\*>>Kind: global function
| Param | Type | Description |
| --- | --- | --- |
| ...a | Array.<\*> | The arrays to zip |
\*Kind: global function
Returns: \* - An element from the array
| Param | Type | Description |
| --- | --- | --- |
| a | Array.<\*> | The array to access |
| i | number | The positively/negatively wrapped array index |
\*Kind: global function
Returns: \* - The last element from the array
| Param | Type |
| --- | --- |
| a | Array.<\*> |
numberKind: global function
Returns: number - The index in the unrolled array
| Param | Type | Description |
| --- | --- | --- |
| x | number | The x position |
| y | number | The y position |
| w | number | The width of the 2d array |
Array.<number>Kind: global function
Returns: Array.<number> - The position as a 2-tuple
| Param | Type | Description |
| --- | --- | --- |
| i | number | The index |
| w | number | The width of the 2d array |
Array.<Array.<\*>>Kind: global function
Returns: Array.<Array.<\*>> - An array of array chunks
| Param | Type | Description |
| --- | --- | --- |
| a | Array.<\*> | |
| n | number | The chunk size |
Array.<\*>Kind: global function
Returns: Array.<\*> - The shuffled array
| Param | Type |
| --- | --- |
| a | Array.<\*> |
objectKind: global function
Returns: object - A flattened object
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| o | object | | |
| concatenator | string | "." | The string to use for concatenating keys |
objectKind: global function
Returns: object - An un-flattened object
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| o | object | | |
| concatenator | string | "." | The string to check for in concatenated keys |
Array.<Array.<\*>>Kind: global function
Returns: Array.<Array.<\*>> - An array of arrays
| Param | Type |
| --- | --- |
| array | Array.<\*> |
| predicate | SplitPredicate |
objectKind: global function
Returns: object - An object containing the plucked keys
| Param | Type | Description |
| --- | --- | --- |
| o | object | |
| ...keys | string | The keys to pluck from the object |
objectKind: global function
Returns: object - An object containing all keys except excluded keys
| Param | Type | Description |
| --- | --- | --- |
| o | object | |
| ...keys | string | The keys to exclude from the object |
objectKind: global function
Returns: object - The transformed object
| Param | Type | Description |
| --- | --- | --- |
| o | object | The object to transform |
| [kf] | KeyTransformFunction | Optional key transform |
| [vf] | ValueTransformFunction | Optional value transform |
numberKind: global typedef
Returns: number - The interpolated value in the interval [a, b]
| Param | Type | Description |
| --- | --- | --- |
| a | number | The minimum number |
| b | number | The maximum number |
| i | number | The interpolation value, should be in the interval [0, 1] |
\*Kind: global typedef
Returns: \* - The array value
| Param | Type | Description |
| --- | --- | --- |
| i | number | The array index |
booleanKind: global typedef
Returns: boolean - True if the array should split at this index
| Param | Type | Description |
| --- | --- | --- |
| value | any | The current value |
string \| nullKind: global typedef
Returns: string \| null - The transformed key, or null to omit the key
| Param | Type | Description |
| --- | --- | --- |
| o | object | The object being transformed |
| path | string | The current path in dot notation |
| key | string | The current key |
| value | any | The current value |
anyKind: global typedef
Returns: any - The transformed value
| Param | Type | Description |
| --- | --- | --- |
| o | object | The object being transformed |
| path | string | The current path in dot notation |
| key | string | The current key |
| value | any | The current value |