3 const processFn = (fn, opts) => function () {
4 const P = opts.promiseModule;
5 const args = new Array(arguments.length);
7 for (let i = 0; i < arguments.length; i++) {
8 args[i] = arguments[i];
11 return new P((resolve, reject) => {
12 if (opts.errorFirst) {
13 args.push(function (err, result) {
15 const results = new Array(arguments.length - 1);
17 for (let i = 1; i < arguments.length; i++) {
18 results[i - 1] = arguments[i];
34 args.push(function (result) {
36 const results = new Array(arguments.length - 1);
38 for (let i = 0; i < arguments.length; i++) {
39 results[i] = arguments[i];
53 module.exports = (obj, opts) => {
54 opts = Object.assign({
55 exclude: [/.+(Sync|Stream)$/],
57 promiseModule: Promise
60 const filter = key => {
61 const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);
62 return opts.include ? opts.include.some(match) : !opts.exclude.some(match);
66 if (typeof obj === 'function') {
68 if (opts.excludeMain) {
69 return obj.apply(this, arguments);
72 return processFn(obj, opts).apply(this, arguments);
75 ret = Object.create(Object.getPrototypeOf(obj));
78 for (const key in obj) { // eslint-disable-line guard-for-in
80 ret[key] = typeof x === 'function' && filter(key) ? processFn(x, opts) : x;