1.2.10 • Published 3 years ago

@basementuniverse/commonjs v1.2.10

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

common.js

A small library of useful functions

Usage

Node:

const { vec, mat } = require('@basementuniverse/commonjs');

Browser:

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

Typescript:

import { vec, mat } from '@basementuniverse/commonjs';

Contents

Classes

Functions

Typedefs

Math

Kind: global class

Math.floatEquals(a, b, p) ⇒ boolean

Check if two numbers are approximately equal

Kind: static method of Math
Returns: boolean - True if numbers a and b are approximately equal

ParamTypeDefaultDescription
anumberNumber a
bnumberNumber b
pnumberNumber.EPSILONThe precision value

Math.clamp(a, min, max) ⇒ number

Clamp a number between min and max

Kind: static method of Math
Returns: number - A clamped number

ParamTypeDefaultDescription
anumberThe number to clamp
minnumber0The minimum value
maxnumber1The maximum value

Math.frac(a) ⇒ number

Get the fractional part of a number

Kind: static method of Math
Returns: number - The fractional part of the number

ParamTypeDescription
anumberThe number from which to get the fractional part

Math.lerp(a, b, i) ⇒ number

Do a linear interpolation between a and b

Kind: static method of Math
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

Math.unlerp(a, b, i) ⇒ number

Get the position of i between a and b

Kind: static method of Math
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

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

Do a bilinear interpolation

Kind: static method of Math
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

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

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

Kind: static method of Math

ParamTypeDescription
inumberThe number to re-map
a1number
a2number
b1number
b2number

Math.smoothstep(a, b, i) ⇒ number

Do a smooth interpolation between a and b

Kind: static method of Math
Returns: number - An interpolated value in the interval a, b

ParamTypeDescription
anumberThe minimum number
bnumberThe maximum number
inumberThe interpolation value

Math.radians(degrees) ⇒ number

Get an angle in radians

Kind: static method of Math
Returns: number - The angle in radians

ParamTypeDescription
degreesnumberThe angle in degrees

Math.degrees(radians) ⇒ number

Get an angle in degrees

Kind: static method of Math
Returns: number - The angle in degrees

ParamTypeDescription
radiansnumberThe angle in radians

Math.randomBetween(min, max) ⇒ number

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

Kind: static method of Math
Returns: number - A random float in the interval [min, max)

ParamTypeDescription
minnumberInclusive min
maxnumberExclusive max

Math.randomIntBetween(min, max) ⇒ number

Get a random integer in the interval min, max

Kind: static method of Math
Returns: number - A random integer in the interval min, max

ParamTypeDescription
minnumberInclusive min
maxnumberInclusive max

Math.cltRandom(mu, sigma, samples) ⇒ number

Get a normally-distributed random number

Kind: static method of Math
Returns: number - A normally-distributed random number

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

Math.cltRandomInt(min, max) ⇒ number

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

Kind: static method of Math
Returns: number - A normally-distributed random integer

ParamTypeDescription
minnumberInclusive min
maxnumberInclusive max

Math.weightedRandom(w) ⇒ number

Return a weighted random integer

Kind: static method of Math
Returns: number - An index from w

ParamTypeDescription
wArray.<number>An array of weights

Math.lerpArray(a, i, f) ⇒ number

Return an interpolated value from an array

Kind: static method of Math
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
finterpolationCallbackMath.lerpThe interpolation function to use

Math.dot(a, b) ⇒ number

Get the dot product of two vectors

Kind: static method of Math
Returns: number - a ∙ b

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

Math.factorial(a) ⇒ number

Get the factorial of a number

Kind: static method of Math
Returns: number - a!

ParamType
anumber

Math.permutation(n, r) ⇒ number

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

Kind: static method of Math
Returns: number - nPr

ParamType
nnumber
rnumber

Math.combination(n, r) ⇒ number

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

Kind: static method of Math
Returns: number - nCr

ParamType
nnumber
rnumber

Array

Kind: global class

array.at(i) ⇒ *

Return arrayi with positive and negative wrapping

Kind: instance method of Array
Returns: * - An element from the array

ParamTypeDescription
inumberThe positively/negatively wrapped array index

array.chunk(n) ⇒ Array.<Array.<*>>

Chop an array into chunks of size n

Kind: instance method of Array
Returns: Array.<Array.<*>> - An array of array chunks

ParamTypeDescription
nnumberThe chunk size

array.shuffle() ⇒ Array.<*>

Randomly shuffle an array in-place

Kind: instance method of Array
Returns: Array.<*> - The shuffled array

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

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

Kind: static method of Array

ParamTypeDescription
ftimesCallback
nnumberThe size of the array

Array.range(n) ⇒ Array.<number>

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

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

ParamTypeDescription
nnumberThe size of the array

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: static method of Array

ParamType
aArray.<*>
bArray.<*>

vec(x, y) ⇒ vec

Create a new vector

Kind: global function
Returns: vec - A new vector

ParamTypeDescription
xnumber | vecThe x component of the vector, or a vector to copy
ynumberThe y component of the vector

Example (Various ways to initialise a vector)

let a = vec(3, 2);  // (3, 2)
let b = vec(4);     // (4, 4)
let c = vec(a);     // (3, 2)
let d = vec();      // (0, 0)

vec.components(a) ⇒ Array.<number>

Get the components of a vector as an array

Kind: static method of vec
Returns: Array.<number> - The vector components as an array

ParamTypeDescription
avecThe vector to get components from

vec.ux() ⇒ vec

Return a unit vector (1, 0)

Kind: static method of vec
Returns: vec - A unit vector (1, 0)

vec.uy() ⇒ vec

Return a unit vector (0, 1)

Kind: static method of vec
Returns: vec - A unit vector (0, 1)

vec.add(a, b) ⇒ vec

Add vectors

Kind: static method of vec
Returns: vec - a + b

ParamTypeDescription
avecVector a
bvecVector b

vec.mul(a, b) ⇒ vec

Scale a vector

Kind: static method of vec
Returns: vec - a * b

ParamTypeDescription
avecVector a
bnumberScalar b

vec.sub(a, b) ⇒ vec

Subtract vectors

Kind: static method of vec
Returns: vec - a - b

ParamTypeDescription
avecVector a
bvecVector b

vec.len(a) ⇒ number

Get the length of a vector

Kind: static method of vec
Returns: number - |a|

ParamTypeDescription
avecVector a

vec.manhattan(a) ⇒ number

Get the length of a vector using taxicab geometry

Kind: static method of vec
Returns: number - |a|

ParamTypeDescription
avecVector a

vec.nor(a) ⇒ vec

Normalise a vector

Kind: static method of vec
Returns: vec - ^a

ParamTypeDescription
avecThe vector to normalise

vec.dot(a, b) ⇒ number

Get a dot product of vectors

Kind: static method of vec
Returns: number - a ∙ b

ParamTypeDescription
avecVector a
bvecVector b

vec.rot(a, r) ⇒ vec

Rotate a vector by r radians

Kind: static method of vec
Returns: vec - A rotated vector

ParamTypeDescription
avecThe vector to rotate
rnumberThe angle to rotate by, measured in radians

vec.eq(a, b) ⇒ boolean

Check if two vectors are equal

Kind: static method of vec
Returns: boolean - True if vectors a and b are equal, false otherwise

ParamTypeDescription
avecVector a
bvecVector b

vec.rad(a) ⇒ number

Get the angle of a vector

Kind: static method of vec
Returns: number - The angle of vector a in radians

ParamTypeDescription
avecVector a

vec.cpy(a) ⇒ vec

Copy a vector

Kind: static method of vec
Returns: vec - A copy of vector a

ParamTypeDescription
avecThe vector to copy

vec.map(a, f) ⇒ vec

Call a function on each component of a vector and build a new vector from the results

Kind: static method of vec
Returns: vec - Vector a mapped through f

ParamTypeDescription
avecVector a
fvectorMapCallbackThe function to call on each component of the vector

vec.str(a, s) ⇒ string

Convert a vector into a string

Kind: static method of vec
Returns: string - A string representation of the vector

ParamTypeDefaultDescription
avecThe vector to convert
sstring"', '"The separator string

mat(m, n, entries) ⇒ mat

Create a new matrix

Kind: global function
Returns: mat - A new matrix

ParamTypeDefaultDescription
mnumber4The number of rows
nnumber4The number of columns
entriesArray.<number>[]Matrix values in reading order

mat.identity(n) ⇒ mat

Get an identity matrix of size n

Kind: static method of mat
Returns: mat - An identity matrix

ParamTypeDescription
nnumberThe size of the matrix

mat.get(a, i, j) ⇒ number

Get an entry from a matrix

Kind: static method of mat
Returns: number - The value at position (i, j) in matrix a

ParamTypeDescription
amatMatrix a
inumberThe row offset
jnumberThe column offset

mat.set(a, i, j, v)

Set an entry of a matrix

Kind: static method of mat

ParamTypeDescription
amatMatrix a
inumberThe row offset
jnumberThe column offset
vnumberThe value to set in matrix a

mat.row(a, m) ⇒ Array.<number>

Get a row from a matrix as an array

Kind: static method of mat
Returns: Array.<number> - Row m from matrix a

ParamTypeDescription
amatMatrix a
mnumberThe row offset

mat.col(a, n) ⇒ Array.<number>

Get a column from a matrix as an array

Kind: static method of mat
Returns: Array.<number> - Column n from matrix a

ParamTypeDescription
amatMatrix a
nnumberThe column offset

mat.add(a, b) ⇒ mat

Add matrices

Kind: static method of mat
Returns: mat - a + b

ParamTypeDescription
amatMatrix a
bmatMatrix b

mat.sub(a, b) ⇒ mat

Subtract matrices

Kind: static method of mat
Returns: mat - a - b

ParamTypeDescription
amatMatrix a
bmatMatrix b

mat.mul(a, b) ⇒ mat | boolean

Multiply matrices

Kind: static method of mat
Returns: mat | boolean - ab or false if the matrices cannot be multiplied

ParamTypeDescription
amatMatrix a
bmatMatrix b

mat.scale(a, b) ⇒ mat

Scale a matrix

Kind: static method of mat
Returns: mat - a * b

ParamTypeDescription
amatMatrix a
bnumberScalar b

mat.trans(a) ⇒ mat

Transpose a matrix

Kind: static method of mat
Returns: mat - A transposed matrix

ParamTypeDescription
amatThe matrix to transpose

mat.minor(a, i, j) ⇒ mat | boolean

Get the minor of a matrix

Kind: static method of mat
Returns: mat | boolean - The (i, j) minor of matrix a or false if the matrix is not square

ParamTypeDescription
amatMatrix a
inumberThe row offset
jnumberThe column offset

mat.det(a) ⇒ number | boolean

Get the determinant of a matrix

Kind: static method of mat
Returns: number | boolean - |a| or false if the matrix is not square

ParamTypeDescription
amatMatrix a

mat.nor(a) ⇒ mat | boolean

Normalise a matrix

Kind: static method of mat
Returns: mat | boolean - ^a or false if the matrix is not square

ParamTypeDescription
amatThe matrix to normalise

mat.adj(a) ⇒ mat

Get the adjugate of a matrix

Kind: static method of mat
Returns: mat - The adjugate of a

ParamTypeDescription
amatThe matrix from which to get the adjugate

mat.inv(a) ⇒ mat | boolean

Get the inverse of a matrix

Kind: static method of mat
Returns: mat | boolean - a^-1 or false if the matrix has no inverse

ParamTypeDescription
amatThe matrix to invert

mat.eq(a, b) ⇒ boolean

Check if two matrices are equal

Kind: static method of mat
Returns: boolean - True if matrices a and b are identical, false otherwise

ParamTypeDescription
amatMatrix a
bmatMatrix b

mat.cpy(a) ⇒ mat

Copy a matrix

Kind: static method of mat
Returns: mat - A copy of matrix a

ParamTypeDescription
amatThe matrix to copy

mat.map(a, f) ⇒ mat

Call a function on each entry of a matrix and build a new matrix from the results

Kind: static method of mat
Returns: mat - Matrix a mapped through f

ParamTypeDescription
amatMatrix a
fmatrixMapCallbackThe function to call on each entry of the matrix

mat.str(a, ms, ns) ⇒ string

Convert a matrix into a string

Kind: static method of mat
Returns: string - A string representation of the matrix

ParamTypeDefaultDescription
amatThe matrix to convert
msstring"', '"The separator string for columns
nsstring"'\n'"The separator string for rows

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

timesCallback ⇒ *

A function for generating array values

Kind: global typedef
Returns: * - The array value

ParamTypeDescription
inumberThe array index

vec : Object

A 2d vector

Kind: global typedef
Properties

NameTypeDescription
xnumberThe x component of the vector
ynumberThe y component of the vector

vec.components(a) ⇒ Array.<number>

Get the components of a vector as an array

Kind: static method of vec
Returns: Array.<number> - The vector components as an array

ParamTypeDescription
avecThe vector to get components from

vec.ux() ⇒ vec

Return a unit vector (1, 0)

Kind: static method of vec
Returns: vec - A unit vector (1, 0)

vec.uy() ⇒ vec

Return a unit vector (0, 1)

Kind: static method of vec
Returns: vec - A unit vector (0, 1)

vec.add(a, b) ⇒ vec

Add vectors

Kind: static method of vec
Returns: vec - a + b

ParamTypeDescription
avecVector a
bvecVector b

vec.mul(a, b) ⇒ vec

Scale a vector

Kind: static method of vec
Returns: vec - a * b

ParamTypeDescription
avecVector a
bnumberScalar b

vec.sub(a, b) ⇒ vec

Subtract vectors

Kind: static method of vec
Returns: vec - a - b

ParamTypeDescription
avecVector a
bvecVector b

vec.len(a) ⇒ number

Get the length of a vector

Kind: static method of vec
Returns: number - |a|

ParamTypeDescription
avecVector a

vec.manhattan(a) ⇒ number

Get the length of a vector using taxicab geometry

Kind: static method of vec
Returns: number - |a|

ParamTypeDescription
avecVector a

vec.nor(a) ⇒ vec

Normalise a vector

Kind: static method of vec
Returns: vec - ^a

ParamTypeDescription
avecThe vector to normalise

vec.dot(a, b) ⇒ number

Get a dot product of vectors

Kind: static method of vec
Returns: number - a ∙ b

ParamTypeDescription
avecVector a
bvecVector b

vec.rot(a, r) ⇒ vec

Rotate a vector by r radians

Kind: static method of vec
Returns: vec - A rotated vector

ParamTypeDescription
avecThe vector to rotate
rnumberThe angle to rotate by, measured in radians

vec.eq(a, b) ⇒ boolean

Check if two vectors are equal

Kind: static method of vec
Returns: boolean - True if vectors a and b are equal, false otherwise

ParamTypeDescription
avecVector a
bvecVector b

vec.rad(a) ⇒ number

Get the angle of a vector

Kind: static method of vec
Returns: number - The angle of vector a in radians

ParamTypeDescription
avecVector a

vec.cpy(a) ⇒ vec

Copy a vector

Kind: static method of vec
Returns: vec - A copy of vector a

ParamTypeDescription
avecThe vector to copy

vec.map(a, f) ⇒ vec

Call a function on each component of a vector and build a new vector from the results

Kind: static method of vec
Returns: vec - Vector a mapped through f

ParamTypeDescription
avecVector a
fvectorMapCallbackThe function to call on each component of the vector

vec.str(a, s) ⇒ string

Convert a vector into a string

Kind: static method of vec
Returns: string - A string representation of the vector

ParamTypeDefaultDescription
avecThe vector to convert
sstring"', '"The separator string

vectorMapCallback ⇒ number

A function to call on each component of a vector

Kind: global typedef
Returns: number - The mapped component

ParamTypeDescription
valuenumberThe component value
label'x' | 'y'The component label (x or y)

mat : Object

A matrix

Kind: global typedef
Properties

NameTypeDescription
mnumberThe number of rows in the matrix
nnumberThe number of columns in the matrix
entriesArray.<number>The matrix values

mat.identity(n) ⇒ mat

Get an identity matrix of size n

Kind: static method of mat
Returns: mat - An identity matrix

ParamTypeDescription
nnumberThe size of the matrix

mat.get(a, i, j) ⇒ number

Get an entry from a matrix

Kind: static method of mat
Returns: number - The value at position (i, j) in matrix a

ParamTypeDescription
amatMatrix a
inumberThe row offset
jnumberThe column offset

mat.set(a, i, j, v)

Set an entry of a matrix

Kind: static method of mat

ParamTypeDescription
amatMatrix a
inumberThe row offset
jnumberThe column offset
vnumberThe value to set in matrix a

mat.row(a, m) ⇒ Array.<number>

Get a row from a matrix as an array

Kind: static method of mat
Returns: Array.<number> - Row m from matrix a

ParamTypeDescription
amatMatrix a
mnumberThe row offset

mat.col(a, n) ⇒ Array.<number>

Get a column from a matrix as an array

Kind: static method of mat
Returns: Array.<number> - Column n from matrix a

ParamTypeDescription
amatMatrix a
nnumberThe column offset

mat.add(a, b) ⇒ mat

Add matrices

Kind: static method of mat
Returns: mat - a + b

ParamTypeDescription
amatMatrix a
bmatMatrix b

mat.sub(a, b) ⇒ mat

Subtract matrices

Kind: static method of mat
Returns: mat - a - b

ParamTypeDescription
amatMatrix a
bmatMatrix b

mat.mul(a, b) ⇒ mat | boolean

Multiply matrices

Kind: static method of mat
Returns: mat | boolean - ab or false if the matrices cannot be multiplied

ParamTypeDescription
amatMatrix a
bmatMatrix b

mat.scale(a, b) ⇒ mat

Scale a matrix

Kind: static method of mat
Returns: mat - a * b

ParamTypeDescription
amatMatrix a
bnumberScalar b

mat.trans(a) ⇒ mat

Transpose a matrix

Kind: static method of mat
Returns: mat - A transposed matrix

ParamTypeDescription
amatThe matrix to transpose

mat.minor(a, i, j) ⇒ mat | boolean

Get the minor of a matrix

Kind: static method of mat
Returns: mat | boolean - The (i, j) minor of matrix a or false if the matrix is not square

ParamTypeDescription
amatMatrix a
inumberThe row offset
jnumberThe column offset

mat.det(a) ⇒ number | boolean

Get the determinant of a matrix

Kind: static method of mat
Returns: number | boolean - |a| or false if the matrix is not square

ParamTypeDescription
amatMatrix a

mat.nor(a) ⇒ mat | boolean

Normalise a matrix

Kind: static method of mat
Returns: mat | boolean - ^a or false if the matrix is not square

ParamTypeDescription
amatThe matrix to normalise

mat.adj(a) ⇒ mat

Get the adjugate of a matrix

Kind: static method of mat
Returns: mat - The adjugate of a

ParamTypeDescription
amatThe matrix from which to get the adjugate

mat.inv(a) ⇒ mat | boolean

Get the inverse of a matrix

Kind: static method of mat
Returns: mat | boolean - a^-1 or false if the matrix has no inverse

ParamTypeDescription
amatThe matrix to invert

mat.eq(a, b) ⇒ boolean

Check if two matrices are equal

Kind: static method of mat
Returns: boolean - True if matrices a and b are identical, false otherwise

ParamTypeDescription
amatMatrix a
bmatMatrix b

mat.cpy(a) ⇒ mat

Copy a matrix

Kind: static method of mat
Returns: mat - A copy of matrix a

ParamTypeDescription
amatThe matrix to copy

mat.map(a, f) ⇒ mat

Call a function on each entry of a matrix and build a new matrix from the results

Kind: static method of mat
Returns: mat - Matrix a mapped through f

ParamTypeDescription
amatMatrix a
fmatrixMapCallbackThe function to call on each entry of the matrix

mat.str(a, ms, ns) ⇒ string

Convert a matrix into a string

Kind: static method of mat
Returns: string - A string representation of the matrix

ParamTypeDefaultDescription
amatThe matrix to convert
msstring"', '"The separator string for columns
nsstring"'\n'"The separator string for rows

matrixMapCallback ⇒ number

A function to call on each entry of a matrix

Kind: global typedef
Returns: number - The mapped entry

ParamTypeDescription
valuenumberThe entry value
indexnumberThe entry index
entriesArray.<number>The array of matrix entries
1.2.10

3 years ago

1.2.9

3 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago