Module: util

Classes

Clock
Color
CountdownTimer
EventEmitter
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/a/9851769

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

(static) extensionFromMimeType(mimeType) → {string}

Return the file extension corresponding to an audio mime type. If the provided mimeType is not a string (e.g. null, undefined, an array) or unknown, then '.dat' is returned, instead of throwing an exception.
Parameters:
Name Type Description
mimeType string the MIME type, e.g. 'audio/webm;codecs=opus'
Source:
Returns:
the corresponding file extension, e.g. '.webm'
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) getRequestError(jqXHR, textStatus, errorThrown)

Get the most informative error from the server response from a jquery server request.
Parameters:
Name Type Description
jqXHR
textStatus
errorThrown
Source:

(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) offerDataForDownload(filename, data, type)

Offer data as download in the browser.
Parameters:
Name Type Description
filename string the name of the file to be downloaded
data * the data
type string the MIME type of the data, e.g. 'text/csv' or 'application/json'
Source:

(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) randint(minopt, max) → {number}

Generates random integers a-la NumPy's in the "half-open" interval [min, max). In other words, from min inclusive to max exclusive. When max is undefined, as is the case by default, results are chosen from [0, min). An error is thrown if max is less than min.
Parameters:
Name Type Attributes Default Description
min number <optional>
0 lowest integer to be drawn, or highest plus one if max is undefined (default)
max number one above the largest integer to be drawn
Source:
Returns:
a random integer in the requested range (signed)
Type
number

(static) round(input, places) → {number}

Round to a certain number of decimal places. This is the Crib Sheet provided solution, but please note that as of 2020 the most popular SO answer is different.
Parameters:
Name Type Description
input number the number to be rounded
places number the max number of decimals desired
Source:
See:
Returns:
input rounded to the specified number of decimal places at most
Type
number

(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) sum(inputMaybe) → {number}

Calculate a total for all numeric input array elements.
Parameters:
Name Type Description
inputMaybe array a list of numbers to add up
Source:
Returns:
numeric input entries added up
Type
number

(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, integerCoordinatesopt) → {Array.<number>}

Convert a position to a PIXI Point.
Parameters:
Name Type Attributes Default Description
pos Array.<number> the input position
posUnit string the position units
win Window the associated Window
integerCoordinates boolean <optional>
false whether or not to round the PIXI Point coordinates.
Source:
Returns:
the position as a PIXI Point
Type
Array.<number>

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

Convert the position to pixel units.
Parameters:
Name Type Attributes Default Description
pos Array.<number> the input position
posUnit string the position units
win Window the associated Window
integerCoordinates boolean <optional>
false whether or not to round the position coordinates.
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

(static) turnSquareBracketsIntoArrays(input, max) → {array}

Convert a string representing a JSON array, e.g. "[1, 2]" into an array, e.g. ["1","2"]. This approach overcomes the built-in JSON parsing limitations when it comes to eg. floats missing the naught prefix, and is able to process several arrays, e.g. "[1,2][3,4]".
Parameters:
Name Type Description
input string string potentially containing JSON arrays
max string how many matches to return, unwrap resulting array if less than two
Source:
Returns:
an array if arrays were found, undefined otherwise
Type
array