UNPKG

@haravan-tech/util

Version:

utilities

165 lines (127 loc) 5.62 kB
<a id="util"></a> ## util : <code>object</code> **Kind**: global namespace * [util](#util) : <code>object</code> * [.collection](#util.collection) : <code>object</code> * [.setOptions(target, options, properties)](#util.collection.setOptions) ⇒ <code>object</code> * [.date](#util.date) : <code>object</code> * [.nowLS([locale])](#util.date.nowLS) ⇒ <code>string</code> * [.isValidDate(val)](#util.date.isValidDate) ⇒ <code>boolean</code> * [.lang](#util.lang) : <code>object</code> * [.to(promise)](#util.lang.to) ⇒ <code>Array.&lt;any&gt;</code> * [.toResolve(callbackBasedAPI)](#util.lang.toResolve) ⇒ <code>promise</code> * [.PromiseAllStep(coll, iteratee, step, thisArg)](#util.lang.PromiseAllStep) ⇒ <code>promise</code> <a id="util.collection"></a> ### util.collection : <code>object</code> **Kind**: static namespace of [<code>util</code>](#util) <a id="util.collection.setOptions"></a> #### collection.setOptions(target, options, properties) ⇒ <code>object</code> Merge all options properties to target, Array value will be assign **Kind**: static method of [<code>collection</code>](#util.collection) **Returns**: <code>object</code> - target **Note**: This method mutates object. | Param | Type | Description | | --- | --- | --- | | target | <code>object</code> | | | options | <code>\*</code> | | | properties | <code>Array.&lt;string&gt;</code> \| <code>object</code> | use to pick from options before merging, should contain all options properties.\ If pass an object, properties will be extracted by Object.keys() | <a id="util.date"></a> ### util.date : <code>object</code> **Kind**: static namespace of [<code>util</code>](#util) * [.date](#util.date) : <code>object</code> * [.nowLS([locale])](#util.date.nowLS) ⇒ <code>string</code> * [.isValidDate(val)](#util.date.isValidDate) ⇒ <code>boolean</code> <a id="util.date.nowLS"></a> #### date.nowLS([locale]) ⇒ <code>string</code> Get current time locale string **Kind**: static method of [<code>date</code>](#util.date) **Returns**: <code>string</code> - current time locale string | Param | Type | | --- | --- | | [locale] | <code>string</code> | **Example** ```js nowLS() => '3/2/2019, 11:48:26 AM' ``` <a id="util.date.isValidDate"></a> #### date.isValidDate(val) ⇒ <code>boolean</code> Check whether val is a valid date **Kind**: static method of [<code>date</code>](#util.date) | Param | Type | Description | | --- | --- | --- | | val | <code>string</code> \| <code>number</code> \| <code>Date</code> | val to check | <a id="util.lang"></a> ### util.lang : <code>object</code> **Kind**: static namespace of [<code>util</code>](#util) * [.lang](#util.lang) : <code>object</code> * [.to(promise)](#util.lang.to) ⇒ <code>Array.&lt;any&gt;</code> * [.toResolve(callbackBasedAPI)](#util.lang.toResolve) ⇒ <code>promise</code> * [.PromiseAllStep(coll, iteratee, step, thisArg)](#util.lang.PromiseAllStep) ⇒ <code>promise</code> <a id="util.lang.to"></a> #### lang.to(promise) ⇒ <code>Array.&lt;any&gt;</code> change the way handle promise **Kind**: static method of [<code>lang</code>](#util.lang) **Returns**: <code>Array.&lt;any&gt;</code> - [error, result] | Param | Type | | --- | --- | | promise | <code>Object</code> | **Example** ```js // To handle mongoose error, instead of try catch : try { let store = await StoresModel.findOne({ id : 1000 }); } catch (err) { // handle mongoose error } // You can : let [err, store] = await to(StoresModel.findOne({ id : 1000 })); if (err) { // handle mongoose error } ``` <a id="util.lang.toResolve"></a> #### lang.toResolve(callbackBasedAPI) ⇒ <code>promise</code> Wrap a callback-based-API with a promise that only resolve all callback arguments, and doesn't reject anything **Kind**: static method of [<code>lang</code>](#util.lang) **Returns**: <code>promise</code> - Promisified API | Param | Type | Description | | --- | --- | --- | | callbackBasedAPI | <code>function</code> | The function that you want to wrap | **Example** ```js // assume you have an function function callbackBasedAPI(a1, a2, ..., aN, callback) { // do something ... callback(err, res1, res2, ..., resN); } // and want to receive all callback argument in one call through async/await, like : let [err, res1, res2, ..., resN] = await callbackBasedAPI(a1, a2, ..., aN); // so can easily handle error : if (err) { handleError(err); } // just wrap it with before use let callbackBasedAPI = toResolve(callbackBasedAPI); ``` <a id="util.lang.PromiseAllStep"></a> #### lang.PromiseAllStep(coll, iteratee, step, thisArg) ⇒ <code>promise</code> Applies the function iteratee to each item in coll after timeout, which's increased by step **Kind**: static method of [<code>lang</code>](#util.lang) **Returns**: <code>promise</code> - like Promise.all result | Param | Type | Description | | --- | --- | --- | | coll | <code>array</code> | A collection to iterate over | | iteratee | <code>function</code> | An async function to apply to each item in coll. Apply with (thisArg, item, index) | | step | <code>number</code> | Amount of milisecond that timeout increase after each item | | thisArg | <code>object</code> | The this pointer of iteratee | **Example** ```js await PromiseAllStep(['hello', 'world', 'from', 'Vietnam !'], async word => console.log(word), 200); Will print : hello // after 0ms world // after 200ms from // after 400ms Vietnam // after 600ms ```