Module: restUtil

A Utility object containing logic independent of making REST calls


restUtil Methods

(static) -> isEmpty(obj)

Returns true if an object is empty

Parameters:
Name Required Type Default Value Description
obj Y Object   object to check

Returns:
Type Description
boolean true if the object is null or empty

(static) -> isValidTextInput(text)

Test the validity of a string for use in a URI.

Parameters:
Name Required Type Default Value Description
text Y string   The text to be validated

Returns:
Type Description
boolean A boolean indicating the string is valid for use in a URI.

Examples:

// an instance of restUtil will be made available via a mixin in every rest worker as this.restUtil
this.logger.info("isValidTextInput(/..):"+this.restUtil.isValidTextInput("/.."));
// expect false
this.logger.info("isValidTextInput(/..):"+this.restUtil.isValidTextInput("/.."));
// expect false

(static) -> getNowMicrosUtc()

Returns the number of microseconds since the unix epoch (January 1, 1970).

Returns:
Type Description
number The number of microseconds since January 1, 1970.

Examples:

MyWorker.prototype.onStart = function(success) {
  this.state.lastUpdateMicros = this.restUtil.getNowMicrosUtc();
}

(static) -> extend(target, base)

Extend a target object’s properties with that of a base object. In the case of duplicate properties, the target object will retain it’s value - unlike the Object.assign() call. Supports aspect programming.

Parameters:
Name Required Type Default Value Description
target Y Object   The object to be modified with the additional properties.
base Y Object   The object whose properties are to be added to the target.

Examples:

base.start$before ()-> say '1'
base.start$after ()-> say '3'
target.start ()-> say '2'

before:
target.start() : say '2'

after:
restUtil.extend(target, base);
target.start() :
             say '1'
             say '2'
             say '3'

(static) -> contains(arry, func)

check if an elem is contains in array by running a boolean method against each element, returning index of first match (first time the function returned true)

Parameters:
Name Required Type Default Value Description
arry Y array   Input array to be searched.
func Y function   Function to validate an array element.

Returns:
Type Description
number The first match position or -1 if no match.

(static) -> deepCopy(obj)

Performs a deep copy via serialization of an object.

__Caveat:__ this method is not compatible with recursive data structures, and in specific, with RestOperation

Parameters:
Name Required Type Default Value Description
obj Y object   The instance object to clone.

Returns:
Type Description
object A deep copy of object.

Examples:

var o1 = { a: "hello"};
var src1 = { x: "x", o: o1 }
var dest1 = this.restUtil.deepCopy(src1);
this.logger.info(src1);// log { x: "x", o : { a: "hello" } }
this.logger.info(dest);// log the same: { x: "x", o : { a: "hello" } }