Module: Utilities

Collection of utilities.

Source:
index.js

Members


<static> _ :lodash

A utility-belt library for JavaScript that provides support for the usual functional suspects (each, map, reduce, filter...) without extending any core JavaScript objects.
See https://lodash.com

Type:
  • lodash
Source:
index.js

<static> async :async

Higher-order functions and common patterns for asynchronous code.
See http://caolan.github.io/async

Type:
  • async
Source:
index.js

<static> fs :fs

Methods that aren't included in the native fs module and adds promise support to the fs methods. It should be a drop in replacement for fs.
See https://www.npmjs.com/package/fs-extra

Type:
  • fs
Source:
index.js

<static> glob :glob

Match files using the patterns the shell uses, like stars and stuff.
See https://www.npmjs.com/package/glob

Type:
  • glob
Source:
index.js

<static> Promise :Promise

Bluebird is a fully featured promise library with focus on innovative features and performance

Type:
  • Promise
Source:
index.js

Methods


<static> bin2Hex(bin)

Bin to hex, like 0x7F

Parameters:
Name Type Description
bin binary
Source:
index.js
Returns:
Type
string

<static> coWrap_(generator)

Returns a function that can use yield to yield promises

Parameters:
Name Type Description
generator Generator
Source:
index.js
Returns:
Type
function

<static> dropLeftIfStartsWith(str, left)

Drop the left part if the left part is.

Parameters:
Name Type Description
str *
left *
Source:
index.js

<static> dropRightIfEndsWith(str, right)

Drop the right part if the right part is.

Parameters:
Name Type Description
str *
right *
Source:
index.js

<static> eachAsync_(obj, iterator)

Iterate an array of an object asynchronously

Parameters:
Name Type Description
obj Array | Object
iterator module:Utilities.iteratorFunction
Source:
index.js
Returns:
Type
Promise.<(Array|Object)>

<static> eachPromise_(arrayOfPromiseFactory)

Run an array of promise factory sequentially.

Parameters:
Name Type Description
arrayOfPromiseFactory Array.<module:Utilities.promiseFunction>
Source:
index.js
Returns:
Type
Promise.<Array>
Example
let array = [ ... ];
Util.eachPromise_(_.map(array, a => (lastResult) => new Promsie(...))).then(lastResult => { ... });

<static> ensureLeftSlash(path)

Add a '/' to the left of a path if it does not have one.

Parameters:
Name Type Description
path string

The path

Source:
index.js
Returns:
Type
string

<static> ensureRightSlash(path)

Add a '/' to the right of a path if it does not have one.

Parameters:
Name Type Description
path string

The path

Source:
index.js
Returns:
Type
string

<static> getValueByPath(collection, keyPath [, defaultValue])

Get a value by dot-separated path from a collection

Parameters:
Name Type Argument Description
collection object

The collection

keyPath string

A dot-separated path (dsp), e.g. settings.xxx.yyy

defaultValue object <optional>

The default value if the path does not exist

Source:
index.js
Returns:
Type
*

<static> hasKeyByPath(collection, keyPath)

Check whether a key exists by dot-separated path

Parameters:
Name Type Description
collection *
keyPath *
Source:
index.js
Returns:
Type
boolean

<static> ifAnyPromise_(arrayOfPromiseFactory [, predicate])

Run an array of promise factory sequentially and return immediately if any result of them meets the predication

Parameters:
Name Type Argument Description
arrayOfPromiseFactory Array.<module:Utilities.promiseFunction>
predicate module:Utilities.predicateFunction <optional>
Source:
index.js
Returns:
Type
Promise.<Array>
Example
let array = [ ... ];
Util.ifAnyPromise_(_.map(array, a => () => new Promsie(...)), result => result === 'somevalue').then(found => { ... });

<static> isQuoted(s)

Check a string if it is quoted with " or '

Parameters:
Name Type Description
s string
Source:
index.js
Returns:
Type
boolean

<static> isWrappedWith(s)

Check a string if it is wrapped with given character

Parameters:
Name Type Description
s string
Source:
index.js
Returns:
Type
boolean

<static> load_(file [, variables] [, deps])

Load a js file in sand box.

Parameters:
Name Type Argument Description
file string

Source file

variables object <optional>

Variables as global

deps object <optional>

Dependencies

Source:
index.js
Returns:
Type
AsyncFunction.<*>

<static> pascalCase(str)

Converts string to pascal case.

Parameters:
Name Type Description
str string
Source:
index.js
Returns:
Type
string

<static> putIntoBucket(collection, key, value, flattenArray)

Push an value into an array element of a collection

Parameters:
Name Type Description
collection object
key string
value object
flattenArray boolean

Whether to flatten the array, if the given value is an array.

Source:
index.js
Returns:
Type
*

<static> quote(str, quoteChar)

Quote a string.

Parameters:
Name Type Description
str string
quoteChar string
Source:
index.js
Returns:
Type
string

<static> replaceAll(str, search, replacement)

Replace all matched search in str by the replacement

Parameters:
Name Type Description
str string
search string
replacement string
Source:
index.js
Returns:
Type
string

<static> runCmd_(cmd)

Execute a shell command.

Parameters:
Name Type Description
cmd string

Command line to execute

Source:
index.js
Returns:
Type
Promise.<Object>

<static> runCmdLive_(cmd [, args])

Execute a shell command and lively output

Parameters:
Name Type Argument Description
cmd string

Command line to execute

args Array <optional>

Arguments list

Source:
index.js
Returns:
Type
Promise.<Object>

<static> runCmdSync(cmd)

Execute a shell command synchronously

Parameters:
Name Type Description
cmd string

Command line to execute

Source:
index.js
Returns:
Type
string

<static> setValueByPath(collection, keyPath, value)

Set a value by dot-separated path from a collection

Parameters:
Name Type Description
collection object

The collection

keyPath string

A dot-separated path (dsp), e.g. settings.xxx.yyy

value object

The default value if the path does not exist

Source:
index.js
Returns:
Type
*

<static> sleep_(ms)

Returns a promise to be resolved after given duration (ms)

Parameters:
Name Type Description
ms integer

milliseconds

Source:
index.js

<static> template(str, values)

Interpolate values

Parameters:
Name Type Description
str string
values object
Source:
index.js
Returns:
Type
string

<static> trimLeftSlash(path)

Trim left '/' of a path.

Parameters:
Name Type Description
path string

The path

Source:
index.js
Returns:
Type
string

<static> trimRightSlash(path)

Trim right '/' of a path.

Parameters:
Name Type Description
path string

The path

Source:
index.js
Returns:
Type
string

<static> urlAppendQuery(url, query)

Merge the query parameters into given url.

Parameters:
Name Type Description
url string

Original url.

query object

Key-value pairs query object to be merged into the url.

Source:
index.js
Returns:
Type
string

<static> urlJoin(base, parts)

Join url parts by adding necessary '/', query not supported, use urlAppendQuery instead.

Parameters:
Name Type Description
base string

Left part

parts array

The rest of Url component parts

Source:
index.js
Returns:
Type
string
Example
urlJoin('/', '/user', 'login') => /user/login
  urlJoin('/') => '/'
  urlJoin('') => '/'
  urlJoin('/path/', '/user') => /path/user 

<static> waitUntil_(checker [, checkInterval] [, maxRounds])

Run the checker every given duration for certain rounds until the checker returns non-false value.

Parameters:
Name Type Argument Default Description
checker function

predicator

checkInterval integer <optional>
1000
maxRounds integer <optional>
10
Source:
index.js
Returns:
Type
*

Type Definitions


iteratorFunction(value, key, object)

Async iterator

Parameters:
Name Type Description
value *
key *
object *
Source:
index.js
Returns:
Type
Promise

predicateFunction(value)

A pure closure to be called to check the value status under certain conditions

Parameters:
Name Type Description
value *
Source:
index.js
Returns:
Type
boolean

promiseFunction()

Promise function

Source:
index.js
Returns:
Type
Promise