source: trip-planner-front/node_modules/pify/index.js@ 571e0df

Last change on this file since 571e0df was e29cc2e, checked in by Ema <ema_spirova@…>, 3 years ago

primeNG components

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[6a3a178]1'use strict';
2
[e29cc2e]3const processFn = (fn, options) => function (...args) {
4 const P = options.promiseModule;
5
6 return new P((resolve, reject) => {
7 if (options.multiArgs) {
8 args.push((...result) => {
9 if (options.errorFirst) {
10 if (result[0]) {
11 reject(result);
12 } else {
13 result.shift();
14 resolve(result);
[6a3a178]15 }
16 } else {
17 resolve(result);
18 }
19 });
[e29cc2e]20 } else if (options.errorFirst) {
21 args.push((error, result) => {
22 if (error) {
23 reject(error);
24 } else {
25 resolve(result);
26 }
27 });
28 } else {
29 args.push(resolve);
30 }
[6a3a178]31
[e29cc2e]32 fn.apply(this, args);
33 });
[6a3a178]34};
35
[e29cc2e]36module.exports = (input, options) => {
37 options = Object.assign({
38 exclude: [/.+(Sync|Stream)$/],
39 errorFirst: true,
40 promiseModule: Promise
41 }, options);
[6a3a178]42
[e29cc2e]43 const objType = typeof input;
44 if (!(input !== null && (objType === 'object' || objType === 'function'))) {
45 throw new TypeError(`Expected \`input\` to be a \`Function\` or \`Object\`, got \`${input === null ? 'null' : objType}\``);
46 }
[6a3a178]47
[e29cc2e]48 const filter = key => {
49 const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);
50 return options.include ? options.include.some(match) : !options.exclude.some(match);
[6a3a178]51 };
52
[e29cc2e]53 let ret;
54 if (objType === 'function') {
55 ret = function (...args) {
56 return options.excludeMain ? input(...args) : processFn(input, options).apply(this, args);
57 };
58 } else {
59 ret = Object.create(Object.getPrototypeOf(input));
60 }
[6a3a178]61
[e29cc2e]62 for (const key in input) { // eslint-disable-line guard-for-in
63 const property = input[key];
64 ret[key] = typeof property === 'function' && filter(key) ? processFn(property, options) : property;
65 }
[6a3a178]66
[e29cc2e]67 return ret;
[6a3a178]68};
Note: See TracBrowser for help on using the repository browser.