Comparing version 4.1.0 to 5.0.0
70
index.js
'use strict'; | ||
var numberIsNan = require('number-is-nan'); | ||
function createArg(key, val, separator) { | ||
key = key.replace(/[A-Z]/g, '-$&').toLowerCase(); | ||
return '--' + key + (val ? separator + val : ''); | ||
} | ||
const match = (arr, val) => arr.some(x => x instanceof RegExp ? x.test(val) : x === val); | ||
function match(arr, val) { | ||
return arr.some(function (x) { | ||
return x instanceof RegExp ? x.test(val) : x === val; | ||
}); | ||
} | ||
module.exports = (input, opts) => { | ||
const args = []; | ||
let extraArgs = []; | ||
function createAliasArg(key, val) { | ||
return '-' + key + (val ? ' ' + val : ''); | ||
} | ||
opts = Object.assign({ | ||
useEquals: true | ||
}, opts); | ||
module.exports = function (input, opts) { | ||
var args = []; | ||
var extraArgs = []; | ||
const makeArg = (key, val) => { | ||
key = '--' + key.replace(/[A-Z]/g, '-$&').toLowerCase(); // eslint-disable-line prefer-template | ||
opts = opts || {}; | ||
if (opts.useEquals) { | ||
args.push(key + (val ? `=${val}` : '')); | ||
} else { | ||
args.push(key); | ||
var separator = opts.useEquals === false ? ' ' : '='; | ||
if (val) { | ||
args.push(val); | ||
} | ||
} | ||
}; | ||
Object.keys(input).forEach(function (key) { | ||
var val = input[key]; | ||
var argFn = createArg; | ||
const makeAliasArg = (key, val) => { | ||
args.push(`-${key}`); | ||
if (val) { | ||
args.push(val); | ||
} | ||
}; | ||
Object.keys(input).forEach(key => { | ||
const val = input[key]; | ||
let pushArg = makeArg; | ||
if (Array.isArray(opts.excludes) && match(opts.excludes, key)) { | ||
@@ -41,3 +49,3 @@ return; | ||
key = opts.aliases[key]; | ||
argFn = createAliasArg; | ||
pushArg = makeAliasArg; | ||
} | ||
@@ -47,3 +55,3 @@ | ||
if (!Array.isArray(val)) { | ||
throw new TypeError('Expected key \'_\' to be an array, but found ' + (typeof val)); | ||
throw new TypeError(`Expected key \`_\` to be Array, got ${typeof val}`); | ||
} | ||
@@ -56,20 +64,20 @@ | ||
if (val === true) { | ||
args.push(argFn(key, '')); | ||
pushArg(key, ''); | ||
} | ||
if (val === false && !opts.ignoreFalse) { | ||
args.push(argFn('no-' + key)); | ||
pushArg(`no-${key}`); | ||
} | ||
if (typeof val === 'string') { | ||
args.push(argFn(key, val, separator)); | ||
pushArg(key, val); | ||
} | ||
if (typeof val === 'number' && !numberIsNan(val)) { | ||
args.push(argFn(key, String(val), separator)); | ||
if (typeof val === 'number' && !Number.isNaN(val)) { | ||
pushArg(key, String(val)); | ||
} | ||
if (Array.isArray(val)) { | ||
val.forEach(function (arrVal) { | ||
args.push(argFn(key, arrVal, separator)); | ||
val.forEach(arrVal => { | ||
pushArg(key, arrVal); | ||
}); | ||
@@ -79,3 +87,3 @@ } | ||
extraArgs.forEach(function (extraArgVal) { | ||
extraArgs.forEach(extraArgVal => { | ||
args.push(String(extraArgVal)); | ||
@@ -82,0 +90,0 @@ }); |
{ | ||
"name": "dargs", | ||
"version": "4.1.0", | ||
"version": "5.0.0", | ||
"description": "Reverse minimist. Convert an object of options into an array of command-line arguments.", | ||
"license": "MIT", | ||
"repository": "sindresorhus/dargs", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"reverse", | ||
"minimist", | ||
"options", | ||
@@ -13,3 +30,3 @@ "arguments", | ||
"nopt", | ||
"minimist", | ||
"commander", | ||
"bin", | ||
@@ -19,3 +36,2 @@ "binary", | ||
"cmd", | ||
"reverse", | ||
"inverse", | ||
@@ -30,25 +46,9 @@ "opposite", | ||
], | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"dependencies": { | ||
"number-is-nan": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"array-equal": "^1.0.0", | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
"index.js" | ||
] | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
@@ -15,3 +15,3 @@ # dargs [![Build Status](https://travis-ci.org/sindresorhus/dargs.svg?branch=master)](https://travis-ci.org/sindresorhus/dargs) | ||
#### Usage | ||
## Usage | ||
@@ -36,3 +36,3 @@ ```js | ||
console.log(dargs(input, {excludes: excludes})); | ||
console.log(dargs(input, {excludes})); | ||
/* | ||
@@ -51,6 +51,3 @@ [ | ||
console.log(dargs(input, { | ||
excludes: excludes, | ||
includes: includes | ||
})); | ||
console.log(dargs(input, {excludes, includes})); | ||
/* | ||
@@ -65,3 +62,3 @@ [ | ||
console.log(dargs(input, {includes: includes})); | ||
console.log(dargs(input, {includes})); | ||
/* | ||
@@ -82,5 +79,3 @@ [ | ||
file: 'baz' | ||
}, { | ||
aliases: aliases | ||
})); | ||
}, {aliases})); | ||
/* | ||
@@ -90,3 +85,3 @@ [ | ||
'--hello', | ||
'-f baz' | ||
'-f', 'baz' | ||
] | ||
@@ -96,2 +91,3 @@ */ | ||
## API | ||
@@ -103,3 +99,3 @@ | ||
Type: `object` | ||
Type: `Object` | ||
@@ -110,7 +106,7 @@ Object to convert to command-line arguments. | ||
Type: `object` | ||
Type: `Object` | ||
##### excludes | ||
Type: `array` | ||
Type: `Array` | ||
@@ -121,3 +117,3 @@ Keys or regex of keys to exclude. Takes precedence over `includes`. | ||
Type: `array` | ||
Type: `Array` | ||
@@ -128,12 +124,12 @@ Keys or regex of keys to include. | ||
Type: `object` | ||
Type: `Object` | ||
Maps keys in `input` to an aliased name. Matching keys are converted to options with a single dash ("-") in front of the aliased name and a space separating the aliased name from the value. Keys are still affected by `includes` and `excludes`. | ||
Maps keys in `input` to an aliased name. Matching keys are converted to arguments with a single dash (`-`) in front of the aliased key and the value in a separate array item. Keys are still affected by `includes` and `excludes`. | ||
##### useEquals | ||
Type: `boolean` | ||
Type: `boolean`<br> | ||
Default: `true` | ||
Setting to `false` switches the separator in generated commands from an equals sign `=` to a single space ` `. For example: | ||
Setting this to `false` makes it return the key and value as separate array items instead of using a `=` separator in one item. This can be useful for tools that doesn't support `--foo=bar` style flags. For example: | ||
@@ -144,3 +140,3 @@ ```js | ||
[ | ||
'--foo bar' | ||
'--foo', 'bar' | ||
] | ||
@@ -152,6 +148,6 @@ */ | ||
Type: `boolean` | ||
Type: `boolean`<br> | ||
Default: `false` | ||
Don't include `false` values. This is mostly useful when dealing with strict argument parsers that would throw on unknown arguments like `--no-foo`. | ||
Exclude `false` values. Can be useful when dealing with strict argument parsers that throw on unknown arguments like `--no-foo`. | ||
@@ -161,2 +157,2 @@ | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
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
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
0
2
68
6384
145
- Removednumber-is-nan@^1.0.0
- Removednumber-is-nan@1.0.1(transitive)