basant307/AI_Governance_Project
048
1'use strict';2 3const processFn = (fn, opts) => function () {4 const P = opts.promiseModule;5 const args = new Array(arguments.length);6 7 for (let i = 0; i < arguments.length; i++) {8 args[i] = arguments[i];9 }10 11 return new P((resolve, reject) => {12 if (opts.errorFirst) {13 args.push(function (err, result) {14 if (opts.multiArgs) {15 const results = new Array(arguments.length - 1);16 17 for (let i = 1; i < arguments.length; i++) {18 results[i - 1] = arguments[i];19 }20 21 if (err) {22 results.unshift(err);23 reject(results);24 } else {25 resolve(results);26 }27 } else if (err) {28 reject(err);29 } else {30 resolve(result);31 }32 });33 } else {34 args.push(function (result) {35 if (opts.multiArgs) {36 const results = new Array(arguments.length - 1);37 38 for (let i = 0; i < arguments.length; i++) {39 results[i] = arguments[i];40 }41 42 resolve(results);43 } else {44 resolve(result);45 }46 });47 }48 49 fn.apply(this, args);50 });51};52 53module.exports = (obj, opts) => {54 opts = Object.assign({55 exclude: [/.+(Sync|Stream)$/],56 errorFirst: true,57 promiseModule: Promise58 }, opts);59 60 const filter = key => {61 const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);62 return opts.include ? opts.include.some(match) : !opts.exclude.some(match);63 };64 65 let ret;66 if (typeof obj === 'function') {67 ret = function () {68 if (opts.excludeMain) {69 return obj.apply(this, arguments);70 }71 72 return processFn(obj, opts).apply(this, arguments);73 };74 } else {75 ret = Object.create(Object.getPrototypeOf(obj));76 }77 78 for (const key in obj) { // eslint-disable-line guard-for-in79 const x = obj[key];80 ret[key] = typeof x === 'function' && filter(key) ? processFn(x, opts) : x;81 }82 83 return ret;84};85 