ghaction-upx/node_modules/pify/index.js

69 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-01-17 01:36:42 -07:00
'use strict';
2020-04-03 02:12:31 -06:00
const processFn = (fn, options) => function (...args) {
const P = options.promiseModule;
2020-01-17 01:36:42 -07:00
return new P((resolve, reject) => {
2020-04-03 02:12:31 -06:00
if (options.multiArgs) {
args.push((...result) => {
if (options.errorFirst) {
if (result[0]) {
reject(result);
2020-01-17 01:36:42 -07:00
} else {
2020-04-03 02:12:31 -06:00
result.shift();
resolve(result);
2020-01-17 01:36:42 -07:00
}
} else {
resolve(result);
}
});
2020-04-03 02:12:31 -06:00
} else if (options.errorFirst) {
args.push((error, result) => {
if (error) {
reject(error);
2020-01-17 01:36:42 -07:00
} else {
resolve(result);
}
});
2020-04-03 02:12:31 -06:00
} else {
args.push(resolve);
2020-01-17 01:36:42 -07:00
}
fn.apply(this, args);
});
};
2020-04-03 02:12:31 -06:00
module.exports = (input, options) => {
options = Object.assign({
2020-01-17 01:36:42 -07:00
exclude: [/.+(Sync|Stream)$/],
errorFirst: true,
promiseModule: Promise
2020-04-03 02:12:31 -06:00
}, options);
const objType = typeof input;
if (!(input !== null && (objType === 'object' || objType === 'function'))) {
throw new TypeError(`Expected \`input\` to be a \`Function\` or \`Object\`, got \`${input === null ? 'null' : objType}\``);
}
2020-01-17 01:36:42 -07:00
const filter = key => {
const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);
2020-04-03 02:12:31 -06:00
return options.include ? options.include.some(match) : !options.exclude.some(match);
2020-01-17 01:36:42 -07:00
};
let ret;
2020-04-03 02:12:31 -06:00
if (objType === 'function') {
ret = function (...args) {
return options.excludeMain ? input(...args) : processFn(input, options).apply(this, args);
2020-01-17 01:36:42 -07:00
};
} else {
2020-04-03 02:12:31 -06:00
ret = Object.create(Object.getPrototypeOf(input));
2020-01-17 01:36:42 -07:00
}
2020-04-03 02:12:31 -06:00
for (const key in input) { // eslint-disable-line guard-for-in
const property = input[key];
ret[key] = typeof property === 'function' && filter(key) ? processFn(property, options) : property;
2020-01-17 01:36:42 -07:00
}
return ret;
};