source: trip-planner-front/node_modules/pify/index.js@ 8d391a1

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

primeNG components

  • Property mode set to 100644
File size: 1.6 KB
Line 
1'use strict';
2
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);
15 }
16 } else {
17 resolve(result);
18 }
19 });
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 }
31
32 fn.apply(this, args);
33 });
34};
35
36module.exports = (input, options) => {
37 options = Object.assign({
38 exclude: [/.+(Sync|Stream)$/],
39 errorFirst: true,
40 promiseModule: Promise
41 }, options);
42
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 }
47
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);
51 };
52
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 }
61
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 }
66
67 return ret;
68};
Note: See TracBrowser for help on using the repository browser.