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 Promise s. | |
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 . |
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.
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.
onSuccess | Function|Undefined callback |
params | Any|Undefined callback argument |
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
.
onFailure | Function|Undefined callback to handle the specified Error |
e | Error error to pass to callback or throw |
Any
res
parameter Undefined utils::checkProperties | ( | Object | obj, |
Array | props | ||
) |
Checks whether specified object has all specified properties.
obj | Object input object |
props | Array list of properties names |
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.
obj | Object input object |
prop | String property name |
type | String property type name |
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.
mod | Object RequireJS Module object |
expected | String expected module name |
Undefined
Object utils::cloneObject | ( | Object | obj | ) |
Permforms deep clone of the "plain" object converting it to JSON and returning the parsed result.
obj | Object object to clone |
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
data | Any input value |
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
obj | Object object to check |
Object
specified object, if it is not-null, emtpy object otherwise String utils::defaultString | ( | Any | str | ) |
Converts specified value to string.
str | Any input value |
String
specified value, if it is a string, value converted to string otherwise; empty string on null
input Boolean utils::endsWith | ( | String | str, |
String | postfix | ||
) |
Checks whether specified string ends with specified postfix.
str | String string to check |
postfix | String postfix to check |
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.
e | Error error object |
String
formatted error Boolean utils::hasProperties | ( | Object | obj, |
Array | props | ||
) |
Checks whether specified object has all specified properties.
obj | Object input object |
props | Array list of properties names |
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.
obj | Object input object |
prop | String property name |
type | String property type name |
Boolean
true
on successful check, false
otherwise Array utils::listProperties | ( | Object | obj | ) |
Lists the names of object's own properties.
obj | Object input object |
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.
str1 | String first string to compare |
str2 | String second string to compare |
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.
obj | Object module object that has Function properties |
Object
input object with additionally added "promisified" functions. Boolean utils::startsWith | ( | String | str, |
String | prefix | ||
) |
Checks whether specified string starts with specified prefix.
str | String string to check |
prefix | String prefix to check |
Boolean
true
if specified string starts with specified prefix, false
otherwise Boolean utils::undefinedOrNull | ( | Any | obj | ) |
Checks whether specifed value is undefined
or null
.
obj | Any |
Boolean
true
if specifed value is undefined
or null
, false
otherwise