Comparing version 6.0.0 to 6.1.0
75
index.js
'use strict'; | ||
const match = (array, value) => array.some(x => x instanceof RegExp ? x.test(value) : x === value); | ||
const match = (array, value) => | ||
array.some(x => (x instanceof RegExp ? x.test(value) : x === value)); | ||
module.exports = (input, opts) => { | ||
const dargs = (input, options) => { | ||
const args = []; | ||
@@ -10,16 +11,20 @@ let extraArgs = []; | ||
opts = Object.assign({ | ||
options = Object.assign({ | ||
useEquals: true | ||
}, opts); | ||
}, options); | ||
const makeArg = (key, val) => { | ||
key = '--' + (opts.allowCamelCase ? key : key.replace(/[A-Z]/g, '-$&').toLowerCase()); | ||
const makeArg = (key, value) => { | ||
key = | ||
'--' + | ||
(options.allowCamelCase ? | ||
key : | ||
key.replace(/[A-Z]/g, '-$&').toLowerCase()); | ||
if (opts.useEquals) { | ||
args.push(key + (val ? `=${val}` : '')); | ||
if (options.useEquals) { | ||
args.push(key + (value ? `=${value}` : '')); | ||
} else { | ||
args.push(key); | ||
if (val) { | ||
args.push(val); | ||
if (value) { | ||
args.push(value); | ||
} | ||
@@ -29,7 +34,7 @@ } | ||
const makeAliasArg = (key, val) => { | ||
const makeAliasArg = (key, value) => { | ||
args.push(`-${key}`); | ||
if (val) { | ||
args.push(val); | ||
if (value) { | ||
args.push(value); | ||
} | ||
@@ -40,15 +45,15 @@ }; | ||
for (let key of Object.keys(input)) { | ||
const val = input[key]; | ||
const value = input[key]; | ||
let pushArg = makeArg; | ||
if (Array.isArray(opts.excludes) && match(opts.excludes, key)) { | ||
if (Array.isArray(options.excludes) && match(options.excludes, key)) { | ||
continue; | ||
} | ||
if (Array.isArray(opts.includes) && !match(opts.includes, key)) { | ||
if (Array.isArray(options.includes) && !match(options.includes, key)) { | ||
continue; | ||
} | ||
if (typeof opts.aliases === 'object' && opts.aliases[key]) { | ||
key = opts.aliases[key]; | ||
if (typeof options.aliases === 'object' && options.aliases[key]) { | ||
key = options.aliases[key]; | ||
pushArg = makeAliasArg; | ||
@@ -58,7 +63,9 @@ } | ||
if (key === '--') { | ||
if (!Array.isArray(val)) { | ||
throw new TypeError(`Expected key \`--\` to be Array, got ${typeof val}`); | ||
if (!Array.isArray(value)) { | ||
throw new TypeError( | ||
`Expected key \`--\` to be Array, got ${typeof value}` | ||
); | ||
} | ||
separatedArgs = val; | ||
separatedArgs = value; | ||
continue; | ||
@@ -68,28 +75,30 @@ } | ||
if (key === '_') { | ||
if (!Array.isArray(val)) { | ||
throw new TypeError(`Expected key \`_\` to be Array, got ${typeof val}`); | ||
if (!Array.isArray(value)) { | ||
throw new TypeError( | ||
`Expected key \`_\` to be Array, got ${typeof value}` | ||
); | ||
} | ||
extraArgs = val; | ||
extraArgs = value; | ||
continue; | ||
} | ||
if (val === true) { | ||
if (value === true) { | ||
pushArg(key, ''); | ||
} | ||
if (val === false && !opts.ignoreFalse) { | ||
if (value === false && !options.ignoreFalse) { | ||
pushArg(`no-${key}`); | ||
} | ||
if (typeof val === 'string') { | ||
pushArg(key, val); | ||
if (typeof value === 'string') { | ||
pushArg(key, value); | ||
} | ||
if (typeof val === 'number' && !Number.isNaN(val)) { | ||
pushArg(key, String(val)); | ||
if (typeof value === 'number' && !Number.isNaN(value)) { | ||
pushArg(key, String(value)); | ||
} | ||
if (Array.isArray(val)) { | ||
for (const arrayValue of val) { | ||
if (Array.isArray(value)) { | ||
for (const arrayValue of value) { | ||
pushArg(key, arrayValue); | ||
@@ -114,1 +123,3 @@ } | ||
}; | ||
module.exports = dargs; |
{ | ||
"name": "dargs", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"description": "Reverse minimist. Convert an object of options into an array of command-line arguments.", | ||
@@ -16,6 +16,7 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && ava && tsd" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -46,5 +47,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -22,2 +22,3 @@ # dargs [![Build Status](https://travis-ci.org/sindresorhus/dargs.svg?branch=master)](https://travis-ci.org/sindresorhus/dargs) | ||
_: ['some', 'option'], // Values in '_' will be appended to the end of the generated argument list | ||
'--': ['separated', 'option'], // Values in '--' will be put at the very end of the argument list after the escape option (`--`) | ||
foo: 'bar', | ||
@@ -46,2 +47,5 @@ hello: true, // Results in only the key being used | ||
'some', | ||
'option', | ||
'--', | ||
'separated', | ||
'option' | ||
@@ -48,0 +52,0 @@ ] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10648
5
206
168
3
1