Miscellaneous collection of functions.
- Source:
Methods
callJustOnce(errFopt, cb)
- Source:
Ensures that cb
is only called once. If not, and optional errF
callback
is called.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
errF |
cbType |
<optional> |
An optional callback for any extra calls to |
cb |
cbType | A callback that should only be called once. |
cloneAndMixin(dest, source, keepOld) → {Object}
- Source:
Clones an object before mixin some properties from a source object.
Parameters:
Name | Type | Description |
---|---|---|
dest |
Object | A target object to patch. |
source |
Object | A simple object with deltas. |
keepOld |
boolean | True if we can silently ignore changes to already defined methods, false if we allow changing existing ones. |
Returns:
A cloned and patched dest
.
- Type
- Object
condPromisify(f) → {function}
- Source:
Promisify a function only when it is called without a callback argument, otherwise use the callback.
Parameters:
Name | Type | Description |
---|---|---|
f |
function | A function that expects a callback in its last argument. |
Returns:
A wrapped function that when invoked without a callback it
returns a Promise with an Array tuple [error, data]
, and when using a
callback, it is a pass-through call.
- Type
- function
deepClone(obj, filter) → {Object|Array|number|null|string|boolean}
- Source:
Deep clones nested arrays and objects (e.g., JSON like collections)
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | Array | number | null | string | boolean | Structure to deep clone. |
filter |
function | An optional filter of type
|
Returns:
A deeply cloned structure.
- Type
- Object | Array | number | null | string | boolean
deepEqual(x, y) → {boolean}
- Source:
Whether two objects are structurally similar.
Parameters:
Name | Type | Description |
---|---|---|
x |
Object | An object to compare. |
y |
Object | An object to compare. |
Returns:
True if x
and y
are structurally similar.
- Type
- boolean
deleteProps(obj)
- Source:
Deletes all the enumerable properties of an object
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | An object to cleanup. |
errToPrettyStr(err) → {string}
- Source:
Stringifies an error object so that its stack is properly formatted in the console.
Parameters:
Name | Type | Description |
---|---|---|
err |
Error | An error object to log or send. |
Returns:
A string representation of the error.
- Type
- string
errToStr(err) → {string}
- Source:
Stringifies an error object for logging or network transmission.
Parameters:
Name | Type | Description |
---|---|---|
err |
Error | An error object to log or send. |
Returns:
A string representation of the error.
- Type
- string
extractData(tuple) → {any}
- Source:
Extracts the data field from a tuple [error, data]
.
Throws error
if not null
Parameters:
Name | Type | Description |
---|---|---|
tuple |
Array | An |
Throws:
-
if the
error
field is not a falsy. - Type
- Error
Returns:
The data
field.
- Type
- any
hashCode(st) → {number}
- Source:
Computes a hash code for a string.
Parameters:
Name | Type | Description |
---|---|---|
st |
string | An input string. |
Returns:
An unsigned 32 bit integer representing a hash code for the string.
- Type
- number
mixin(dest, source, keepOld) → {Object}
- Source:
Merges properties defined in a source map into a destination object.
Parameters:
Name | Type | Description |
---|---|---|
dest |
Object | A target object to patch. |
source |
Object | A simple object with deltas. |
keepOld |
boolean | True if we can silently ignore changes to already defined methods, false if we allow changing existing ones. |
Returns:
dest
after patching.
- Type
- Object
(static) clone(obj) → {Object}
- Source:
Clones arrays or objects.
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | An object to be cloned. |
Returns:
A clone of obj
.
- Type
- Object
onlyFun(obj) → {Object}
- Source:
Filters keys in an object that are inherited or do not have a function value.
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object | An object to filter. |
Returns:
An object with only methods.
- Type
- Object
promiseToCallback(promise, cb, wrapException, wrapAppError)
- Source:
Maps a promise to a callback.
Assumes that the promise resolves to a [error, value]
array, compatible
with an standard node.js callback.
It does nothing if the promise is missing.
If it has a non-null value but is not a promise object, or resolves to a non-array object, or an array with length greater than 2, it will propagate an error in the callback.
An optional wrapException
function can modify that error, e.g., to
distinguish them from standard application errors propagated in the array.
Parameters:
Name | Type | Description |
---|---|---|
promise |
Promise | A promise to be mapped into a callback |
cb |
cbType | A callback to propagate the resolved promise values. |
wrapException |
function | An optional function to wrap non-propagated errors. |
wrapAppError |
function | An optional function to wrap application errors propagated in the first element of the returned array. |
randomString(len) → {string}
- Source:
Returns a random string with capital letters and digits.
Parameters:
Name | Type | Description |
---|---|---|
len |
number | The number of characters in the string. |
Returns:
The new string.
- Type
- string
retryWithDelay(f, nTimes, delay, cb)
- Source:
Retries an asynchronous function several times until it succeeds. It delays a retry by a fixed amount of time.
Parameters:
Name | Type | Description |
---|---|---|
f |
function | An asynchronous function of type
|
nTimes |
number | Max number of attempts. |
delay |
number | Time between retries in miliseconds. |
cb |
cbType | Standard callback function for error/result propagation. |
superior(target, methodName) → {function}
- Source:
Captures in a closure a method of the parent class before we override it.
For example:
const supHello = myUtils.superior(that, 'hello');
that.hello = function() {
supHello(); // calls original 'hello'
// do something else
}
Parameters:
Name | Type | Description |
---|---|---|
target |
Object | An object to capture a method from. |
methodName |
string | The name of the method that we want to override. |
Returns:
The function implementing that method in the parent class.
- Type
- function
superiorPromisify(target, methodName) → {function}
- Source:
Captures in a closure a method of the parent class before we override it. It also transforms a callback based function into one that returns a promise.
For example:
const supHello = myUtils.superiorPromisify(that, 'hello');
that.hello = async function() {
try {
const data = await supHello(); // calls original 'hello'
// do something else
} catch (err) {
...
}
Parameters:
Name | Type | Description |
---|---|---|
target |
Object | An object to capture a method from. |
methodName |
string | The name of the method that we want to override. |
Returns:
The function implementing that method in the parent class, modified to return a promise.
- Type
- function
uniqueId() → {string}
- Source:
Returns a unique identifier.
Returns:
A unique identifier.
- Type
- string
wrapAsyncFunction(fopt, targetopt) → {function}
- Source:
Wraps an asynchronous function.
The goal is to make it behave the same whether it uses a callback or returns a promise, e.g, uses the async/await pattern.
Assumes that the promise resolves to a [error, value]
array, compatible
with an standard node.js callback.
If the function throws, and the exception was captured in the promise, we
just mark the exception as thrown, i.e., wasThrown=true
, so that later we
can handle it like a callback-based function exception.
It does nothing if the function is missing. It throws if called without a callback or something that is not a function.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
f |
function |
<optional> |
An asynchronous function to be wrapped. |
target |
Object |
<optional> |
An optional target object when the function is one
of its methods. It defaults to |
Throws:
-
if
f
is provided but is not a function, or we call the wrapped function without a callback. - Type
- Error
Returns:
A wrapped function that behaves the same whether f
is
callback or promise based.
- Type
- function
wrapWithTimeout(f, timeoutopt) → {function}
- Source:
Wraps an asynchronous function to limit its duration.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
f |
function | An asynchronous function to be wrapped. |
|
timeout |
number |
<optional> |
A maximum duration for the call (in msec). |
Returns:
A wrapped function that will return an error in
the callback if timeout expires. The error has a field timeout
set to
true
.
- Type
- function