Sha256: 38d9237a7b437c3fb76eef9a98d2de3d83a2c9a8ea068f65c7cf4da5adf13214

Contents?: true

Size: 1.82 KB

Versions: 23

Compression:

Stored size: 1.82 KB

Contents

'use strict';

const isPlainObject = require('is-plain-obj');
const arrify = require('arrify');

const push = (obj, prop, value) => {
	if (!obj[prop]) {
		obj[prop] = [];
	}

	obj[prop].push(value);
};

const insert = (obj, prop, key, value) => {
	if (!obj[prop]) {
		obj[prop] = {};
	}

	obj[prop][key] = value;
};

const passthroughOptions = ['stopEarly', 'unknown', '--'];
const availableTypes = ['string', 'boolean', 'number', 'array'];

const buildOptions = options => {
	options = options || {};

	const result = {};

	passthroughOptions.forEach(key => {
		if (options[key]) {
			result[key] = options[key];
		}
	});

	Object.keys(options).forEach(key => {
		let value = options[key];

		if (key === 'arguments') {
			key = '_';
		}

		// If short form is used
		// convert it to long form
		// e.g. { 'name': 'string' }
		if (typeof value === 'string') {
			value = {type: value};
		}

		if (isPlainObject(value)) {
			const props = value;
			const {type} = props;

			if (type) {
				if (!availableTypes.includes(type)) {
					throw new TypeError(`Expected "${key}" to be one of ["string", "boolean", "number", "array"], got ${type}`);
				}

				push(result, type, key);
			}

			const aliases = arrify(props.alias);

			aliases.forEach(alias => {
				insert(result, 'alias', alias, key);
			});

			if ({}.hasOwnProperty.call(props, 'default')) {
				if (type === 'array' && !Array.isArray(props.default)) {
					throw new TypeError(`Expected "${key}" default value to be array, got ${typeof props.default}`);
				}

				if (type && type !== 'array' && typeof props.default !== type) {
					throw new TypeError(`Expected "${key}" default value to be ${type}, got ${typeof props.default}`);
				}

				insert(result, 'default', key, props.default);
			}
		}
	});

	return result;
};

module.exports = buildOptions;
module.exports.default = buildOptions;

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
trusty-cms-5.0.7 node_modules/minimist-options/index.js
trusty-cms-5.0.6 node_modules/minimist-options/index.js
trusty-cms-5.0.5 node_modules/minimist-options/index.js
trusty-cms-5.0.4 node_modules/minimist-options/index.js
trusty-cms-5.0.3 node_modules/minimist-options/index.js
trusty-cms-5.0.2 node_modules/minimist-options/index.js
trusty-cms-5.0.1 node_modules/minimist-options/index.js
trusty-cms-4.3.5 node_modules/minimist-options/index.js
trusty-cms-5.0.0 node_modules/minimist-options/index.js
trusty-cms-4.3.4 node_modules/minimist-options/index.js
trusty-cms-4.3.3 node_modules/minimist-options/index.js
trusty-cms-4.3.2 node_modules/minimist-options/index.js
trusty-cms-4.3.1 node_modules/minimist-options/index.js
trusty-cms-4.3 node_modules/minimist-options/index.js
trusty-cms-4.2.3 node_modules/minimist-options/index.js
trusty-cms-4.2.2 node_modules/minimist-options/index.js
trusty-cms-4.2.1 node_modules/minimist-options/index.js
trusty-cms-4.2 node_modules/minimist-options/index.js
trusty-cms-4.1.9 node_modules/minimist-options/index.js
trusty-cms-4.1.8 node_modules/minimist-options/index.js