Changeset e29cc2e for trip-planner-front/node_modules/pify/index.js
- Timestamp:
- 11/25/21 22:08:24 (3 years ago)
- Branches:
- master
- Children:
- 8d391a1
- Parents:
- 59329aa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner-front/node_modules/pify/index.js
r59329aa re29cc2e 1 1 'use strict'; 2 2 3 var processFn = function (fn, P, opts) { 4 return function () { 5 var that = this; 6 var args = new Array(arguments.length); 3 const processFn = (fn, options) => function (...args) { 4 const P = options.promiseModule; 7 5 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]; 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); 21 15 } 22 23 resolve(results);24 16 } else { 25 17 resolve(result); 26 18 } 27 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 } 28 31 29 fn.apply(that, args); 30 }); 31 }; 32 fn.apply(this, args); 33 }); 32 34 }; 33 35 34 var pify = module.exports = function (obj, P, opts) { 35 if (typeof P !== 'function') { 36 opts = P; 37 P = Promise; 36 module.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}\``); 38 46 } 39 47 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); 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); 49 51 }; 50 52 51 var ret = typeof obj === 'function' ? function () { 52 if (opts.excludeMain) { 53 return obj.apply(this, arguments); 54 } 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 } 55 61 56 return processFn(obj, P, opts).apply(this, arguments); 57 } : {}; 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 } 58 66 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); 67 return ret; 66 68 }; 67 68 pify.all = pify;
Note:
See TracChangeset
for help on using the changeset viewer.