• Public
  • Public/Protected
  • All

Class Util

Util provides various helper functions


  • Util



Static warnLevel

warnLevel: "error" | "warn" | "default" = "default"


Static combine

  • combine<T>(a: T[], b: T[], op: function): T[]
  • Given two arrays of object, and a function that operate on two object, return an array of T

    Type parameters

    • T


    • a: T[]

      an array of object, eg [ Group, Group, ... ]

    • b: T[]

      another array of object

    • op: function

      a function that takes two parameters (a, b) and returns a T

        • (a: T, b: T): T
        • Parameters

          • a: T
          • b: T

          Returns T

    Returns T[]

Static flatten

  • flatten(pts: any[], flattenAsGroup?: boolean): any
  • Flatten an array of arrays such as Group[] to a flat Array or Group


    • pts: any[]

      an array, usually an array of Groups

    • Default value flattenAsGroup: boolean = true

      a boolean to specify whether the return type should be a Group or Array. Default is true which returns a Group.

    Returns any

Static forRange

  • forRange(fn: function, range: number, start?: number, step?: number): any[]
  • A convenient way to step through a range. Same as for (i=0; i<range; i++), except this also stores the resulting return values at each step and return them as an array.


    • fn: function

      a callback function(index). If this function returns a value, it will be stored at each step

        • (index: number): any
        • Parameters

          • index: number

          Returns any

    • range: number

      a range to step through

    • Default value start: number = 0
    • Default value step: number = 1

    Returns any[]

    an array of returned values at each step

Static getArgs

  • getArgs(args: any[]): Array<number>
  • Convert different kinds of parameters (arguments, array, object) into an array of numbers


    • args: any[]

      a list of numbers, an array of number, or an object with {x,y,z,w} properties

    Returns Array<number>

Static randomInt

  • randomInt(range: number, start?: number): number
  • Parameters

    • range: number
    • Default value start: number = 0

    Returns number

Static split

  • split(pts: any[], size: number, stride?: number, loopBack?: boolean): any[][]
  • Split an array into chunks of sub-array


    • pts: any[]

      an array

    • size: number

      chunk size, ie, number of items in a chunk

    • Optional stride: number

      optional parameter to "walk through" the array in steps

    • Default value loopBack: boolean = false

      if true, always go through the array till the end and loop back to the beginning to complete the segments if needed

    Returns any[][]

Static stepper

  • stepper(max: number, min?: number, stride?: number, callback?: function): function
  • Create a convenient stepper. This returns a function which you can call repeatedly to step a counter.


    let counter = stepper(100); let c = counter(); c = counter(); ...


    • max: number

      Maximum of the stepper range. The resulting stepper will return (min to max-1) values.

    • Default value min: number = 0

      Minimum of the stepper range. Default is 0.

    • Default value stride: number = 1

      Stride of the step. Default is 1.

    • Optional callback: function

      An optional callback function( step ), which will be called each tiem when stepper function is called.

        • (n: number): void
        • Parameters

          • n: number

          Returns void

    Returns function

    a function which will increment the stepper and return its value at each call.

      • (): number
      • Returns number

Static warn

  • warn(message?: string, defaultReturn?: any): any
  • Send a warning message based on Util.warnLevel global setting. This allows you to dynamically set whether minor errors should be thrown or printed in console or muted.


    • Default value message: string = "error"

      any error or warning message

    • Default value defaultReturn: any = undefined

      optional return value

    Returns any

Static zip

  • zip(...arrays: Array<any>[]): any[]
  • Zip arrays. eg, [[1,2],[3,4],[5,6]] => [[1,3,5],[2,4,6]]


    • Rest ...arrays: Array<any>[]

      an array of arrays

    Returns any[]

Generated using TypeDoc