Options
All
  • Public
  • Public/Protected
  • All
Menu

algomatic

Index

Type aliases

ArrayElement<T>: T extends ArrayLike<infer E> ? E : never

Infers a type of an array value.

Type parameters

  • T: ArrayLike<unknown>

Comparator<T>: (a: T, b: T) => number

Type parameters

  • T

Type declaration

    • (a: T, b: T): number
    • Compares a and b and returns:

      • Negative number if a < b;
      • Positive number if a > b;
      • 0 if a is equal to b.

      Parameters

      • a: T
      • b: T

      Returns number

Easing: (t: number) => number

Type declaration

    • (t: number): number
    • The easing function that receives t ∈ [0, 1] and return a mapped value.

      Parameters

      • t: number

      Returns number

Functions

  • and(a: number, b: number): number
  • Bitwise AND operator for large unsigned integers.

    Parameters

    • a: number
    • b: number

    Returns number

  • asc<T>(a: T, b: T): number
  • Returns -1 if a < b, 1 if a > b and 0 otherwise.

    Type parameters

    • T

    Parameters

    • a: T
    • b: T

    Returns number

  • binarySearch<T>(xs: ArrayLike<T>, x: T, n?: number, comparator?: Comparator<T>): number
  • Searches the specified array xs for the specified value x using the binary search algorithm. The array must be sorted into ascending order according to the natural ordering of its elements prior to making this call. If it is not sorted, the results are undefined.

    Type parameters

    • T

    Parameters

    • xs: ArrayLike<T>

      The array to be searched.

    • x: T

      The value to be searched for.

    • n: number = xs.length
    • Optional comparator: Comparator<T>

      The callback that defines the sort order. If omitted, the array elements are compared using comparison operators.

    Returns number

    The index of the searched value, if it is contained in the array; otherwise, -(insertion point) - 1. The insertion point is defined as the point at which the searched value would be inserted into the array: the index of the first element greater than the searched value, or array length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be ≥ 0 if and only if the searched value is found.

  • byte(x: number): number
  • Converts number to non-NaN 8-bit unsigned integer.

    see

    int

    Parameters

    • x: number

    Returns number

  • callOrGet<T, A>(value: T | ((...args: A) => T), ...args: A): T
  • If value is a function then the invocation result is returned. Otherwise value is returned as is.

    Type parameters

    • T

    • A: unknown[]

    Parameters

    • value: T | ((...args: A) => T)
    • Rest ...args: A

    Returns T

  • clamp(x: number, a: number, b: number): number
  • Clamps x to range [a, b].

    Range can be inverse.

    see

    {@link clamp01}

    Parameters

    • x: number
    • a: number
    • b: number

    Returns number

  • clamp1(x: number): number
  • closest(x: number, xs: ArrayLike<number>): number
  • Returns the closest value to x from arr.

    If arr is empty then x is returned as is.

    closest(1.8, [0, 3, 6]) // → 3
    
    see

    isEpsClose

    Parameters

    • x: number
    • xs: ArrayLike<number>

    Returns number

  • Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

    Type parameters

    Parameters

    • src: MutableArrayLike<ArrayElement<T>>

      The source array.

    • dest: T

      The destination array.

    • srcIndex: number = 0

      The start position in the source array.

    • destIndex: number = 0

      The start position in the destination array.

    • n: number = src.length

      The number of elements to copy.

    Returns T

    The destination array.

  • Computes cubic splines for given pivot points.

    Note: This function doesn't do any checks of arguments for performance reasons.

    const splines = createCSplines(xs, ys, xs.length);
    

    Parameters

    • xs: ArrayLike<number>

      The array of X coordinates of pivot points in ascending order, length must be at least 2.

    • ys: ArrayLike<number>

      The array of corresponding Y coordinates of pivot points.

    • n: number

      The number of pivot points, usually equals xs.length.

    • Optional splines: MutableArrayLike<number>

      Mutable array that would be populated with spline components, length must be at least 3 * n.

    Returns MutableArrayLike<number>

    The splines array.

  • Computes monotonous splines for xs and ys that prevents overshoot of interpolated values.

    Note: This function doesn't do any checks of arguments for performance reasons.

    const splines = createCSplinesMonot(xs, ys, xs.length);
    
    see

    Monotone cubic interpolation

    Parameters

    • xs: ArrayLike<number>

      The array of X coordinates of pivot points in ascending order, length must be at least 2.

    • ys: ArrayLike<number>

      The array of corresponding Y coordinates of pivot points.

    • n: number

      The number of pivot points, usually equals xs.length.

    • Optional splines: MutableArrayLike<number>

      Mutable array that would be populated with spline components, length must be at least 3 * n - 2.

    Returns MutableArrayLike<number>

    The splines array.

  • cspline(xs: ArrayLike<number>, ys: ArrayLike<number>): Interpolator
  • Returns a natural cubic spline interpolation function for given pivot points.

    Notes: Don't mutate xs and ys arrays after creating this function since data from these arrays is read during interpolation.

    const f = cspline(xs, ys);
    const y = f(x);

    Parameters

    • xs: ArrayLike<number>

      The array of X coordinates of pivot points in ascending order.

    • ys: ArrayLike<number>

      The array of corresponding Y coordinates of pivot points.

    Returns Interpolator

    The function that takes X coordinate and returns an interpolated Y coordinate.

  • csplineMonot(xs: ArrayLike<number>, ys: ArrayLike<number>): Interpolator
  • Returns a monotonous cubic spline interpolation function for given pivot points, that prevent overshoot of interpolated values.

    Notes: Don't mutate xs and ys arrays after creating this function since data from these arrays is read during interpolation.

    const f = csplineMonot(xs, ys);
    const y = f(x);
    see

    Monotone cubic interpolation

    Parameters

    • xs: ArrayLike<number>

      The array of X coordinates of pivot points in ascending order.

    • ys: ArrayLike<number>

      The array of corresponding Y coordinates of pivot points.

    Returns Interpolator

    The function that takes X coordinate and returns an interpolated Y coordinate.

  • cycle(x: number, a: number, b: number): number
  • Brings x to the range [a, b] by adding or subtracting the range size |a - b|.

    Range can be inverse.

    cycle(12, 0, 10) // → 2
    cycle(-333, 100, 33) // → 69
    see

    snap

    Parameters

    • x: number
    • a: number
    • b: number

    Returns number

  • deg(x: number): number
  • Converts degrees to radians.

    see

    {@rad}

    Parameters

    • x: number

    Returns number

  • desc<T>(a: T, b: T): number
  • Returns 1 if a < b, -1 if a > b and 0 otherwise.

    Type parameters

    • T

    Parameters

    • a: T
    • b: T

    Returns number

  • easeExp(t: number, q?: number): number
  • Maps t ∈ [0, 1] exponentially to [0, 1].

    Parameters

    • t: number

      The value to map.

    • q: number = 1

      Greater values produce more bent curve, f(t, 0) = t.

    Returns number

  • easeInCubic(t: number): number
  • easeInOutCubic(t: number): number
  • easeInOutQuad(t: number): number
  • easeInOutQuart(t: number): number
  • easeInOutQuint(t: number): number
  • easeInQuad(t: number): number
  • easeInQuart(t: number): number
  • easeInQuint(t: number): number
  • easeLog(t: number, q?: number): number
  • Maps t ∈ [0, 1] logarithmically to [0, 1].

    Parameters

    • t: number

      The value to map.

    • q: number = 1

      Greater values produce more bent curve, f(t, 0) = t.

    Returns number

  • easeOutCubic(t: number): number
  • easeOutQuad(t: number): number
  • easeOutQuart(t: number): number
  • easeOutQuint(t: number): number
  • flip(x: number, a1: number, b1: number, a2: number, b2: number): number
  • Flips x ∈ [a1, b1] to [a2, b2].

    Ranges can be inverse.

    Parameters

    • x: number
    • a1: number
    • b1: number
    • a2: number
    • b2: number

    Returns number

  • hypot(x: number, y: number): number
  • Returns the square root of the sum of squares of its arguments.

    Parameters

    • x: number
    • y: number

    Returns number

  • int(x: number): number
  • Converts number to a non-NaN large integer.

    see

    trunc

    Parameters

    • x: number

    Returns number

  • interpolateCSpline(xs: ArrayLike<number>, ys: ArrayLike<number>, x: number, n: number, splines: MutableArrayLike<number>): number
  • Interpolates y at x using a natural cubic spline algorithm for a set of pivot points.

    Note: This function doesn't do any checks of arguments for performance reasons.

    const y = interpolateCSpline(xs, ys, x, xs.length, createCSplines(xs, ys, xs.length));
    
    see

    createCSplines

    see

    Algorithm for computing natural cubic splines

    Parameters

    • xs: ArrayLike<number>

      The array of X coordinates of pivot points in ascending order, length must be at least 2.

    • ys: ArrayLike<number>

      The array of corresponding Y coordinates of pivot points.

    • x: number

      The X coordinate of interpolated point.

    • n: number

      The number of pivot points, usually equals xs.length.

    • splines: MutableArrayLike<number>

      The array of spline components, length must be 3 * n.

    Returns number

    Interpolated Y coordinate.

  • interpolateCSplineMonot(xs: ArrayLike<number>, ys: ArrayLike<number>, x: number, n: number, splines: MutableArrayLike<number>): number
  • Computes y at x for a set of pivot points (xs and ys) using monotonous cubic spline interpolation that prevents overshoot of interpolated values.

    Note: This function doesn't do any checks of arguments for performance reasons.

    const y = interpolateCSplineMonot(xs, ys, x, xs.length, createCSplinesMonot(xs, ys, xs.length));
    
    see

    Monotone cubic interpolation

    Parameters

    • xs: ArrayLike<number>

      The array of X coordinates of pivot points in ascending order, length must be al least 2.

    • ys: ArrayLike<number>

      The array of corresponding Y coordinates of pivot points.

    • x: number

      The X coordinate of interpolated point.

    • n: number

      The number of pivot points, usually equals xs.length.

    • splines: MutableArrayLike<number>

      The array of spline components, length must be 3 * n - 2.

    Returns number

    Interpolated Y coordinate.

  • isBetween(x: number, a: number, b: number): boolean
  • Returns true if x is in range [a, b].

    Range can be inverse.

    Parameters

    • x: number
    • a: number
    • b: number

    Returns boolean

  • isEpsClose(x: number, a: number, eps?: number): boolean
  • Returns true if x is in the eps neighbourhood of a.

    see

    closest

    Parameters

    • x: number
    • a: number
    • eps: number = 0.01

    Returns boolean

  • isNumeric<T>(x: T): x is NonNullable<T>
  • Returns true if x is not null and can be cast to a non-NaN number.

    isNumeric(1); // → true
    isNumeric('1'); // → true
    isNumeric('1a'); // → false
    isNumeric(null); // → false

    Type parameters

    • T

    Parameters

    • x: T

    Returns x is NonNullable<T>

  • left(x: number, shift: number): number
  • Bitwise left shift operator for large unsigned integers.

    left(0xAB, 8); // → 0xAB_00
    // or
    0xAB << 8;

    Parameters

    • x: number
    • shift: number

    Returns number

  • lerp(xs: ArrayLike<number>, ys: ArrayLike<number>): Interpolator
  • Returns a linear interpolation function for given pivot points.

    Notes: Don't mutate xs and ys arrays after creating this function since data from these arrays is read during interpolation.

    const f = lerp(xs, ys);
    const y = f(x);

    Parameters

    • xs: ArrayLike<number>

      The array of X coordinates of pivot points in ascending order.

    • ys: ArrayLike<number>

      The array of corresponding Y coordinates of pivot points.

    Returns Interpolator

    The function that takes X coordinate and returns an interpolated Y coordinate.

  • logx(x: number, n: number): number
  • Returns the logarithm of x with base n.

    logx(64, 2) // → 6
    logx(1000, 10) // → 3
    logx(99, 10) // → 1.99563519459755
    logx(0.01, 10) // → -2

    Parameters

    • x: number
    • n: number

    Returns number

  • or(a: number, b: number): number
  • Bitwise OR operator for large unsigned integers.

    Parameters

    • a: number
    • b: number

    Returns number

  • rad(x: number): number
  • range(n: number, a?: number, b?: number, easing?: Easing): number[]
  • range<T>(arr: T, n: number, a?: number, b?: number, easing?: Easing): T
  • Creates an array of length n and fills it with numbers in range [a, b].

    Parameters

    • n: number

      The array length.

    • Optional a: number

      The minimum value or 0 if omitted.

    • Optional b: number

      The maximum value or 1 if omitted.

    • Optional easing: Easing

      The easing function that receives t ∈ [0, 1] and return a mapped value.

    Returns number[]

    The array of numbers.

  • Populates first n elements of an array with numbers in range [a, b].

    Type parameters

    Parameters

    • arr: T

      The array to populate.

    • n: number

      The array length.

    • Optional a: number

      The minimum value.

    • Optional b: number

      The maximum value.

    • Optional easing: Easing

      The easing function that receives t ∈ [0, 1] and return a mapped value.

    Returns T

    The array of numbers.

  • right(x: number, shift: number): number
  • Bitwise right shift operator for large unsigned integers.

    right(0xAB_CD, 8); // → 0xAB
    // or
    0xAB_CD >> 8;

    Parameters

    • x: number
    • shift: number

    Returns number

  • sign(x: number): number
  • Returns either a positive or negative +/- 1, indicating the sign of a number passed into the argument. If the number passed into sign() is 0, it will return a +/- 0. Note that if the number is positive, an explicit (+) will not be returned.

    Parameters

    • x: number

    Returns number

  • snap(x: number, n: number): number
  • Rounds x to the closest value that is divided by n without any remainder.

    snap(17, 10) // → 20
    snap(-10, 3) // → -9
    see

    cycle

    Parameters

    • x: number
    • n: number

    Returns number

  • Sorts the array in-place using an optional comparator and invokes a callback after a pair of elements was swapped.

    swap or comparator callbacks are guaranteed to be called after the elements of arr are swapped.

    Type parameters

    Parameters

    • arr: T

      The mutable array-like data structure that is sorted in-place.

    • Optional swap: (i: number, j: number) => void

      The callback that is invoked with indices that were swapped.

        • (i: number, j: number): void
        • Parameters

          • i: number
          • j: number

          Returns void

    • Optional comparator: Comparator<ArrayElement<T>>

      The callback that defines the sort order. If omitted, the array elements are compared using comparison operators.

    Returns T

    The arr array.

  • sq(n: number): number
  • Semantic shortcut for n ** 2.

    Parameters

    • n: number

    Returns number

  • trunc(x: number): number
  • Returns the integer part of a number by removing any fractional digits.

    Parameters

    • x: number

    Returns number

  • uint(x: number): number
  • Converts number to a non-NaN large unsigned integer.

    see

    int

    Parameters

    • x: number

    Returns number

  • unNaN(x: number, n?: number): number
  • Returns x as is or n if x is NaN.

    Parameters

    • x: number
    • Optional n: number

    Returns number

  • xor(a: number, b: number): number
  • Bitwise XOR operator for large unsigned integers.

    Parameters

    • a: number
    • b: number

    Returns number

Generated using TypeDoc