Comparing version 5.0.0 to 6.0.0
24
index.js
@@ -1,4 +0,2 @@ | ||
'use strict'; | ||
const processFn = (fn, options, proxy, unwrapped) => function (...arguments_) { | ||
const processFunction = (function_, options, proxy, unwrapped) => function (...arguments_) { | ||
const P = options.promiseModule; | ||
@@ -33,3 +31,3 @@ | ||
const self = this === proxy ? unwrapped : this; | ||
Reflect.apply(fn, self, arguments_); | ||
Reflect.apply(function_, self, arguments_); | ||
}); | ||
@@ -40,3 +38,3 @@ }; | ||
module.exports = (input, options) => { | ||
export default function pify(input, options) { | ||
options = { | ||
@@ -46,3 +44,3 @@ exclude: [/.+(?:Sync|Stream)$/], | ||
promiseModule: Promise, | ||
...options | ||
...options, | ||
}; | ||
@@ -68,5 +66,5 @@ | ||
const match = pattern => (typeof pattern === 'string' || typeof key === 'symbol') ? key === pattern : pattern.test(key); | ||
const desc = Reflect.getOwnPropertyDescriptor(target, key); | ||
const writableOrConfigurableOwn = (desc === undefined || desc.writable || desc.configurable); | ||
const included = options.include ? options.include.some(match) : !options.exclude.some(match); | ||
const descriptor = Reflect.getOwnPropertyDescriptor(target, key); | ||
const writableOrConfigurableOwn = (descriptor === undefined || descriptor.writable || descriptor.configurable); | ||
const included = options.include ? options.include.some(element => match(element)) : !options.exclude.some(element => match(element)); | ||
const shouldFilter = included && writableOrConfigurableOwn; | ||
@@ -87,3 +85,3 @@ cached[key] = shouldFilter; | ||
const pified = options.excludeMain ? target : processFn(target, options, proxy, target); | ||
const pified = options.excludeMain ? target : processFunction(target, options, proxy, target); | ||
cache.set(target, pified); | ||
@@ -108,3 +106,3 @@ return Reflect.apply(pified, thisArg, args); | ||
if (typeof property === 'function') { | ||
const pified = processFn(property, options, proxy, target); | ||
const pified = processFunction(property, options, proxy, target); | ||
cache.set(property, pified); | ||
@@ -115,6 +113,6 @@ return pified; | ||
return property; | ||
} | ||
}, | ||
}); | ||
return proxy; | ||
}; | ||
} |
{ | ||
"name": "pify", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Promisify a callback-style function", | ||
@@ -13,4 +13,6 @@ "license": "MIT", | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=10" | ||
"node": ">=14.16" | ||
}, | ||
@@ -45,7 +47,7 @@ "scripts": { | ||
"devDependencies": { | ||
"ava": "^2.4.0", | ||
"pinkie-promise": "^2.0.0", | ||
"v8-natives": "^1.1.0", | ||
"xo": "^0.26.1" | ||
"ava": "^4.3.0", | ||
"pinkie-promise": "^2.0.1", | ||
"v8-natives": "^1.2.5", | ||
"xo": "^0.49.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# pify [![Build Status](https://travis-ci.org/sindresorhus/pify.svg?branch=master)](https://travis-ci.org/sindresorhus/pify) | ||
# pify | ||
@@ -7,5 +7,5 @@ > Promisify a callback-style function | ||
```sh | ||
npm install pify | ||
``` | ||
$ npm install pify | ||
``` | ||
@@ -15,16 +15,14 @@ ## Usage | ||
```js | ||
const fs = require('fs'); | ||
const pify = require('pify'); | ||
import fs from 'fs'; | ||
import pify from 'pify'; | ||
(async () => { | ||
// Promisify a single function. | ||
const data = await pify(fs.readFile)('package.json', 'utf8'); | ||
console.log(JSON.parse(data).name); | ||
//=> 'pify' | ||
// Promisify a single function. | ||
const data = await pify(fs.readFile)('package.json', 'utf8'); | ||
console.log(JSON.parse(data).name); | ||
//=> 'pify' | ||
// Promisify all methods in a module. | ||
const data2 = await pify(fs).readFile('package.json', 'utf8'); | ||
console.log(JSON.parse(data2).name); | ||
//=> 'pify' | ||
})(); | ||
// Promisify all methods in a module. | ||
const data2 = await pify(fs).readFile('package.json', 'utf8'); | ||
console.log(JSON.parse(data2).name); | ||
//=> 'pify' | ||
``` | ||
@@ -56,10 +54,8 @@ | ||
```js | ||
const request = require('request'); | ||
const pify = require('pify'); | ||
import request from 'request'; | ||
import pify from 'pify'; | ||
const pRequest = pify(request, {multiArgs: true}); | ||
(async () => { | ||
const [httpResponse, body] = await pRequest('https://sindresorhus.com'); | ||
})(); | ||
const [httpResponse, body] = await pRequest('https://sindresorhus.com'); | ||
``` | ||
@@ -88,3 +84,3 @@ | ||
```js | ||
const pify = require('pify'); | ||
import pify from 'pify'; | ||
@@ -101,10 +97,8 @@ function fn() { | ||
(async () => { | ||
// Promisify methods but not `fn()`. | ||
const promiseFn = pify(fn, {excludeMain: true}); | ||
// Promisify methods but not `fn()`. | ||
const promiseFn = pify(fn, {excludeMain: true}); | ||
if (promiseFn()) { | ||
console.log(await promiseFn.method('hi')); | ||
} | ||
})(); | ||
if (promiseFn()) { | ||
console.log(await promiseFn.method('hi')); | ||
} | ||
``` | ||
@@ -135,2 +129,23 @@ | ||
#### How can I promisify a single class method? | ||
Class methods are not bound, so when they're not called on the class itself, they don't have any context. You can either promisify the whole class or use `.bind()`. | ||
```js | ||
import pify from 'pify'; | ||
import SomeClass from './some-class.js'; | ||
const someInstance = new SomeClass(); | ||
// ❌ `someFunction` can't access its class context. | ||
const someFunction = pify(someClass.someFunction); | ||
// ✅ The whole class is promisified and the `someFunction` method is called on its class. | ||
const someClassPromisified = pify(someClass); | ||
someClassPromisified.someFunction(); | ||
// ✅ `someFunction` is bound to its class before being promisified. | ||
const someFunction = pify(someClass.someFunction.bind(someClass)); | ||
``` | ||
## Related | ||
@@ -137,0 +152,0 @@ |
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
9536
162
Yes
91