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.comType:
- lodash
- Source:
- index.js
-
<static> async :async
-
Higher-order functions and common patterns for asynchronous code.
See http://caolan.github.io/asyncType:
- 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-extraType:
- 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/globType:
- 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 binbinary - Source:
- index.js
Returns:
- Type
- string
-
<static> coWrap_(generator)
-
Returns a function that can use yield to yield promises
Parameters:
Name Type Description generatorGenerator - 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 objArray | Object iteratormodule:Utilities.iteratorFunction - Source:
- index.js
Returns:
- Type
- Promise.<(Array|Object)>
-
<static> eachPromise_(arrayOfPromiseFactory)
-
Run an array of promise factory sequentially.
Parameters:
Name Type Description arrayOfPromiseFactoryArray.<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 pathstring 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 pathstring 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 collectionobject The collection
keyPathstring A dot-separated path (dsp), e.g. settings.xxx.yyy
defaultValueobject <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 arrayOfPromiseFactoryArray.<module:Utilities.promiseFunction> predicatemodule: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 sstring - Source:
- index.js
Returns:
- Type
- boolean
-
<static> isWrappedWith(s)
-
Check a string if it is wrapped with given character
Parameters:
Name Type Description sstring - Source:
- index.js
Returns:
- Type
- boolean
-
<static> load_(file [, variables] [, deps])
-
Load a js file in sand box.
Parameters:
Name Type Argument Description filestring Source file
variablesobject <optional>
Variables as global
depsobject <optional>
Dependencies
- Source:
- index.js
Returns:
- Type
- AsyncFunction.<*>
-
<static> pascalCase(str)
-
Converts string to pascal case.
Parameters:
Name Type Description strstring - 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 collectionobject keystring valueobject flattenArrayboolean 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 strstring quoteCharstring - Source:
- index.js
Returns:
- Type
- string
-
<static> replaceAll(str, search, replacement)
-
Replace all matched search in str by the replacement
Parameters:
Name Type Description strstring searchstring replacementstring - Source:
- index.js
Returns:
- Type
- string
-
<static> runCmd_(cmd)
-
Execute a shell command.
Parameters:
Name Type Description cmdstring 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 cmdstring Command line to execute
argsArray <optional>
Arguments list
- Source:
- index.js
Returns:
- Type
- Promise.<Object>
-
<static> runCmdSync(cmd)
-
Execute a shell command synchronously
Parameters:
Name Type Description cmdstring 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 collectionobject The collection
keyPathstring A dot-separated path (dsp), e.g. settings.xxx.yyy
valueobject 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 msinteger milliseconds
- Source:
- index.js
-
<static> template(str, values)
-
Interpolate values
Parameters:
Name Type Description strstring valuesobject - Source:
- index.js
Returns:
- Type
- string
-
<static> trimLeftSlash(path)
-
Trim left '/' of a path.
Parameters:
Name Type Description pathstring The path
- Source:
- index.js
Returns:
- Type
- string
-
<static> trimRightSlash(path)
-
Trim right '/' of a path.
Parameters:
Name Type Description pathstring The path
- Source:
- index.js
Returns:
- Type
- string
-
<static> urlAppendQuery(url, query)
-
Merge the query parameters into given url.
Parameters:
Name Type Description urlstring Original url.
queryobject 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 basestring Left part
partsarray 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 checkerfunction predicator
checkIntervalinteger <optional>
1000 maxRoundsinteger <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