Socket
Socket
Sign inDemoInstall

@basementuniverse/utils

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @basementuniverse/utils

A collection of useful functions


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Install size
46.8 kB
Created
Weekly downloads
 

Readme

Source

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

floatEquals(a, b, [p])boolean

Check if two numbers are approximately equal

clamp(a, [min], [max])number

Clamp a number between min and max

frac(a)number

Get the fractional part of a number

round(n, [d])number

Round n to d decimal places

lerp(a, b, i)number

Do a linear interpolation between a and b

unlerp(a, b, i)number

Get the position of i between a and b

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

Do a bilinear interpolation

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

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

smoothstep(a, b, i)number

Do a smooth interpolation between a and b

radians(degrees)number

Get an angle in radians

degrees(radians)number

Get an angle in degrees

randomBetween(min, max)number

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

randomIntBetween(min, max)number

Get a random integer in the interval [min, max]

cltRandom([mu], [sigma], [samples])number

Get a normally-distributed random number

cltRandomInt(min, max)number

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

weightedRandom(w)number

Return a weighted random integer

lerpArray(a, i, [f])number

Return an interpolated value from an array

dot(a, b)number

Get the dot product of two vectors

factorial(a)number

Get the factorial of a number

npr(n, r)number

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

ncr(n, r)number

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

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

Generate all combinations of r elements from an array

cartesian()

Get a cartesian product of arrays

times(f, n)Array.<*>

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

range(n)Array.<number>

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

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

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

at(a, i)*

Return array[i] with positive and negative wrapping

peek(a)*

Return the last element of an array without removing it

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

Chop an array into chunks of size n

shuffle(a)Array.<*>

Randomly shuffle a shallow copy of an array

flat(o, concatenator)object

Flatten an object

unflat(o, concatenator)object

Unflatten an object

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

Split an array into sub-arrays based on a predicate

pluck(o, ...keys)object

Pluck keys from an object

exclude(o, ...keys)object

Exclude keys from an object

Typedefs

InterpolationFunctionnumber

An interpolation function

TimesFunction*

A function for generating array values

SplitPredicateboolean

A split predicate

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
[p]numberNumber.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
[min]number0The minimum value
[max]number1The 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
[d]number0The 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
[mu]number0.5The mean value
[sigma]number0.5The standard deviation
[samples]number2The 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]
[f]InterpolationFunctionMath.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 array[i] 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

FAQs

Last updated on 11 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc