Module: util

Classes

Clock
Color
CountdownTimer
EventEmitter
Logger
MixinBuilder
MonotonicClock
PsychObject
Scheduler

Mixins

ColorMixin

Methods

(static) addInfoFromUrl(info)

Add info extracted from the URL to the given dictionary.

We exclude all URL parameters starting with a double underscore since those are reserved for client/server communication

Parameters:
Name Type Description
info Object the dictionary
Source:

(static) detectBrowser() → {string}

Detect the user's browser.

Note: since user agent is easily spoofed, we use a more sophisticated approach, as described here: https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser

Source:
Returns:
the detected browser, one of 'Opera', 'Firefox', 'Safari', 'IE', 'Edge', 'Chrome', 'Blink", 'unknown'
Type
string

(static) flattenArray(array) → {Array.<Object>}

Recursively flatten an array of arrays.
Parameters:
Name Type Description
array Array.<Object> the input array of arrays
Source:
Returns:
the flatten array
Type
Array.<Object>

(static) getErrorStack() → {string}

Get the error stack of the calling, exception-throwing function.
Source:
Returns:
the error stack as a string
Type
string

(static) getPositionFromObject(object, units) → {Array.<number>}

Get the position of the object in pixel units
Parameters:
Name Type Description
object Object the input object
units string the units
Source:
Returns:
the position of the object in pixel units
Type
Array.<number>

(static) getUrlParameters() → {URLSearchParams}

Get the URL parameters.
Source:
Returns:
the iterable URLSearchParams
Type
URLSearchParams
Example
const urlParameters = util.getUrlParameters();
for (const [key, value] of urlParameters)
  console.log(key + ' = ' + value);

(static) isEmpty(x) → {boolean}

Test if x is an 'empty' value.
Parameters:
Name Type Description
x Object the value to test
Source:
Returns:
true if x is one of the following: undefined, [], [undefined]
Type
boolean

(static) isInt(obj) → {boolean}

Test whether an object is either an integer or the string representation of an integer.

This is adapted from: https://stackoverflow.com/a/14794066

Parameters:
Name Type Description
obj Object the input object
Source:
Returns:
whether or not the object is an integer or the string representation of an integer
Type
boolean

(static) IsPointInsidePolygon(point, vertices) → {boolean}

Check whether a point lies within a polygon

We are using the algorithm described here: https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html

Parameters:
Name Type Description
point Array.<number> the point
vertices Object the vertices defining the polygon
Source:
Returns:
whether or not the point lies within the polygon
Type
boolean

(static) makeUuid() → {string}

Get a Universally Unique Identifier (RFC4122 version 4)

See details here: https://www.ietf.org/rfc/rfc4122.txt

Source:
Returns:
the uuid
Type
string

(static) promiseToTupple(promise) → {Array.<Object>}

Convert the resulting value of a promise into a tupple.
Parameters:
Name Type Description
promise Promise the promise
Source:
Returns:
the resulting value in the format [error, return data] where error is null if there was no error
Type
Array.<Object>

(static) selectFromArray(array, selection) → {Object|Array.<Object>}

Select values from an array.

'selection' can be a single integer, an array of indices, or a string to be parsed, e.g.:

  • 5
  • [1,2,3,10]
  • '1,5,10'
  • '1:2:5'
  • '5:'
  • '-5:-2, 9, 11:5:22'

Parameters:
Name Type Description
array Array.<Object> the input array
selection number | Array.<number> | string the selection
Source:
Returns:
the array of selected items
Type
Object | Array.<Object>

(static) shuffle(array) → {Array.<Object>}

Shuffle an array in place using the Fisher-Yastes's modern algorithm

See details here: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm

Parameters:
Name Type Description
array Array.<Object> the input 1-D array
Source:
Returns:
the shuffled array
Type
Array.<Object>

(static) sliceArray(array, fromopt, toopt, stepopt) → {Array.<Object>}

Slice an array.
Parameters:
Name Type Attributes Default Description
array Array.<Object> the input array
from number <optional>
NaN the start of the slice
to number <optional>
NaN the end of the slice
step number <optional>
NaN the step of the slice
Source:
Returns:
the array slice
Type
Array.<Object>

(static) to_height(pos, posUnit, win) → {Array.<number>}

Convert the position to height units.
Parameters:
Name Type Description
pos Array.<number> the input position
posUnit string the position units
win Window the associated Window
Source:
Returns:
the position in height units
Type
Array.<number>

(static) to_norm(pos, posUnit, win) → {Array.<number>}

Convert the position to norm units.
Parameters:
Name Type Description
pos Array.<number> the input position
posUnit string the position units
win Window the associated Window
Source:
Returns:
the position in norm units
Type
Array.<number>

(static) to_pixiPoint(pos, posUnit, win) → {Array.<number>}

Convert a position to a PIXI Point.
Parameters:
Name Type Description
pos Array.<number> the input position
posUnit string the position units
win Window the associated Window
Source:
Returns:
the position as a PIXI Point
Type
Array.<number>

(static) to_px(pos, posUnit, win) → {Array.<number>}

Convert the position to pixel units.
Parameters:
Name Type Description
pos Array.<number> the input position
posUnit string the position units
win Window the associated Window
Source:
Returns:
the position in pixel units
Type
Array.<number>

(static) to_unit(pos, posUnit, win, targetUnit) → {Array.<number>}

Convert the position to given units.
Parameters:
Name Type Description
pos Array.<number> the input position
posUnit string the position units
win Window the associated Window
targetUnit string the target units
Source:
Returns:
the position in target units
Type
Array.<number>

(static) to_win(pos, posUnit, win) → {Array.<number>}

Convert the position to window units.
Parameters:
Name Type Description
pos Array.<number> the input position
posUnit string the position units
win Window the associated Window
Source:
Returns:
the position in window units
Type
Array.<number>

(static) toNumerical(obj) → {number|Array.<number>}

Convert obj to its numerical form.
  • number -> number, e.g. 2 -> 2
  • [number] -> [number], e.g. [1,2,3] -> [1,2,3]
  • numeral string -> number, e.g. "8" -> 8
  • [number | numeral string] -> [number], e.g. [1, 2, "3"] -> [1,2,3]
Parameters:
Name Type Description
obj Object the input object
Source:
Returns:
the numerical form of the input object
Type
number | Array.<number>

(static) toString(object) → {string}

Convert an object to its string representation, taking care of symbols.

Note: if the object is not already a string, we JSON stringify it and detect circularity.

Parameters:
Name Type Description
object Object the input object
Source:
Returns:
a string representation of the object or 'Object (circular)'
Type
string