Namespace: Utils

GoBrain. Utils

A collection of useful util functions.

Namespaces

Base64

Methods

staticGoBrain.Utils.all(arr, func, callback)

GoBrain.Core/src/utils.js, line 220

Applies a function to each item in an array and only calls the callback if all of them succeeds.

Applies a function to each item in an array and only calls the callback if all of them succeeds.

Name Type Description
arr Array.<*>

Array of items to operate against.

func function

Function to be applied to each item in the array.

callback function

Callback to be executed once all operations have succeeded.

Throws:

staticGoBrain.Utils.argumentNames(func){Array.<String>}

GoBrain.Core/src/utils.js, line 250

Parses argument names for a function.

Parses argument names for a function.

Name Type Description
func function
Throws:

If argument is not a function.

Type
GoBrain.Exceptions.Argument
Returns:
Type Description
Array.<String> A list of argument names for the given function.

staticGoBrain.Utils.array(array){*|null}

GoBrain.Core/src/utils.js, line 648

Get first item from array, or null.

Get first item from array, or null.

Name Type Description
array Array.<*>
Returns:
Type Description
* | null The first item in the array, or null.

staticGoBrain.Utils.async(functions, callback)

GoBrain.Core/src/utils.js, line 273

Executes any number of asynchronous operations and invokes a callback once all of them are completed.

Executes any number of asynchronous operations and invokes a callback once all of them are completed.

Name Type Description
functions function repeatable

Any number of asynchronous functions to be executed.

callback function

Callback to be executed once all asynchronous operations have been completed.

Throws:

staticGoBrain.Utils.callEndpoint(endpoint, method, data, headers, timeoutInMS, onSuccess, onError, onTimeout, withCredentials)

GoBrain.Core/src/utils.js, line 302

Makes a function or HTTP call to an endpoint.

Makes a function or HTTP call to an endpoint.

Name Type Default Description
endpoint String | function

Endpoint to call.

method String GET optional

HTTP method to use.

data String {} optional

Data to send.

headers Object.<String, String> { 'Content-Type': 'application/json' } optional

Headers to send.

timeoutInMS Number {} optional

Timeout in milliseconds.

onSuccess function optional

Successful callback.

onError function optional

Failure callback.

onTimeout function optional

Timeout callback.

withCredentials Boolean optional

Set the XHR property withCredentials.

staticGoBrain.Utils.cancelFrame(id)

GoBrain.Core/src/utils.js, line 9

Cancels the scheduled execution of a function at the next available animation frame.

Cancels the scheduled execution of a function at the next available animation frame.

Name Type Description
id Number

The cancellable animation frame id.

staticGoBrain.Utils.createFunctionFromLambda(lambda){function}

GoBrain.Core/src/utils.js, line 465

Constructs a new function from a C# style lambda expression string.

Constructs a new function from a C# style lambda expression string.

Name Type Description
lambda String

Lambda expression to create function from.

Returns:
Type Description
function

staticGoBrain.Utils.createOrReplaceScript(script, src){HTMLElement}

GoBrain.Core/src/utils.js, line 508

If a script already exists with the same source then replaces that one, otherwise adds the new one.

If a script already exists with the same source then replaces that one, otherwise adds the new one.

Name Type Description
script HTMLElement

Script element.

src String

The source of the script.

Returns:
Type Description
HTMLElement The script element.

staticGoBrain.Utils.defer(func)

GoBrain.Core/src/utils.js, line 530

Defers execution of a function.

Defers execution of a function.

Name Type Description
func function
Throws:

If argument is not a function.

Type
GoBrain.Exceptions.Argument

staticGoBrain.Utils.endsWith(str, suffix){Boolean}

GoBrain.Core/src/utils.js, line 558

Checks if a string ends with another string.

Checks if a string ends with another string.

Name Type Description
str String

The string to search in.

suffix String

The suffix string to check for.

Returns:
Type Description
Boolean Whether the string ends with the suffix.

staticGoBrain.Utils.enumerate(obj, func){*}

GoBrain.Core/src/utils.js, line 574

Enumerates an object and applies a Function to each key value pair.

Enumerates an object and applies a Function to each key value pair.

Name Type Description
obj Object
func function
Returns:
Type Description
* Whatever is returned from the Function given as an argument.

staticGoBrain.Utils.exists(args){Boolean}

GoBrain.Core/src/utils.js, line 599

Checks if all given arguments are defined.

Checks if all given arguments are defined.

Name Type Description
args * repeatable
Returns:
Type Description
Boolean Whether all the args exist.

staticGoBrain.Utils.extend(args){Object}

GoBrain.Core/src/utils.js, line 614

Deep copy from right to left.

Deep copy from right to left.

Name Type Description
args Object repeatable
Returns:
Type Description
Object The extended base object.

staticGoBrain.Utils.guid(format){String}

GoBrain.Core/src/utils.js, line 663

Generates a GUID.

Generates a GUID.

Name Type Default Description
format String xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx optional
Returns:
Type Description
String The generated GUID.

staticGoBrain.Utils.inherits(child, parent){function}

GoBrain.Core/src/utils.js, line 678

Sets up child to inherit from parent.

Sets up child to inherit from parent.

Name Type Description
child function
parent function
Returns:
Type Description
function

staticGoBrain.Utils.isArray(arg){Boolean}

GoBrain.Core/src/utils.js, line 712

Checks if arg is an Array.

Checks if arg is an Array.

Name Type Description
arg *
Returns:
Type Description
Boolean Whether arg is an array.

staticGoBrain.Utils.isEmpty(arg){Boolean}

GoBrain.Core/src/utils.js, line 723

Checks if arg is empty.

Checks if arg is empty.

Name Type Description
arg *
Returns:
Type Description
Boolean

staticGoBrain.Utils.isFunction(arg){Boolean}

GoBrain.Core/src/utils.js, line 741

Checks if arg is a Function.

Checks if arg is a Function.

Name Type Description
arg *
Returns:
Type Description
Boolean Whether arg is a function.

staticGoBrain.Utils.isObject(arg){Boolean}

GoBrain.Core/src/utils.js, line 752

Checks if arg is an Object.

Checks if arg is an Object.

Name Type Description
arg *
Returns:
Type Description
Boolean Whether arg is an object.

staticGoBrain.Utils.loadScript(url, callback, track){HTMLElement}

GoBrain.Core/src/utils.js, line 763

Loads a script and calls callback when script is loaded.

Loads a script and calls callback when script is loaded.

Name Type Default Description
url String
callback function optional
track Boolean true optional

Whether or not to track script for later removal.

Returns:
Type Description
HTMLElement The script element. If script is being tracked and has already been requested then null will be returned.

staticGoBrain.Utils.loadScriptArray(scriptArray, callback, baseUrl)

GoBrain.Core/src/utils.js, line 834

Loads an array of scripts and calls callback once all scripts are loaded.

Loads an array of scripts and calls callback once all scripts are loaded.

Name Type Description
scriptArray Array.<String>

Array of scripts to load.

callback optional

Callback to be called once all scripts are loaded.

baseUrl String optional

Base url to use for all scripts.

staticGoBrain.Utils.loadStyle(url, callback){HTMLElement}

GoBrain.Core/src/utils.js, line 856

Loads a css and calls callback when css is loaded.

Loads a css and calls callback when css is loaded.

Name Type Description
url String
callback function optional
Returns:
Type Description
HTMLElement The style element.

staticGoBrain.Utils.normalize(str){String|null}

GoBrain.Core/src/utils.js, line 904

Normalizes a String.

Normalizes a String.

Name Type Description
str String
Returns:
Type Description
String | null The normalized string.

staticGoBrain.Utils.notNullOrEmpty(str){Boolean}

GoBrain.Core/src/utils.js, line 919

Checks that a String is not undefined, not null and not empty.

Checks that a String is not undefined, not null and not empty.

Name Type Description
str String
Returns:
Type Description
Boolean Whether arg is not null or empty.

staticGoBrain.Utils.once(func){function}

GoBrain.Core/src/utils.js, line 930

Returns a wrapper Function around func that ensures func is only called once, no matter how many times the wrapper Function is called.

Returns a wrapper Function around func that ensures func is only called once, no matter how many times the wrapper Function is called.

Name Type Description
func function
Returns:
Type Description
function The wrapper function.

staticGoBrain.Utils.padLeft(str, length, char){String}

GoBrain.Core/src/utils.js, line 944

Pads a string from the left.

Pads a string from the left.

Name Type Description
str String

String to be padded.

length Number

Desired length of string.

char String

Character to use for padding.

Returns:
Type Description
String The padded string.

staticGoBrain.Utils.requestFrame(func){Number}

GoBrain.Core/src/utils.js, line 22

Requests that a function will be executed at the next available animation frame.

Requests that a function will be executed at the next available animation frame.

Name Type Description
func function
Returns:
Type Description
Number A cancellable animation frame id.

staticGoBrain.Utils.resolveUrl(args){String}

GoBrain.Core/src/utils.js, line 965

Resolves a url given parts of it.

Resolves a url given parts of it.

Name Type Description
args String repeatable

Parts of url.

Returns:
Type Description
String The resolved url.

staticGoBrain.Utils.seq(arr, func, callback)

GoBrain.Core/src/utils.js, line 1003

Applies a function to each item in an array sequentially/synchronously and invokes the callback once all of them are completed.

Applies a function to each item in an array sequentially/synchronously and invokes the callback once all of them are completed.

Name Type Description
arr Array.<*>

Array of items to operate against.

func function

Function to be applied to each item in the array.

callback function

Callback to be executed once all operations have been completed.

Throws:

staticGoBrain.Utils.setQueryStringParameter(url, key, value){String}

GoBrain.Core/src/utils.js, line 1037

Adds or updates a query string parameter in a URL.

Adds or updates a query string parameter in a URL.

Name Type Description
url String

URL to set query string parameter in.

key String

The query string parameter key.

value String

The query string parameter value.

Returns:
Type Description
String The updated URL.

staticGoBrain.Utils.stringFormat(pattern, args){String}

GoBrain.Core/src/utils.js, line 1052

Uses replacement tokens to format a string.

Uses replacement tokens to format a string. The replacement tokens can be either named or positional.

Name Type Description
pattern String

Pattern containing replacement tokens.

args Object | *

Values to replace replacement tokens in pattern with. Can be an object containing all values or any number of value arguments if using positional replacement.

Returns:
Type Description
String The formatted string.

staticGoBrain.Utils.values(obj){Array.<*>}

GoBrain.Core/src/utils.js, line 1100

Gets all the values of an object.

Gets all the values of an object.

Name Type Description
obj Object

The object to get the values from.

Returns:
Type Description
Array.<*> The object's values.