source: trip-planner-front/node_modules/pify/index.js@ 6a3a178

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

initial commit

  • Property mode set to 100644
File size: 1.4 KB
Line 
1'use strict';
2
3var processFn = function (fn, P, opts) {
4 return function () {
5 var that = this;
6 var args = new Array(arguments.length);
7
8 for (var i = 0; i < arguments.length; i++) {
9 args[i] = arguments[i];
10 }
11
12 return new P(function (resolve, reject) {
13 args.push(function (err, result) {
14 if (err) {
15 reject(err);
16 } else if (opts.multiArgs) {
17 var results = new Array(arguments.length - 1);
18
19 for (var i = 1; i < arguments.length; i++) {
20 results[i - 1] = arguments[i];
21 }
22
23 resolve(results);
24 } else {
25 resolve(result);
26 }
27 });
28
29 fn.apply(that, args);
30 });
31 };
32};
33
34var pify = module.exports = function (obj, P, opts) {
35 if (typeof P !== 'function') {
36 opts = P;
37 P = Promise;
38 }
39
40 opts = opts || {};
41 opts.exclude = opts.exclude || [/.+Sync$/];
42
43 var filter = function (key) {
44 var match = function (pattern) {
45 return typeof pattern === 'string' ? key === pattern : pattern.test(key);
46 };
47
48 return opts.include ? opts.include.some(match) : !opts.exclude.some(match);
49 };
50
51 var ret = typeof obj === 'function' ? function () {
52 if (opts.excludeMain) {
53 return obj.apply(this, arguments);
54 }
55
56 return processFn(obj, P, opts).apply(this, arguments);
57 } : {};
58
59 return Object.keys(obj).reduce(function (ret, key) {
60 var x = obj[key];
61
62 ret[key] = typeof x === 'function' && filter(key) ? processFn(x, P, opts) : x;
63
64 return ret;
65 }, ret);
66};
67
68pify.all = pify;
Note: See TracBrowser for help on using the repository browser.