Ignore:
Timestamp:
11/25/21 22:08:24 (3 years ago)
Author:
Ema <ema_spirova@…>
Branches:
master
Children:
8d391a1
Parents:
59329aa
Message:

primeNG components

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trip-planner-front/node_modules/pify/index.js

    r59329aa re29cc2e  
    11'use strict';
    22
    3 var processFn = function (fn, P, opts) {
    4         return function () {
    5                 var that = this;
    6                 var args = new Array(arguments.length);
     3const processFn = (fn, options) => function (...args) {
     4        const P = options.promiseModule;
    75
    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);
    2115                                        }
    22 
    23                                         resolve(results);
    2416                                } else {
    2517                                        resolve(result);
    2618                                }
    2719                        });
     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                }
    2831
    29                         fn.apply(that, args);
    30                 });
    31         };
     32                fn.apply(this, args);
     33        });
    3234};
    3335
    34 var pify = module.exports = function (obj, P, opts) {
    35         if (typeof P !== 'function') {
    36                 opts = P;
    37                 P = Promise;
     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}\``);
    3846        }
    3947
    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);
    4951        };
    5052
    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        }
    5561
    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        }
    5866
    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;
    6668};
    67 
    68 pify.all = pify;
Note: See TracChangeset for help on using the changeset viewer.