Sha256: e77abeca1a83a97d2f03a88ded75d2e52ebd1d7a4ec9f2ac9ea816417d5effc8

Contents?: true

Size: 1.77 KB

Versions: 60

Compression:

Stored size: 1.77 KB

Contents

'use strict';

const processFn = (fn, opts) => function () {
	const P = opts.promiseModule;
	const args = new Array(arguments.length);

	for (let i = 0; i < arguments.length; i++) {
		args[i] = arguments[i];
	}

	return new P((resolve, reject) => {
		if (opts.errorFirst) {
			args.push(function (err, result) {
				if (opts.multiArgs) {
					const results = new Array(arguments.length - 1);

					for (let i = 1; i < arguments.length; i++) {
						results[i - 1] = arguments[i];
					}

					if (err) {
						results.unshift(err);
						reject(results);
					} else {
						resolve(results);
					}
				} else if (err) {
					reject(err);
				} else {
					resolve(result);
				}
			});
		} else {
			args.push(function (result) {
				if (opts.multiArgs) {
					const results = new Array(arguments.length - 1);

					for (let i = 0; i < arguments.length; i++) {
						results[i] = arguments[i];
					}

					resolve(results);
				} else {
					resolve(result);
				}
			});
		}

		fn.apply(this, args);
	});
};

module.exports = (obj, opts) => {
	opts = Object.assign({
		exclude: [/.+(Sync|Stream)$/],
		errorFirst: true,
		promiseModule: Promise
	}, opts);

	const filter = key => {
		const match = pattern => typeof pattern === 'string' ? key === pattern : pattern.test(key);
		return opts.include ? opts.include.some(match) : !opts.exclude.some(match);
	};

	let ret;
	if (typeof obj === 'function') {
		ret = function () {
			if (opts.excludeMain) {
				return obj.apply(this, arguments);
			}

			return processFn(obj, opts).apply(this, arguments);
		};
	} else {
		ret = Object.create(Object.getPrototypeOf(obj));
	}

	for (const key in obj) { // eslint-disable-line guard-for-in
		const x = obj[key];
		ret[key] = typeof x === 'function' && filter(key) ? processFn(x, opts) : x;
	}

	return ret;
};

Version data entries

60 entries across 52 versions & 6 rubygems

Version Path
ilog-0.4.1 node_modules/pify/index.js
ilog-0.4.0 node_modules/pify/index.js
ilog-0.3.3 node_modules/pify/index.js
govuk_publishing_components-18.0.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.21.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.20.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.19.1 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.19.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.18.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.17.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.16.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.15.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.14.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.13.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.12.2 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.12.1 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.12.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.11.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.10.0 node_modules/pkg-conf/node_modules/pify/index.js
govuk_publishing_components-17.9.0 node_modules/pkg-conf/node_modules/pify/index.js