@supercharge/goodies
Advanced tools
Comparing version 1.1.0 to 1.2.0
# Changelog | ||
## [1.2.0](https://github.com/supercharge/goodies/compare/v1.1.0...v1.2.0) - 2020-05-12 | ||
### Added | ||
- `isAsyncFunction(input)` method determining whether the given `input` is an async function | ||
- testing against Node.js v14 | ||
### Updated | ||
- bump dependencies | ||
- move tests back to JavaScript | ||
- `tap(value, callback)` now only returns a Promise when the value is a promise or the callback is an async function. Otherwise, tap behaves synchronously | ||
- `upon(value, callback)` now only returns a Promise when the value is a promise or the callback is an async function. Otherwise, upon behaves synchronously | ||
### Removed | ||
- testing against Node.js v13 | ||
## [1.1.0](https://github.com/supercharge/goodies/compare/v1.0.0...v1.1.0) - 2020-04-15 | ||
@@ -9,2 +25,3 @@ | ||
- `tap` the value when a promise as the first argument before passing it to the callback | ||
- `isPromise(input)` method: determine whether the given `input` is a promise | ||
@@ -11,0 +28,0 @@ ### Updated |
export declare class Goodies { | ||
/** | ||
* Calls the given `callback` function with the given `value` | ||
* and returns `value`. It resolves the `value` before | ||
* passing it to the callback in case it is a Promise. | ||
* Handles the tap call and delegates it either to an async tap | ||
* handler or to a sync tap handler. | ||
* | ||
@@ -12,4 +11,25 @@ * @param {*} value | ||
*/ | ||
tap(value: any, callback: Function | any): Promise<any>; | ||
tap(value: any, callback: Function): Promise<any> | any; | ||
/** | ||
* Calls the given `callback` function with the | ||
* given `value` and returns `value`. | ||
* | ||
* @param {*} value | ||
* @param {Function} callback | ||
* | ||
* @returns {*} value | ||
*/ | ||
tapSync(value: any, callback: Function): any; | ||
/** | ||
* Calls the given `callback` function with the given `value` | ||
* and returns `value`. It resolves the `value` before | ||
* passing it to the callback in case it is a Promise. | ||
* | ||
* @param {*} value | ||
* @param {Function} callback | ||
* | ||
* @returns {*} value | ||
*/ | ||
tapAsync(value: any, callback: Function): Promise<any>; | ||
/** | ||
* Calls the given `callback` function with the given `value` and returns | ||
@@ -24,4 +44,25 @@ * the result of the callback. It resolves the `value` before passing | ||
*/ | ||
upon(value: any, callback: Function | any): Promise<any>; | ||
upon(value: any, callback: Function): Promise<any> | any; | ||
/** | ||
* Calls the given `callback` function with the given `value` and returns | ||
* the result of the callback. | ||
* | ||
* @param {*} value | ||
* @param {Function} callback | ||
* | ||
* @returns {*} value | ||
*/ | ||
uponSync(value: any, callback: Function): any; | ||
/** | ||
* Calls the given `callback` function with the given `value` and returns | ||
* the result of the callback. It resolves the `value` before passing | ||
* it to the callback in case it is a Promise. | ||
* | ||
* @param {*} value | ||
* @param {Function} callback | ||
* | ||
* @returns {*} value | ||
*/ | ||
uponAsync(value: any, callback: Function): Promise<any>; | ||
/** | ||
* Determine whether the given `promise` is a Promise. | ||
@@ -42,2 +83,10 @@ * | ||
isFunction(input: any): Boolean; | ||
/** | ||
* Determine whether the given `func` is an async function. | ||
* | ||
* @param {*} input | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
isAsyncFunction(input: any): Boolean; | ||
} |
@@ -5,5 +5,4 @@ 'use strict'; | ||
/** | ||
* Calls the given `callback` function with the given `value` | ||
* and returns `value`. It resolves the `value` before | ||
* passing it to the callback in case it is a Promise. | ||
* Handles the tap call and delegates it either to an async tap | ||
* handler or to a sync tap handler. | ||
* | ||
@@ -15,4 +14,38 @@ * @param {*} value | ||
*/ | ||
async tap(value, callback) { | ||
tap(value, callback) { | ||
if (this.isPromise(value)) { | ||
return this.tapAsync(value, callback); | ||
} | ||
if (this.isAsyncFunction(callback)) { | ||
return this.tapAsync(value, callback); | ||
} | ||
return this.tapSync(value, callback); | ||
} | ||
/** | ||
* Calls the given `callback` function with the | ||
* given `value` and returns `value`. | ||
* | ||
* @param {*} value | ||
* @param {Function} callback | ||
* | ||
* @returns {*} value | ||
*/ | ||
tapSync(value, callback) { | ||
if (this.isFunction(callback)) { | ||
callback(value); | ||
} | ||
return value; | ||
} | ||
/** | ||
* Calls the given `callback` function with the given `value` | ||
* and returns `value`. It resolves the `value` before | ||
* passing it to the callback in case it is a Promise. | ||
* | ||
* @param {*} value | ||
* @param {Function} callback | ||
* | ||
* @returns {*} value | ||
*/ | ||
async tapAsync(value, callback) { | ||
if (this.isPromise(value)) { | ||
value = await value; | ||
@@ -35,4 +68,37 @@ } | ||
*/ | ||
async upon(value, callback) { | ||
upon(value, callback) { | ||
if (this.isPromise(value)) { | ||
return this.uponAsync(value, callback); | ||
} | ||
if (this.isAsyncFunction(callback)) { | ||
return this.uponAsync(value, callback); | ||
} | ||
return this.uponSync(value, callback); | ||
} | ||
/** | ||
* Calls the given `callback` function with the given `value` and returns | ||
* the result of the callback. | ||
* | ||
* @param {*} value | ||
* @param {Function} callback | ||
* | ||
* @returns {*} value | ||
*/ | ||
uponSync(value, callback) { | ||
return this.isFunction(callback) | ||
? callback(value) | ||
: value; | ||
} | ||
/** | ||
* Calls the given `callback` function with the given `value` and returns | ||
* the result of the callback. It resolves the `value` before passing | ||
* it to the callback in case it is a Promise. | ||
* | ||
* @param {*} value | ||
* @param {Function} callback | ||
* | ||
* @returns {*} value | ||
*/ | ||
async uponAsync(value, callback) { | ||
if (this.isPromise(value)) { | ||
value = await value; | ||
@@ -64,3 +130,13 @@ } | ||
} | ||
/** | ||
* Determine whether the given `func` is an async function. | ||
* | ||
* @param {*} input | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
isAsyncFunction(input) { | ||
return this.isFunction(input) && input.constructor.name === 'AsyncFunction'; | ||
} | ||
} | ||
exports.Goodies = Goodies; |
@@ -17,3 +17,3 @@ /** | ||
*/ | ||
export declare function tap(value: any, callback?: Function): Promise<any>; | ||
export declare function tap(value: any, callback: Function): any | Promise<any>; | ||
/** | ||
@@ -34,3 +34,3 @@ * Calls the given `callback` function with the given `value` and returns | ||
*/ | ||
export declare function upon(value: any, callback?: Function): Promise<any>; | ||
export declare function upon(value: any, callback: Function): any | Promise<any>; | ||
/** | ||
@@ -48,1 +48,9 @@ * Determine whether the given `promise` is a Promise. | ||
export declare function isPromise(promise?: any): Boolean; | ||
/** | ||
* Determine whether the given `input` is an async function. | ||
* | ||
* @param {*} input | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
export declare function isAsyncFunction(input: any): Boolean; |
@@ -20,3 +20,3 @@ 'use strict'; | ||
*/ | ||
async function tap(value, callback) { | ||
function tap(value, callback) { | ||
return new goodies_1.Goodies().tap(value, callback); | ||
@@ -40,3 +40,3 @@ } | ||
*/ | ||
async function upon(value, callback) { | ||
function upon(value, callback) { | ||
return new goodies_1.Goodies().upon(value, callback); | ||
@@ -60,1 +60,12 @@ } | ||
exports.isPromise = isPromise; | ||
/** | ||
* Determine whether the given `input` is an async function. | ||
* | ||
* @param {*} input | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
function isAsyncFunction(input) { | ||
return new goodies_1.Goodies().isAsyncFunction(input); | ||
} | ||
exports.isAsyncFunction = isAsyncFunction; |
{ | ||
"name": "@supercharge/goodies", | ||
"description": "Async utility functions for Node.js and JavaScript", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"author": "Marcus Pöhls <marcus@superchargejs.com>", | ||
@@ -13,5 +13,7 @@ "bugs": { | ||
"@hapi/lab": "~21.0.0", | ||
"@typescript-eslint/eslint-plugin": "~2.28.0", | ||
"@supercharge/tsconfig": "~1.0.0", | ||
"@typescript-eslint/parser": "~2.31.0", | ||
"@typescript-eslint/eslint-plugin": "~2.31.0", | ||
"eslint": "~6.8.0", | ||
"eslint-config-standard-with-typescript": "~15.0.1", | ||
"eslint-config-standard-with-typescript": "~16.0.0", | ||
"eslint-plugin-import": "~2.20.2", | ||
@@ -21,3 +23,2 @@ "eslint-plugin-node": "~11.1.0", | ||
"eslint-plugin-standard": "~4.0.1", | ||
"lab-transform-typescript": "~3.0.1", | ||
"typescript": "~3.8.3" | ||
@@ -57,6 +58,6 @@ }, | ||
"test": "npm run build && npm run lint && npm run test:run", | ||
"test:run": "lab --transform node_modules/lab-transform-typescript --assert @hapi/code --leaks --coverage --reporter console --output stdout --reporter html --output ./coverage/coverage.html", | ||
"test:single": "lab --transform node_modules/lab-transform-typescript --assert @hapi/code --leaks --lint --id" | ||
"test:run": "lab --assert @hapi/code --leaks --coverage --reporter console --output stdout --reporter html --output ./coverage/coverage.html", | ||
"test:single": "lab --assert @hapi/code --leaks --lint --id" | ||
}, | ||
"types": "dist" | ||
} |
@@ -23,2 +23,3 @@ <div align="center"> | ||
<a href="https://www.npmjs.com/package/@supercharge/goodies"><img src="https://img.shields.io/npm/v/@supercharge/goodies.svg" alt="Latest Version"></a> | ||
<a href="https://www.npmjs.com/package/@supercharge/goodies"><img src="https://img.shields.io/npm/dm/@supercharge/goodies.svg" alt="Monthly downloads"></a> | ||
</p> | ||
@@ -25,0 +26,0 @@ <p> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16208
350
97
0
12