Socket
Socket
Sign inDemoInstall

minimist-options

Package Overview
Dependencies
3
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.0.2 to 4.1.0

18

index.d.ts
import {Opts as MinimistOptions} from 'minimist';
export type OptionType = 'string' | 'boolean' | 'number' | 'array';
export type OptionType = 'string' | 'boolean' | 'number' | 'array' | 'string-array' | 'boolean-array' | 'number-array';

@@ -28,6 +28,7 @@ export interface BaseOption<

export type NumberOption = BaseOption<'number', number>;
export type ArrayOption<ArrayContentType = unknown> = BaseOption<
'array',
ReadonlyArray<ArrayContentType>
>;
export type DefaultArrayOption = BaseOption<'array', ReadonlyArray<string>>;
export type StringArrayOption = BaseOption<'string-array', ReadonlyArray<string>>;
export type BooleanArrayOption = BaseOption<'boolean-array', ReadonlyArray<boolean>>;
export type NumberArrayOption = BaseOption<'number-array', ReadonlyArray<number>>;
type MinimistOption = NonNullable<

@@ -39,3 +40,3 @@ | MinimistOptions['stopEarly']

export type Options<ArrayOptionContentType = unknown> = {
export type Options = {
[key: string]:

@@ -46,3 +47,6 @@ | OptionType

| NumberOption
| ArrayOption<ArrayOptionContentType>
| DefaultArrayOption
| StringArrayOption
| BooleanArrayOption
| NumberArrayOption
| MinimistOption; // Workaround for https://github.com/microsoft/TypeScript/issues/17867

@@ -49,0 +53,0 @@ };

@@ -5,2 +5,3 @@ 'use strict';

const arrify = require('arrify');
const kindOf = require('kind-of');

@@ -23,4 +24,31 @@ const push = (obj, prop, value) => {

const prettyPrint = output => {
return Array.isArray(output) ?
`[${output.map(prettyPrint).join(', ')}]` :
kindOf(output) === 'string' ? JSON.stringify(output) : output;
};
const resolveType = value => {
if (Array.isArray(value) && value.length > 0) {
const [element] = value;
return `${kindOf(element)}-array`;
}
return kindOf(value);
};
const normalizeExpectedType = (type, defaultValue) => {
const inferredType = type === 'array' ? 'string-array' : type;
if (arrayTypes.includes(inferredType) && Array.isArray(defaultValue) && defaultValue.length === 0) {
return 'array';
}
return inferredType;
};
const passthroughOptions = ['stopEarly', 'unknown', '--'];
const availableTypes = ['string', 'boolean', 'number', 'array'];
const primitiveTypes = ['string', 'boolean', 'number'];
const arrayTypes = primitiveTypes.map(t => `${t}-array`);
const availableTypes = [...primitiveTypes, 'array', ...arrayTypes];

@@ -58,25 +86,28 @@ const buildOptions = options => {

if (!availableTypes.includes(type)) {
throw new TypeError(`Expected "${key}" to be one of ["string", "boolean", "number", "array"], got ${type}`);
throw new TypeError(`Expected type of "${key}" to be one of ${prettyPrint(availableTypes)}, got ${prettyPrint(type)}`);
}
push(result, type, key);
if (arrayTypes.includes(type)) {
const [elementType] = type.split('-');
push(result, 'array', {key, [elementType]: true});
} else {
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}`);
}
const {default: defaultValue} = props;
const defaultType = resolveType(defaultValue);
const expectedType = normalizeExpectedType(type, defaultValue);
if (type && type !== 'array' && typeof props.default !== type) {
throw new TypeError(`Expected "${key}" default value to be ${type}, got ${typeof props.default}`);
if (expectedType && expectedType !== defaultType) {
throw new TypeError(`Expected "${key}" default value to be of type "${expectedType}", got ${prettyPrint(defaultType)}`);
}
insert(result, 'default', key, props.default);
insert(result, 'default', key, defaultValue);
}
arrify(props.alias).forEach(alias => {
insert(result, 'alias', alias, key);
});
}

@@ -83,0 +114,0 @@ });

{
"name": "minimist-options",
"version": "4.0.2",
"version": "4.1.0",
"description": "Pretty options for minimist",

@@ -25,3 +25,4 @@ "repository": "vadimdemedes/minimist-options",

"arrify": "^1.0.1",
"is-plain-obj": "^1.1.0"
"is-plain-obj": "^1.1.0",
"kind-of": "^6.0.3"
},

@@ -28,0 +29,0 @@ "devDependencies": {

@@ -1,5 +0,5 @@

# minimist-options [![Build Status](https://travis-ci.org/vadimdemedes/minimist-options.svg?branch=master)](https://travis-ci.org/vadimdemedes/minimist-options)
# minimist-options ![test](https://github.com/vadimdemedes/minimist-options/workflows/test/badge.svg)
> Write options for [minimist](https://npmjs.org/package/minimist) in a comfortable way.
> Support string, boolean, number and array options.
> Write options for [minimist](https://npmjs.org/package/minimist) and [yargs](https://npmjs.org/package/yargs) in a comfortable way.
> Supports string, boolean, number and array options.

@@ -43,2 +43,20 @@ ## Installation

strings: {
type: 'string-array',
alias: 's',
default: ['a', 'b']
},
booleans: {
type: 'boolean-array',
alias: 'b',
default: [true, false]
},
numbers: {
type: 'number-array',
alias: 'n',
default: [0, 1]
},
published: 'boolean',

@@ -61,3 +79,8 @@

number: ['score'],
array: ['arr'],
array: [
'arr',
{key: 'strings', string: true},
{key: 'booleans', boolean: true},
{key: 'numbers', number: true}
],
boolean: ['force', 'published'],

@@ -81,4 +104,12 @@ alias: {

## Array options
The `array` types are only supported by [yargs](https://npmjs.org/package/yargs).
[minimist](https://npmjs.org/package/minimist) does _not_ explicitly support array type options. If you set an option multiple times, it will indeed yield an array of values. However, if you only set it once, it will simply give the value as is, without wrapping it in an array. Thus, effectively ignoring `{type: 'array'}`.
`{type: 'array'}` is shorthand for `{type: 'string-array'}`. To have values coerced to `boolean` or `number`, use `boolean-array` or `number-array`, respectively.
## License
MIT © [Vadim Demedes](https://vadimdemedes.com)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc