Utility()

A group of commonly used utility functions

new Utility()

Methods

static checkInstance(fnName, valueName, value, cls)

Checks the type of a variable and throws if it is the wrong type

Parameters:
Name Type Description
fnName string

Name of the calling function (for cosmetic purposes)

valueName string

Name of the function being checked (for cosmetic purposes)

value *

The object to check

cls string

The expected type of the variable as reported by typeof

Throws:

If value is not an object or not an instanceof cls

Type
TypeError

static checkType(fnName, valueName, value, type)

Checks the type of a variable and throws if it is the wrong type

Parameters:
Name Type Description
fnName string

Name of the calling function (for cosmetic purposes)

valueName string

Name of the function being checked (for cosmetic purposes)

value *

The variable to check

type string

The expected type of the variable as reported by typeof

Throws:

If value is not typeof type

Type
TypeError

static createHiddenProp(obj, prop, val, roopt)

Creates a new property on an object that is hidden (non-enumerable) and optionally read-only. Syntactic sugar around Object.defineProperty to help with readability.

Parameters:
Name Type Attributes Default Description
obj object

The object to create the new property on

prop string

The name of the new property

val *

The value for the new property

ro boolean <optional>
false

only (can't be written to). false if not specified.

async, static delay(ms) → {Promise}

Async version of setTimeout. To be replaced by timers/promises someday.

Parameters:
Name Type Description
ms number

Number of milliseconds to delay. Passed to setTimeout.

Returns:
Promise -

A Promise that resolves to undefined after the specified number of milliseconds has passed.

static randomFloat(minopt, maxopt) → {number}

Returns a random integer <= min and >= max

Parameters:
Name Type Attributes Default Description
min number <optional>
0

Minimum number to return

max number <optional>
1

Maximum number to return

Returns:
number -

A random float

static randomInt(minopt, maxopt) → {number}

Returns a random integer <= min and >= max

Parameters:
Name Type Attributes Default Description
min number <optional>
0

Minimum number to return

max number <optional>
(2**31)-1

Maximum number to return

Returns:
number -

A random integer

static randomSeed(seed)

Seed the pseudo-random number generator (PRNG). Note that the sequence of random numbers will always be the same for the same seed.

Parameters:
Name Type Description
seed *

Takes any type and uses it as a seed for thte random number generator. undefined or null will create a non-deterministic sequence of numbers.

static resolveFileOrString(str, opts) → {string}

Resolves a string to a filename and loads that file, or simply returns the string if it doesn't resolve to the file.

Parameters:
Name Type Description
str string

A filename or string literal

opts Object

Options for the resolution. opts.basename specifies a folder of where to look for the files. opts.ext specifies a file extension (e.g. .html) to append in looking for the file.

Returns:
string -

Returns the contents of the file if resolved to a file; otherwise, returns the original string