Skip to content

Commit a6c20b3

Browse files
committedFeb 14, 2020
Require Node.js 10
1 parent 57e9a04 commit a6c20b3

File tree

8 files changed

+170
-143
lines changed

8 files changed

+170
-143
lines changed
 

Diff for: ‎.gitattributes

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* text=auto
2-
*.js text eol=lf
1+
* text=auto eol=lf

Diff for: ‎.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ language: node_js
22
node_js:
33
- '12'
44
- '10'
5-
- '8'

Diff for: ‎index.js

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22

3-
const processFn = (fn, options, proxy, unwrapped) => function (...args) {
3+
const processFn = (fn, options, proxy, unwrapped) => function (...arguments_) {
44
const P = options.promiseModule;
55

66
return new P((resolve, reject) => {
77
if (options.multiArgs) {
8-
args.push((...result) => {
8+
arguments_.push((...result) => {
99
if (options.errorFirst) {
1010
if (result[0]) {
1111
reject(result);
@@ -18,34 +18,35 @@ const processFn = (fn, options, proxy, unwrapped) => function (...args) {
1818
}
1919
});
2020
} else if (options.errorFirst) {
21-
args.push((error, result) => {
21+
arguments_.push((error, result) => {
2222
if (error) {
2323
reject(error);
2424
} else {
2525
resolve(result);
2626
}
2727
});
2828
} else {
29-
args.push(resolve);
29+
arguments_.push(resolve);
3030
}
3131

3232
const self = this === proxy ? unwrapped : this;
33-
Reflect.apply(fn, self, args);
33+
Reflect.apply(fn, self, arguments_);
3434
});
3535
};
3636

3737
const filterCache = new WeakMap();
3838

3939
module.exports = (input, options) => {
40-
options = Object.assign({
41-
exclude: [/.+(Sync|Stream)$/],
40+
options = {
41+
exclude: [/.+(?:Sync|Stream)$/],
4242
errorFirst: true,
43-
promiseModule: Promise
44-
}, options);
43+
promiseModule: Promise,
44+
...options
45+
};
4546

46-
const objType = typeof input;
47-
if (!(input !== null && (objType === 'object' || objType === 'function'))) {
48-
throw new TypeError(`Expected \`input\` to be a \`Function\` or \`Object\`, got \`${input === null ? 'null' : objType}\``);
47+
const objectType = typeof input;
48+
if (!(input !== null && (objectType === 'object' || objectType === 'function'))) {
49+
throw new TypeError(`Expected \`input\` to be a \`Function\` or \`Object\`, got \`${input === null ? 'null' : objectType}\``);
4950
}
5051

5152
const filter = (target, key) => {
@@ -85,26 +86,26 @@ module.exports = (input, options) => {
8586
},
8687

8788
get(target, key) {
88-
const prop = target[key];
89+
const property = target[key];
8990

9091
// eslint-disable-next-line no-use-extend-native/no-use-extend-native
91-
if (!filter(target, key) || prop === Function.prototype[key]) {
92-
return prop;
92+
if (!filter(target, key) || property === Function.prototype[key]) {
93+
return property;
9394
}
9495

95-
const cached = cache.get(prop);
96+
const cached = cache.get(property);
9697

9798
if (cached) {
9899
return cached;
99100
}
100101

101-
if (typeof prop === 'function') {
102-
const pified = processFn(prop, options, proxy, target);
103-
cache.set(prop, pified);
102+
if (typeof property === 'function') {
103+
const pified = processFn(property, options, proxy, target);
104+
cache.set(property, pified);
104105
return pified;
105106
}
106107

107-
return prop;
108+
return property;
108109
}
109110
});
110111

Diff for: ‎license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

Diff for: ‎optimization-test.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,20 @@ function assertOptimized(fn, name) {
2626
}
2727
}
2828

29-
const sut = pify({
30-
unicorn: cb => {
31-
cb(null, 'unicorn');
29+
const fn = pify({
30+
unicorn: callback => {
31+
callback(null, 'unicorn');
3232
}
3333
});
3434

35-
sut.unicorn().then(() => {
36-
v8.optimizeFunctionOnNextCall(sut.unicorn);
37-
38-
return sut.unicorn().then(() => {
39-
assertOptimized(sut.unicorn, 'unicorn');
40-
});
41-
}).catch(error => {
42-
console.error(error.stack);
43-
process.exit(1); // eslint-disable-line unicorn/no-process-exit
44-
});
35+
(async () => {
36+
try {
37+
await fn.unicorn();
38+
v8.optimizeFunctionOnNextCall(fn.unicorn);
39+
await fn.unicorn();
40+
assertOptimized(fn.unicorn, 'unicorn');
41+
} catch (error) {
42+
console.error(error);
43+
process.exit(1); // eslint-disable-line unicorn/no-process-exit
44+
}
45+
})();

Diff for: ‎package.json

+6-8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"description": "Promisify a callback-style function",
55
"license": "MIT",
66
"repository": "sindresorhus/pify",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1112
},
1213
"engines": {
13-
"node": ">=6"
14+
"node": ">=10"
1415
},
1516
"scripts": {
1617
"test": "xo && ava",
@@ -20,14 +21,12 @@
2021
"index.js"
2122
],
2223
"keywords": [
24+
"promisify",
25+
"callback",
2326
"promise",
2427
"promises",
25-
"promisify",
26-
"all",
2728
"denodify",
2829
"denodeify",
29-
"callback",
30-
"cb",
3130
"node",
3231
"then",
3332
"thenify",
@@ -36,7 +35,6 @@
3635
"wrap",
3736
"wrapper",
3837
"bind",
39-
"to",
4038
"async",
4139
"await",
4240
"es2015",
@@ -46,6 +44,6 @@
4644
"ava": "^2.4.0",
4745
"pinkie-promise": "^2.0.0",
4846
"v8-natives": "^1.1.0",
49-
"xo": "^0.23.0"
47+
"xo": "^0.26.1"
5048
}
5149
}

Diff for: ‎readme.md

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,31 @@
22

33
> Promisify a callback-style function
44
5-
65
## Install
76

87
```
98
$ npm install pify
109
```
1110

12-
1311
## Usage
1412

1513
```js
1614
const fs = require('fs');
1715
const pify = require('pify');
1816

1917
(async () => {
20-
// Promisify a single function
18+
// Promisify a single function.
2119
const data = await pify(fs.readFile)('package.json', 'utf8');
2220
console.log(JSON.parse(data).name);
2321
//=> 'pify'
2422

25-
// Promisify all methods in a module
23+
// Promisify all methods in a module.
2624
const data2 = await pify(fs).readFile('package.json', 'utf8');
2725
console.log(JSON.parse(data2).name);
2826
//=> 'pify'
2927
})();
3028
```
3129

32-
3330
## API
3431

3532
### pify(input, options?)
@@ -48,7 +45,7 @@ Type: `object`
4845

4946
##### multiArgs
5047

51-
Type: `boolean`<br>
48+
Type: `boolean`\
5249
Default: `false`
5350

5451
By default, the promisified function will only return the second argument from the callback, which works fine for most APIs. This option can be useful for modules like `request` that return multiple arguments. Turning this on will make it return an array of all arguments from the callback, excluding the error argument, instead of just the second argument. This also applies to rejections, where it returns an array of all the callback arguments, including the error.
@@ -72,14 +69,14 @@ Methods in a module to promisify. Remaining methods will be left untouched.
7269

7370
##### exclude
7471

75-
Type: `Array<string | RegExp>`<br>
76-
Default: `[/.+(Sync|Stream)$/]`
72+
Type: `Array<string | RegExp>`\
73+
Default: `[/.+(?:Sync|Stream)$/]`
7774

7875
Methods in a module **not** to promisify. Methods with names ending with `'Sync'` are excluded by default.
7976

8077
##### excludeMain
8178

82-
Type: `boolean`<br>
79+
Type: `boolean`\
8380
Default: `false`
8481

8582
If the given module is a function itself, it will be promisified. Enable this option if you want to promisify only methods of the module.
@@ -98,7 +95,7 @@ fn.method = (data, callback) => {
9895
};
9996

10097
(async () => {
101-
// Promisify methods but not `fn()`
98+
// Promisify methods but not `fn()`.
10299
const promiseFn = pify(fn, {excludeMain: true});
103100

104101
if (promiseFn()) {
@@ -109,7 +106,7 @@ fn.method = (data, callback) => {
109106

110107
##### errorFirst
111108

112-
Type: `boolean`<br>
109+
Type: `boolean`\
113110
Default: `true`
114111

115112
Whether the callback has an error as the first argument. You'll want to set this to `false` if you're dealing with an API that doesn't have an error as the first argument, like `fs.exists()`, some browser APIs, Chrome Extension APIs, etc.
@@ -120,7 +117,6 @@ Type: `Function`
120117

121118
Custom promise module to use instead of the native one.
122119

123-
124120
## FAQ
125121

126122
#### How is this different from Node.js's [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original)?
@@ -131,14 +127,12 @@ Custom promise module to use instead of the native one.
131127
- Pify has useful options like the ability to handle multiple arguments (`multiArgs`).
132128
- Pify does not have [magic behavior](https://nodejs.org/api/util.html#util_custom_promisified_functions) for certain Node.js methods and instead focuses on predictability.
133129

134-
135130
## Related
136131

137132
- [p-event](https://github.com/sindresorhus/p-event) - Promisify an event by waiting for it to be emitted
138133
- [p-map](https://github.com/sindresorhus/p-map) - Map over promises concurrently
139134
- [More…](https://github.com/sindresorhus/promise-fun)
140135

141-
142136
---
143137

144138
<div align="center">

Diff for: ‎test.js

+119-84
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.