Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to support non-error-first APIs #42

Merged
merged 1 commit into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,42 @@ const processFn = (fn, opts) => function () {
}

return new P((resolve, reject) => {
args.push(function (err, result) {
if (opts.multiArgs) {
const results = new Array(arguments.length - 1);
if (opts.errorFirst) {
args.push(function (err, result) {
if (opts.multiArgs) {
const results = new Array(arguments.length - 1);

for (let i = 1; i < arguments.length; i++) {
results[i - 1] = arguments[i];
}
for (let i = 1; i < arguments.length; i++) {
results[i - 1] = arguments[i];
}

if (err) {
results.unshift(err);
reject(results);
if (err) {
results.unshift(err);
reject(results);
} else {
resolve(results);
}
} else if (err) {
reject(err);
} else {
resolve(result);
}
});
} else {
args.push(function (result) {
if (opts.multiArgs) {
const results = new Array(arguments.length - 1);

for (let i = 0; i < arguments.length; i++) {
results[i] = arguments[i];
}

resolve(results);
} else {
resolve(result);
}
} else if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}

fn.apply(that, args);
});
Expand All @@ -38,7 +54,8 @@ const processFn = (fn, opts) => function () {
module.exports = (obj, opts) => {
opts = Object.assign({
exclude: [/.+(Sync|Stream)$/],
promiseModule: Promise
promiseModule: Promise,
errorFirst: true
}, opts);

const filter = key => {
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ if (promiseFn()) {
}
```

##### errorFirst

Type: `boolean`<br>
Default: `true`

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.

##### promiseModule

Type: `Function`
Expand Down
19 changes: 19 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,22 @@ test('module support — function modules exclusion', t => {
t.is(typeof pModule.meow().then, 'function');
t.not(typeof pModule(() => {}).then, 'function');
});

test('`errorFirst` option', async t => {
const fixture = (foo, cb) => {
cb(foo);
};

t.is(await m(fixture, {errorFirst: false})('🦄'), '🦄');
});

test('`errorFirst` option and `multiArgs`', async t => {
const fixture = (foo, bar, cb) => {
cb(foo, bar);
};

t.deepEqual(await m(fixture, {
errorFirst: false,
multiArgs: true
})('🦄', '🌈'), ['🦄', '🌈']);
});