wilton.js  v202103141
 All Namespaces Functions
Functions
utils Namespace Reference

wilton/utils
Utility functions. More...

Functions

Any callOrIgnore (Function|Undefined onSuccess, Any|Undefined params)
 Call specified function (if it is not Undefined).
Any callOrThrow (Function|Undefined onFailure, Error e)
 Call specified callback or throw specified Error if callback is Undefined.
Undefined checkProperties (Object obj, Array props)
 Check whether object has all specified properties.
Undefined checkPropertyType (Object obj, String prop, String type)
 Check whether object has a specified property with a specified type.
Undefined checkRootModuleName (Object mod, String expected)
 Check that root module name is configured as expected.
Object cloneObject (Object obj)
 Deep clone object.
String defaultJson (Any data)
 Convert specified value to JSON.
Object defaultObject (Object obj)
 Return object on any input.
String defaultString (Any str)
 Return string on any input.
Boolean endsWith (String str, String postfix)
 Check whether specified string ends with specified postfix.
String formatError (Error e)
 Format specifed error.
Boolean hasProperties (Object obj, Array props)
 Check whether object has all specified properties.
Boolean hasPropertyWithType (Object obj, String prop, String type)
 Check whether object has a specified property with a specified type.
Array listProperties (Object obj)
 List properties names of the specified object.
Number localeComparator (String str1, String str2)
 Compare two strings using String.prototype.localeCompare()
Object promisifyAll (Object obj)
 Convert all function in the specified module object to use Promises.
Boolean startsWith (String str, String prefix)
 Check whether specified string starts with specified prefix.
Boolean undefinedOrNull (Any obj)
 Check whether specifed value is undefined or null.

Detailed Description

This module contains a set of utility function that are used by wilton.js internally.

It is suggested to use Lodash library (that is shipped with wilton distribution) instead of this module for the general-use utilities.

Function Documentation

Any utils::callOrIgnore ( Function|Undefined  onSuccess,
Any|Undefined  params 
)

If callback function is specified - calls it passing null as a first argument (to adhere with Node's callback conventions) and specified params value as a second argument.

Parameters
onSuccessFunction|Undefined callback
paramsAny|Undefined callback argument
Returns
Any result of onSuccess call (if it is specified); params (if it is specified, but onSuccess - not); undefined otherwise
Any utils::callOrThrow ( Function|Undefined  onFailure,
Error  e 
)

If function callback is specified, calls it providing specified Error as argument. Otherewise throws specified Error.

Parameters
onFailureFunction|Undefined callback to handle the specified Error
eError error to pass to callback or throw
Returns
Any res parameter
Undefined utils::checkProperties ( Object  obj,
Array  props 
)

Checks whether specified object has all specified properties.

Parameters
objObject input object
propsArray list of properties names
Returns
Undefined throws Error on check fail
Undefined utils::checkPropertyType ( Object  obj,
String  prop,
String  type 
)

Checks whether object has a specified property with a specified type.

Parameters
objObject input object
propString property name
typeString property type name
Returns
Undefined throws Error on check fail
Undefined utils::checkRootModuleName ( Object  mod,
String  expected 
)

Checks that root module name is configured as expected. Throws an Error if module name does not match.

Parameters
modObject RequireJS Module object
expectedString expected module name
Returns
Undefined
Object utils::cloneObject ( Object  obj)

Permforms deep clone of the "plain" object converting it to JSON and returning the parsed result.

Parameters
objObject object to clone
Returns
Object cloned object
String utils::defaultJson ( Any  data)

If specified value is an object - converts it to JSON, if it is a string - returns it unchanged

Parameters
dataAny input value
Returns
String JSON string, empty JSON object (as a string) on null input
Object utils::defaultObject ( Object  obj)

Returns specified object, if it is not-null, returns emtpy object otherwise

Parameters
objObject object to check
Returns
Object specified object, if it is not-null, emtpy object otherwise
String utils::defaultString ( Any  str)

Converts specified value to string.

Parameters
strAny input value
Returns
String specified value, if it is a string, value converted to string otherwise; empty string on nullinput
Boolean utils::endsWith ( String  str,
String  postfix 
)

Checks whether specified string ends with specified postfix.

Parameters
strString string to check
postfixString postfix to check
Returns
Boolean true if specified string ends with specified postfix, false otherwise
String utils::formatError ( Error  e)

Formats specified Error object into string, including error message and a stack trace.

Parameters
eError error object
Returns
String formatted error
Boolean utils::hasProperties ( Object  obj,
Array  props 
)

Checks whether specified object has all specified properties.

Parameters
objObject input object
propsArray list of properties names
Returns
Boolean true on successful check, false otherwise
Boolean utils::hasPropertyWithType ( Object  obj,
String  prop,
String  type 
)

Checks whether object has a specified property with a specified type.

Parameters
objObject input object
propString property name
typeString property type name
Returns
Boolean true on successful check, false otherwise
Array utils::listProperties ( Object  obj)

Lists the names of object's own properties.

Parameters
objObject input object
Returns
Array list of properties names
Number utils::localeComparator ( String  str1,
String  str2 
)

Compares two strings using String.prototype.localeCompare().

Non-string input values are coalesced to strings.

Null and undefined input values are colalesced to empty string.

Parameters
str1String first string to compare
str2String second string to compare
Returns
Number a negative number if the str1 occurs before str2; positive if the str1 occurs after str2; 0 if str1 and str2 are equivalent
Object utils::promisifyAll ( Object  obj)

Applies bluebird promisifyAll to the specified object using Promise as a postfix for "promisified" functions.

Parameters
objObject module object that has Function properties
Returns
Object input object with additionally added "promisified" functions.
Boolean utils::startsWith ( String  str,
String  prefix 
)

Checks whether specified string starts with specified prefix.

Parameters
strString string to check
prefixString prefix to check
Returns
Boolean true if specified string starts with specified prefix, false otherwise
Boolean utils::undefinedOrNull ( Any  obj)

Checks whether specifed value is undefined or null.

Parameters
objAny
Returns
Boolean true if specifed value is undefined or null, false otherwise