1.4.0 • Published 1 year ago

@basementuniverse/utils v1.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

utils

A small library of useful functions

Usage

Node:

const utils = require('@basementuniverse/utils');

Browser:

<script src="utils.js"></script>

Typescript:

import * as utils from '@basementuniverse/utils';

Contents

Functions

Typedefs

floatEquals(a, b, p) ⇒ boolean

Check if two numbers are approximately equal

Kind: global function
Returns: boolean - True if numbers a and b are approximately equal

ParamTypeDefaultDescription
anumberNumber a
bnumberNumber b
pnumberNumber.EPSILONThe precision value

clamp(a, min, max) ⇒ number

Clamp a number between min and max

Kind: global function
Returns: number - A clamped number

ParamTypeDefaultDescription
anumberThe number to clamp
minnumber0The minimum value
maxnumber1The maximum value

frac(a) ⇒ number

Get the fractional part of a number

Kind: global function
Returns: number - The fractional part of the number

ParamTypeDescription
anumberThe number from which to get the fractional part

round(n, d) ⇒ number

Round n to d decimal places

Kind: global function
Returns: number - A rounded number

ParamTypeDefaultDescription
nnumberThe number to round
dnumber0The number of decimal places to round to

lerp(a, b, i) ⇒ number

Do a linear interpolation between a and b

Kind: global function
Returns: number - An interpolated value in the interval a, b

ParamTypeDescription
anumberThe minimum number
bnumberThe maximum number
inumberThe interpolation value, should be in the interval 0, 1

unlerp(a, b, i) ⇒ number

Get the position of i between a and b

Kind: global function
Returns: number - The position of i between a and b

ParamTypeDescription
anumberThe minimum number
bnumberThe maximum number
inumberThe interpolated value in the interval a, b

blerp(c00, c10, c01, c11, ix, iy) ⇒ number

Do a bilinear interpolation

Kind: global function
Returns: number - A bilinear interpolated value

ParamTypeDescription
c00numberTop-left value
c10numberTop-right value
c01numberBottom-left value
c11numberBottom-right value
ixnumberInterpolation value along x
iynumberInterpolation value along y

remap(i, a1, a2, b1, b2) ⇒ number

Re-map a number i from range a1...a2 to b1...b2

Kind: global function

ParamTypeDescription
inumberThe number to re-map
a1number
a2number
b1number
b2number

smoothstep(a, b, i) ⇒ number

Do a smooth interpolation between a and b

Kind: global function
Returns: number - An interpolated value in the interval a, b

ParamTypeDescription
anumberThe minimum number
bnumberThe maximum number
inumberThe interpolation value

radians(degrees) ⇒ number

Get an angle in radians

Kind: global function
Returns: number - The angle in radians

ParamTypeDescription
degreesnumberThe angle in degrees

degrees(radians) ⇒ number

Get an angle in degrees

Kind: global function
Returns: number - The angle in degrees

ParamTypeDescription
radiansnumberThe angle in radians

randomBetween(min, max) ⇒ number

Get a random float in the interval [min, max)

Kind: global function
Returns: number - A random float in the interval [min, max)

ParamTypeDescription
minnumberInclusive min
maxnumberExclusive max

randomIntBetween(min, max) ⇒ number

Get a random integer in the interval min, max

Kind: global function
Returns: number - A random integer in the interval min, max

ParamTypeDescription
minnumberInclusive min
maxnumberInclusive max

cltRandom(mu, sigma, samples) ⇒ number

Get a normally-distributed random number

Kind: global function
Returns: number - A normally-distributed random number

ParamTypeDefaultDescription
munumber0.5The mean value
sigmanumber0.5The standard deviation
samplesnumber2The number of samples

cltRandomInt(min, max) ⇒ number

Get a normally-distributed random integer in the interval min, max

Kind: global function
Returns: number - A normally-distributed random integer

ParamTypeDescription
minnumberInclusive min
maxnumberInclusive max

weightedRandom(w) ⇒ number

Return a weighted random integer

Kind: global function
Returns: number - An index from w

ParamTypeDescription
wArray.<number>An array of weights

lerpArray(a, i, f) ⇒ number

Return an interpolated value from an array

Kind: global function
Returns: number - An interpolated value in the interval min(a), max(a)

ParamTypeDefaultDescription
aArray.<number>An array of values interpolate
inumberA number in the interval 0, 1
fInterpolationFunctionMath.lerpThe interpolation function to use

dot(a, b) ⇒ number

Get the dot product of two vectors

Kind: global function
Returns: number - a ∙ b

ParamTypeDescription
aArray.<number>Vector a
bArray.<number>Vector b

factorial(a) ⇒ number

Get the factorial of a number

Kind: global function
Returns: number - a!

ParamType
anumber

npr(n, r) ⇒ number

Get the number of permutations of r elements from a set of n elements

Kind: global function
Returns: number - nPr

ParamType
nnumber
rnumber

ncr(n, r) ⇒ number

Get the number of combinations of r elements from a set of n elements

Kind: global function
Returns: number - nCr

ParamType
nnumber
rnumber

combinations(a, r) ⇒ Array.<Array.<*>>

Generate all combinations of r elements from an array

Kind: global function
Returns: Array.<Array.<*>> - An array of combination arrays

ParamTypeDescription
aArray.<*>
rnumberThe number of elements to choose in each combination

Example

combinations([1, 2, 3], 2);

Output:

[
  [1, 2],
  [1, 3],
  [2, 3]
]

cartesian()

Get a cartesian product of arrays

Kind: global function
Example

cartesian([1, 2, 3], ['a', 'b']);

Output:

[
  [1, "a"],
  [1, "b"],
  [2, "a"],
  [2, "b"],
  [3, "a"],
  [3, "b"]
]

times(f, n) ⇒ Array.<*>

Return a new array with length n by calling function f(i) on each element

Kind: global function

ParamTypeDescription
fTimesFunction
nnumberThe size of the array

range(n) ⇒ Array.<number>

Return an array containing numbers 0->(n - 1)

Kind: global function
Returns: Array.<number> - An array of integers 0->(n - 1)

ParamTypeDescription
nnumberThe size of the array

zip(a, b) ⇒ Array.<Array.<*>>

Zip 2 arrays together, i.e. (1, 2, 3, a, b, c) => [1, a, 2, b, 3, c]

Kind: global function

ParamType
aArray.<*>
bArray.<*>

at(a, i) ⇒ *

Return arrayi with positive and negative wrapping

Kind: global function
Returns: * - An element from the array

ParamTypeDescription
aArray.<*>
inumberThe positively/negatively wrapped array index

peek(a) ⇒ *

Return the last element of an array without removing it

Kind: global function
Returns: * - The last element from the array

ParamType
aArray.<*>

chunk(a, n) ⇒ Array.<Array.<*>>

Chop an array into chunks of size n

Kind: global function
Returns: Array.<Array.<*>> - An array of array chunks

ParamTypeDescription
aArray.<*>
nnumberThe chunk size

shuffle(a) ⇒ Array.<*>

Randomly shuffle a shallow copy of an array

Kind: global function
Returns: Array.<*> - The shuffled array

ParamType
aArray.<*>

flat(o, concatenator) ⇒ object

Flatten an object

Kind: global function
Returns: object - A flattened object

ParamTypeDefaultDescription
oobject
concatenatorstring"."The string to use for concatenating keys

unflat(o, concatenator) ⇒ object

Unflatten an object

Kind: global function
Returns: object - An un-flattened object

ParamTypeDefaultDescription
oobject
concatenatorstring"."The string to check for in concatenated keys

split(array, predicate) ⇒ Array.<Array.<*>>

Split an array into sub-arrays based on a predicate

Kind: global function
Returns: Array.<Array.<*>> - An array of arrays

ParamType
arrayArray.<*>
predicateSplitPredicate

pluck(o, ...keys) ⇒ object

Pluck keys from an object

Kind: global function
Returns: object - An object containing the plucked keys

ParamTypeDescription
oobject
...keysstringThe keys to pluck from the object

exclude(o, ...keys) ⇒ object

Exclude keys from an object

Kind: global function
Returns: object - An object containing all keys except excluded keys

ParamTypeDescription
oobject
...keysstringThe keys to exclude from the object

InterpolationFunction ⇒ number

An interpolation function

Kind: global typedef
Returns: number - The interpolated value in the interval a, b

ParamTypeDescription
anumberThe minimum number
bnumberThe maximum number
inumberThe interpolation value, should be in the interval 0, 1

TimesFunction ⇒ *

A function for generating array values

Kind: global typedef
Returns: * - The array value

ParamTypeDescription
inumberThe array index

SplitPredicate ⇒ boolean

A split predicate

Kind: global typedef
Returns: boolean - True if the array should split at this index

ParamTypeDescription
valueanyThe current value