Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tiny-parse-argv

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tiny-parse-argv - npm Package Compare versions

Comparing version 2.3.0 to 2.4.0

21

dist/index.js
/* IMPORT */
import { isBoolean, isOverridable, set, uniq, without, zip } from './utils.js';
import { isBoolean, isOverridable, setNormal, setVariadic, uniq, without, zip } from './utils.js';
/* HELPERS */

@@ -21,3 +21,4 @@ const getAliasesMap = (aliases = {}) => {

};
const setAliased = (target, key, value, aliases) => {
const setAliased = (target, key, value, variadic, aliases) => {
const set = variadic ? setVariadic : setNormal;
set(target, key, value);

@@ -94,3 +95,4 @@ aliases[key]?.forEach(alias => {

const strings = getAliasedSet(aliases, options.string);
const eager = getAliasedSet(aliases, options.eager);
const eagers = getAliasedSet(aliases, options.eager);
const variadics = getAliasedSet(aliases, options.variadic);
const defaults = options.default || {};

@@ -115,4 +117,5 @@ const required = options.required || [];

if (!strings.has(key)) { // String options shouldn't have an inferred value
const value = (strings.has(key) ? '' : positive);
setAliased(parsed, key, value, aliases);
const variadic = variadics.has(key);
const value = variadic ? [positive] : positive;
setAliased(parsed, key, value, variadic, aliases);
}

@@ -122,3 +125,3 @@ }

optionPrev = option;
optionEagerPrev = eager.has(key) ? option : '';
optionEagerPrev = eagers.has(key) ? option : '';
}

@@ -128,6 +131,8 @@ else { // Value or Argument

if (optionPrev && (!booleans.has(optionPrev) || isBoolean(value))) { // Regular value
setAliased(parsed, optionPrev, value, aliases);
const variadic = variadics.has(optionPrev);
setAliased(parsed, optionPrev, value, variadic, aliases);
}
else if (optionEagerPrev && !booleans.has(optionEagerPrev)) { // Eager value
setAliased(parsed, optionEagerPrev, value, aliases);
const variadic = variadics.has(optionEagerPrev);
setAliased(parsed, optionEagerPrev, value, variadic, aliases);
}

@@ -134,0 +139,0 @@ else { // Argument

@@ -6,2 +6,3 @@ type Options = {

required?: string[];
variadic?: string[];
alias?: Record<string, string[]>;

@@ -8,0 +9,0 @@ default?: Partial<Record<string, any>>;

@@ -0,8 +1,10 @@

declare const castArray: <T>(value: T | T[]) => T[];
declare const isBoolean: (value: unknown) => value is boolean;
declare const isNil: (value: unknown) => value is null | undefined;
declare const isOverridable: (value: unknown) => value is boolean | "" | null | undefined;
declare const set: (target: any, key: string, value: any) => void;
declare const setNormal: (target: any, key: string, value: any) => void;
declare const setVariadic: (target: any, key: string, value: any) => void;
declare const uniq: <T>(values: T[]) => T[];
declare const without: <T>(values: T[], value: T) => T[];
declare const zip: <T extends string, U>(keys: Set<T> | T[], value: U) => Record<T, U>;
export { isBoolean, isNil, isOverridable, set, uniq, without, zip };
export { castArray, isBoolean, isNil, isOverridable, setNormal, setVariadic, uniq, without, zip };
/* MAIN */
const castArray = (value) => {
return Array.isArray(value) ? value : [value];
};
const isBoolean = (value) => {

@@ -11,3 +14,3 @@ return value === true || value === false;

};
const set = (target, key, value) => {
const setNormal = (target, key, value) => {
if (Array.isArray(target[key])) {

@@ -23,2 +26,14 @@ target[key].push(value);

};
const setVariadic = (target, key, value) => {
const values = castArray(value);
if (Array.isArray(target[key])) {
target[key].push(...values);
}
else if (isOverridable(target[key])) {
target[key] = values;
}
else {
target[key] = [target[key], ...values];
}
};
const uniq = (values) => {

@@ -34,2 +49,2 @@ return Array.from(new Set(values));

/* EXPORT */
export { isBoolean, isNil, isOverridable, set, uniq, without, zip };
export { castArray, isBoolean, isNil, isOverridable, setNormal, setVariadic, uniq, without, zip };

@@ -5,3 +5,3 @@ {

"description": "A tiny function for parsing process.argv, a modern rewrite of a sensible subset of minimist.",
"version": "2.3.0",
"version": "2.4.0",
"type": "module",

@@ -8,0 +8,0 @@ "main": "dist/index.js",

@@ -20,2 +20,3 @@ # Tiny Parse Argv

- `options.eager`: the listed flags are considered to be eager, and will consume multiple consecutive non-flag values.
- `options.variadic`: the listed flags are considered to be variadic, and their value, if present, will always be an array.
- `options.required`: the listed flags are considered to be required, if some are missing `options.onMissing` will be called.

@@ -22,0 +23,0 @@ - `options.alias`: if any aliased flag is assigned then all the aliases for it will be assigned too, automatically.

/* IMPORT */
import {isBoolean, isOverridable, set, uniq, without, zip} from './utils';
import {isBoolean, isOverridable, setNormal, setVariadic, uniq, without, zip} from './utils';
import type {Options, ParsedArgs} from './types';

@@ -40,4 +40,6 @@

const setAliased = ( target: any, key: string, value: any, aliases: Partial<Record<string, string[]>> ): void => {
const setAliased = ( target: any, key: string, value: any, variadic: boolean, aliases: Partial<Record<string, string[]>> ): void => {
const set = variadic ? setVariadic : setNormal;
set ( target, key, value );

@@ -167,3 +169,4 @@

const strings = getAliasedSet ( aliases, options.string );
const eager = getAliasedSet ( aliases, options.eager );
const eagers = getAliasedSet ( aliases, options.eager );
const variadics = getAliasedSet ( aliases, options.variadic );
const defaults = options.default || {};

@@ -197,5 +200,6 @@ const required = options.required || [];

const value = ( strings.has ( key ) ? '' : positive );
const variadic = variadics.has ( key );
const value = variadic ? [positive] : positive;
setAliased ( parsed, key, value, aliases );
setAliased ( parsed, key, value, variadic, aliases );

@@ -209,3 +213,3 @@ }

optionPrev = option;
optionEagerPrev = eager.has ( key ) ? option : '';
optionEagerPrev = eagers.has ( key ) ? option : '';

@@ -218,8 +222,12 @@ } else { // Value or Argument

setAliased ( parsed, optionPrev, value, aliases );
const variadic = variadics.has ( optionPrev );
setAliased ( parsed, optionPrev, value, variadic, aliases );
} else if ( optionEagerPrev && !booleans.has ( optionEagerPrev ) ) { // Eager value
setAliased ( parsed, optionEagerPrev, value, aliases );
const variadic = variadics.has ( optionEagerPrev );
setAliased ( parsed, optionEagerPrev, value, variadic, aliases );
} else { // Argument

@@ -226,0 +234,0 @@

@@ -11,2 +11,3 @@

required?: string[],
variadic?: string[],
alias?: Record<string, string[]>,

@@ -13,0 +14,0 @@ default?: Partial<Record<string, any>>,

/* MAIN */
const castArray = <T> ( value: T | T[] ): T[] => {
return Array.isArray ( value ) ? value : [value];
};
const isBoolean = ( value: unknown ): value is true | false => {

@@ -22,3 +28,3 @@

const set = ( target: any, key: string, value: any ): void => {
const setNormal = ( target: any, key: string, value: any ): void => {

@@ -41,2 +47,22 @@ if ( Array.isArray ( target[key] ) ) {

const setVariadic = ( target: any, key: string, value: any ): void => {
const values = castArray ( value );
if ( Array.isArray ( target[key] ) ) {
target[key].push ( ...values );
} else if ( isOverridable ( target[key] ) ) {
target[key] = values;
} else {
target[key] = [target[key], ...values];
}
};
const uniq = <T> ( values: T[] ): T[] => {

@@ -62,2 +88,2 @@

export {isBoolean, isNil, isOverridable, set, uniq, without, zip};
export {castArray, isBoolean, isNil, isOverridable, setNormal, setVariadic, uniq, without, zip};

@@ -126,2 +126,25 @@

it ( 'supports explicitly variadic flags', t => {
parse ( t, {
input: ['--bool', '--str', '--no-foo', '--bar', 'one', '--baz', 'one', '--baz', 'one', '--qux', 'one', 'two'],
options: {
boolean: ['bool', 'foo'],
string: ['str', 'bar', 'baz'],
eager: ['qux'],
variadic: ['bool', 'str', 'foo', 'bar', 'baz'],
},
output: {
bool: [true],
foo: [false],
bar: ['one'],
baz: ['one', 'one'],
qux: ['one', 'two'],
_: [],
'--': []
}
});
});
it ( 'detects string flags with empty value as missing, when they are required', t => { //TODO: Maybe they should just never receive an empty value

@@ -128,0 +151,0 @@

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc