AK-21/Graphite-Industrial-Intelligence
0
1 2var thenify = require('thenify')3 4module.exports = thenifyAll5thenifyAll.withCallback = withCallback6thenifyAll.thenify = thenify7 8/**9 * Promisifies all the selected functions in an object.10 *11 * @param {Object} source the source object for the async functions12 * @param {Object} [destination] the destination to set all the promisified methods13 * @param {Array} [methods] an array of method names of `source`14 * @return {Object}15 * @api public16 */17 18function thenifyAll(source, destination, methods) {19 return promisifyAll(source, destination, methods, thenify)20}21 22/**23 * Promisifies all the selected functions in an object and backward compatible with callback.24 *25 * @param {Object} source the source object for the async functions26 * @param {Object} [destination] the destination to set all the promisified methods27 * @param {Array} [methods] an array of method names of `source`28 * @return {Object}29 * @api public30 */31 32function withCallback(source, destination, methods) {33 return promisifyAll(source, destination, methods, thenify.withCallback)34}35 36function promisifyAll(source, destination, methods, promisify) {37 if (!destination) {38 destination = {};39 methods = Object.keys(source)40 }41 42 if (Array.isArray(destination)) {43 methods = destination44 destination = {}45 }46 47 if (!methods) {48 methods = Object.keys(source)49 }50 51 if (typeof source === 'function') destination = promisify(source)52 53 methods.forEach(function (name) {54 // promisify only if it's a function55 if (typeof source[name] === 'function') destination[name] = promisify(source[name])56 })57 58 // proxy the rest59 Object.keys(source).forEach(function (name) {60 if (deprecated(source, name)) return61 if (destination[name]) return62 destination[name] = source[name]63 })64 65 return destination66}67 68function deprecated(source, name) {69 var desc = Object.getOwnPropertyDescriptor(source, name)70 if (!desc || !desc.get) return false71 if (desc.get.name === 'deprecated') return true72 return false73}74 