Skip to content
Permalink
Browse files
Meta tweaks
  • Loading branch information
sindresorhus committed Apr 18, 2017
1 parent 30116d9 commit 65fa82016c9e04a80d357f102affd6e0cafd83d5
Showing 5 changed files with 24 additions and 25 deletions.
@@ -1 +1,2 @@
* text=auto
*.js text eol=lf
@@ -2,19 +2,20 @@
'use strict';
const assert = require('assert');
const v8 = require('v8-natives');
const pify = require('./');
const pify = require('.');

function assertOptimized(fn, name) {
const status = v8.getOptimizationStatus(fn);

switch (status) {
case 1:
// fn is optimized
// `fn` is optimized
console.log('pify is optimized');
return;
case 2:
assert(false, `${name} is not optimized (${status})`);
case 3:
// fn is always optimized
// `fn` is always optimized
return;
case 4:
assert(false, `${name} is never optimized (${status})`);
@@ -44,10 +44,7 @@
"devDependencies": {
"ava": "*",
"pinkie-promise": "^2.0.0",
"v8-natives": "0.0.3",
"v8-natives": "^1.0.0",
"xo": "*"
},
"xo": {
"esnext": true
}
}
@@ -51,7 +51,7 @@ Callback-style function or module whose methods you want to promisify.
Type: `boolean`<br>
Default: `false`

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.
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.

```js
const request = require('request');
34 test.js
@@ -1,7 +1,7 @@
import fs from 'fs';
import test from 'ava';
import pinkiePromise from 'pinkie-promise';
import fn from './';
import m from '.';

const fixture = cb => setImmediate(() => cb(null, 'unicorn'));
const fixture1 = cb => setImmediate(() => cb('error', 'unicorn', 'rainbow'));
@@ -27,40 +27,40 @@ const fixtureModule = {
};

test('main', async t => {
t.is(typeof fn(fixture)().then, 'function');
t.is(await fn(fixture)(), 'unicorn');
t.is(typeof m(fixture)().then, 'function');
t.is(await m(fixture)(), 'unicorn');
});

test('error', async t => {
t.is(await fn(fixture1)().catch(err => err), 'error');
t.is(await m(fixture1)().catch(err => err), 'error');
});

test('pass argument', async t => {
t.is(await fn(fixture2)('rainbow'), 'rainbow');
t.is(await m(fixture2)('rainbow'), 'rainbow');
});

test('custom Promise module', async t => {
t.is(await fn(fixture, {promiseModule: pinkiePromise})(), 'unicorn');
t.is(await m(fixture, {promiseModule: pinkiePromise})(), 'unicorn');
});

test('multiArgs option', async t => {
t.deepEqual(await fn(fixture3, {multiArgs: true})(), ['unicorn', 'rainbow']);
t.deepEqual(await m(fixture3, {multiArgs: true})(), ['unicorn', 'rainbow']);
});

test('multiArgs option — rejection', async t => {
t.deepEqual(await fn(fixture1, {multiArgs: true})().catch(err => err), ['error', 'unicorn', 'rainbow']);
t.deepEqual(await m(fixture1, {multiArgs: true})().catch(err => err), ['error', 'unicorn', 'rainbow']);
});

test('wrap core method', async t => {
t.is(JSON.parse(await fn(fs.readFile)('package.json')).name, 'pify');
t.is(JSON.parse(await m(fs.readFile)('package.json')).name, 'pify');
});

test('module support', async t => {
t.is(JSON.parse(await fn(fs).readFile('package.json')).name, 'pify');
t.is(JSON.parse(await m(fs).readFile('package.json')).name, 'pify');
});

test('module support - doesn\'t transform *Sync methods by default', t => {
t.is(JSON.parse(fn(fs).readFileSync('package.json')).name, 'pify');
t.is(JSON.parse(m(fs).readFileSync('package.json')).name, 'pify');
});

test('module support - preserves non-function members', t => {
@@ -69,11 +69,11 @@ test('module support - preserves non-function members', t => {
nonMethod: 3
};

t.deepEqual(Object.keys(module), Object.keys(fn(module)));
t.deepEqual(Object.keys(module), Object.keys(m(module)));
});

test('module support - transforms only members in options.include', t => {
const pModule = fn(fixtureModule, {
const pModule = m(fixtureModule, {
include: ['method1', 'method2']
});

@@ -83,7 +83,7 @@ test('module support - transforms only members in options.include', t => {
});

test('module support - doesn\'t transform members in options.exclude', t => {
const pModule = fn(fixtureModule, {
const pModule = m(fixtureModule, {
exclude: ['method3']
});

@@ -93,7 +93,7 @@ test('module support - doesn\'t transform members in options.exclude', t => {
});

test('module support - options.include over options.exclude', t => {
const pModule = fn(fixtureModule, {
const pModule = m(fixtureModule, {
include: ['method1', 'method2'],
exclude: ['method2', 'method3']
});
@@ -104,14 +104,14 @@ test('module support - options.include over options.exclude', t => {
});

test('module support — function modules', t => {
const pModule = fn(fixture4);
const pModule = m(fixture4);

t.is(typeof pModule().then, 'function');
t.is(typeof pModule.meow().then, 'function');
});

test('module support — function modules exclusion', t => {
const pModule = fn(fixture4, {
const pModule = m(fixture4, {
excludeMain: true
});

0 comments on commit 65fa820

Please sign in to comment.