Module: caf_components/myUtils

Miscellaneous collection of functions.

Source:

Methods

callJustOnce(errFopt, cb)

Ensures that cb is only called once. If not, and optional errF callback is called.

Parameters:
Name Type Attributes Description
errF cbType <optional>

An optional callback for any extra calls to cb.

cb cbType

A callback that should only be called once.

Source:

cloneAndMixin(dest, source, keepOld) → {Object}

Clones an object before mixin some properties from a source object.

Parameters:
Name Type Description
dest Object

A target object to patch.

source Object

A simple object with deltas.

keepOld boolean

True if we can silently ignore changes to already defined methods, false if we allow changing existing ones.

Source:
Returns:

A cloned and patched dest.

Type
Object

deepClone(obj, filter) → {Object|Array|number|null|string|boolean}

Deep clones nested arrays and objects (e.g., JSON like collections)

Parameters:
Name Type Description
obj Object | Array | number | null | string | boolean

Structure to deep clone.

filter function

An optional filter of type function(string): boolean to ignore certain keys in an object.

Source:
Returns:

A deeply cloned structure.

Type
Object | Array | number | null | string | boolean

deepEqual(x, y) → {boolean}

Whether two objects are structurally similar.

Parameters:
Name Type Description
x Object

An object to compare.

y Object

An object to compare.

Source:
Returns:

True if x and y are structurally similar.

Type
boolean

errToPrettyStr(err) → {string}

Stringifies an error object so that its stack is properly formatted in the console.

Parameters:
Name Type Description
err Error

An error object to log or send.

Source:
Returns:

A string representation of the error.

Type
string

errToStr(err) → {string}

Stringifies an error object for logging or network transmission.

Parameters:
Name Type Description
err Error

An error object to log or send.

Source:
Returns:

A string representation of the error.

Type
string

extractData(tuple) → {any}

Extracts the data field from a tuple [error, data].

Throws error if not null

Parameters:
Name Type Description
tuple Array

An [error, data] pair.

Source:
Throws:

if the error field is not a falsy.

Type
Error
Returns:

The data field.

Type
any

hashCode(st) → {number}

Computes a hash code for a string.

Parameters:
Name Type Description
st string

An input string.

Source:
Returns:

An unsigned 32 bit integer representing a hash code for the string.

Type
number

mixin(dest, source, keepOld) → {Object}

Merges properties defined in a source map into a destination object.

Parameters:
Name Type Description
dest Object

A target object to patch.

source Object

A simple object with deltas.

keepOld boolean

True if we can silently ignore changes to already defined methods, false if we allow changing existing ones.

Source:
Returns:

dest after patching.

Type
Object

(static) clone(obj) → {Object}

Clones arrays or objects.

Parameters:
Name Type Description
obj Object

An object to be cloned.

Source:
Returns:

A clone of obj.

Type
Object

onlyFun(obj) → {Object}

Filters keys in an object that are inherited or do not have a function value.

Parameters:
Name Type Description
obj Object

An object to filter.

Source:
Returns:

An object with only methods.

Type
Object

promiseToCallback(promise, cb, wrapException, wrapAppError)

Maps a promise to a callback.

Assumes that the promise resolves to a [error, value] array, compatible with an standard node.js callback.

It does nothing if the promise is missing.

If it has a non-null value but is not a promise object, or resolves to a non-array object, or an array with length greater than 2, it will propagate an error in the callback.

An optional wrapException function can modify that error, e.g., to distinguish them from standard application errors propagated in the array.

Parameters:
Name Type Description
promise Promise

A promise to be mapped into a callback

cb cbType

A callback to propagate the resolved promise values.

wrapException function

An optional function to wrap non-propagated errors.

wrapAppError function

An optional function to wrap application errors propagated in the first element of the returned array.

Source:

retryWithDelay(f, nTimes, delay, cb)

Retries an asynchronous function several times until it succeeds. It delays a retry by a fixed amount of time.

Parameters:
Name Type Description
f function

An asynchronous function of type function(cbType) to be evaluated. It returns error/result using a callback with node.js conventions.

nTimes number

Max number of attempts.

delay number

Time between retries in miliseconds.

cb cbType

Standard callback function for error/result propagation.

Source:

superior(target, methodName) → {function}

Captures in a closure a method of the parent class before we override it.

For example:

var supHello = myUtils.superior(that, 'hello');
that.hello = function() {
   supHello(); // calls original 'hello'
   // do something else
}
Parameters:
Name Type Description
target Object

An object to capture a method from.

methodName string

The name of the method that we want to override.

Source:
Returns:

The function implementing that method in the parent class.

Type
function

superiorPromisify(target, methodName) → {function}

Captures in a closure a method of the parent class before we override it. It also transforms a callback based function into one that returns a promise.

For example:

var supHello = myUtils.superiorPromisify(that, 'hello');
that.hello = async function() {
   try {
      var data = await supHello(); // calls original 'hello'
   // do something else
   } catch (err) {
      ...
}
Parameters:
Name Type Description
target Object

An object to capture a method from.

methodName string

The name of the method that we want to override.

Source:
Returns:

The function implementing that method in the parent class, modified to return a promise.

Type
function

uniqueId() → {string}

Returns a unique identifier.

Source:
Returns:

A unique identifier.

Type
string

wrapAsyncFunction(fopt, targetopt) → {function}

Wraps an asynchronous function.

The goal is to make it behave the same whether it uses a callback or returns a promise, e.g, uses the async/await pattern.

Assumes that the promise resolves to a [error, value] array, compatible with an standard node.js callback.

If the function throws, and the exception was captured in the promise, we just rethrow the exception, so that it behaves like a callback based async function.

It does nothing if the function is missing. It throws if called without a callback or something that is not a function.

Parameters:
Name Type Attributes Description
f function <optional>

An asynchronous function to be wrapped.

target Object <optional>

An optional target object when the function is one of its methods. It defaults to f.

Source:
Throws:

if f is provided but is not a function, or we call the wrapped function without a callback.

Type
Error
Returns:

A wrapped function that behaves the same whether f is callback or promise based.

Type
function

wrapWithTimeout(f, timeoutopt) → {function}

Wraps an asynchronous function to limit its duration.

Parameters:
Name Type Attributes Description
f function

An asynchronous function to be wrapped.

timeout number <optional>

A maximum duration for the call (in msec).

Source:
Returns:

A wrapped function that will return an error in the callback if timeout expires. The error has a field timeout set to true.

Type
function