Comparing version 4.0.1 to 4.1.0
52
index.js
'use strict'; | ||
var numberIsNan = require('number-is-nan'); | ||
function createArg(key, val) { | ||
function createArg(key, val, separator) { | ||
key = key.replace(/[A-Z]/g, '-$&').toLowerCase(); | ||
return '--' + key + (val ? '=' + val : ''); | ||
}; | ||
return '--' + key + (val ? separator + val : ''); | ||
} | ||
function match(arr, val) { | ||
return arr.some(function (x) { | ||
return x instanceof RegExp ? x.test(val) : x === val; | ||
}); | ||
} | ||
function createAliasArg(key, val) { | ||
return '-' + key + (val ? ' ' + val : ''); | ||
} | ||
module.exports = function (input, opts) { | ||
var args = []; | ||
var extraArgs = []; | ||
opts = opts || {}; | ||
var separator = opts.useEquals === false ? ' ' : '='; | ||
Object.keys(input).forEach(function (key) { | ||
var val = input[key]; | ||
var argFn = createArg; | ||
if (Array.isArray(opts.excludes) && opts.excludes.indexOf(key) !== -1) { | ||
if (Array.isArray(opts.excludes) && match(opts.excludes, key)) { | ||
return; | ||
} | ||
if (Array.isArray(opts.includes) && opts.includes.indexOf(key) === -1) { | ||
if (Array.isArray(opts.includes) && !match(opts.includes, key)) { | ||
return; | ||
} | ||
if (typeof opts.aliases === 'object' && opts.aliases[key]) { | ||
key = opts.aliases[key]; | ||
argFn = createAliasArg; | ||
} | ||
if (key === '_') { | ||
if (!Array.isArray(val)) { | ||
throw new TypeError('Expected key \'_\' to be an array, but found ' + (typeof val)); | ||
} | ||
extraArgs = val; | ||
return; | ||
} | ||
if (val === true) { | ||
args.push(createArg(key)); | ||
args.push(argFn(key, '')); | ||
} | ||
if (val === false && !opts.ignoreFalse) { | ||
args.push(createArg('no-' + key)); | ||
args.push(argFn('no-' + key)); | ||
} | ||
if (typeof val === 'string') { | ||
args.push(createArg(key, val)); | ||
args.push(argFn(key, val, separator)); | ||
} | ||
if (typeof val === 'number' && !numberIsNan(val)) { | ||
args.push(createArg(key, '' + val)); | ||
args.push(argFn(key, String(val), separator)); | ||
} | ||
@@ -43,3 +71,3 @@ | ||
val.forEach(function (arrVal) { | ||
args.push(createArg(key, arrVal)); | ||
args.push(argFn(key, arrVal, separator)); | ||
}); | ||
@@ -49,3 +77,7 @@ } | ||
extraArgs.forEach(function (extraArgVal) { | ||
args.push(String(extraArgVal)); | ||
}); | ||
return args; | ||
}; |
{ | ||
"name": "dargs", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Reverse minimist. Convert an object of options into an array of command-line arguments.", | ||
@@ -34,3 +34,3 @@ "repository": "sindresorhus/dargs", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "xo && ava" | ||
}, | ||
@@ -42,3 +42,4 @@ "dependencies": { | ||
"array-equal": "^1.0.0", | ||
"ava": "^0.0.4" | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
@@ -45,0 +46,0 @@ "engines": { |
@@ -18,5 +18,6 @@ # dargs [![Build Status](https://travis-ci.org/sindresorhus/dargs.svg?branch=master)](https://travis-ci.org/sindresorhus/dargs) | ||
```js | ||
var dargs = require('dargs'); | ||
const dargs = require('dargs'); | ||
var input = { | ||
const input = { | ||
_: ['some', 'option'], // values in '_' will be appended to the end of the generated argument list | ||
foo: 'bar', | ||
@@ -27,7 +28,9 @@ hello: true, // results in only the key being used | ||
multiple: ['value', 'value2'], // converted to multiple arguments | ||
pieKind: 'cherry', | ||
sad: ':(' | ||
}; | ||
var excludes = ['sad']; | ||
var includes = ['camelCase', 'multiple', 'sad']; | ||
const excludes = ['sad', /.*Kind$/]; // excludes and includes accept regular expressions | ||
const includes = ['camelCase', 'multiple', 'sad', /^pie.*/]; | ||
const aliases = {file: 'f'}; | ||
@@ -42,3 +45,5 @@ console.log(dargs(input, {excludes: excludes})); | ||
'--multiple=value', | ||
'--multiple=value2' | ||
'--multiple=value2', | ||
'some', | ||
'option' | ||
] | ||
@@ -66,5 +71,22 @@ */ | ||
'--multiple=value2', | ||
'--sad=:('' | ||
'--pie-kind=cherry', | ||
'--sad=:(' | ||
] | ||
*/ | ||
console.log(dargs({ | ||
foo: 'bar', | ||
hello: true, | ||
file: 'baz' | ||
}, { | ||
aliases: aliases | ||
})); | ||
/* | ||
[ | ||
'--foo=bar', | ||
'--hello', | ||
'-f baz' | ||
] | ||
*/ | ||
``` | ||
@@ -74,7 +96,6 @@ | ||
### dargs(input, options) | ||
### dargs(input, [options]) | ||
#### input | ||
*Required* | ||
Type: `object` | ||
@@ -92,3 +113,3 @@ | ||
Keys to exclude. Takes precedence over `includes`. | ||
Keys or regex of keys to exclude. Takes precedence over `includes`. | ||
@@ -99,4 +120,26 @@ ##### includes | ||
Keys to include. | ||
Keys or regex of keys to include. | ||
##### aliases | ||
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`. | ||
##### useEquals | ||
Type: `boolean` | ||
Default: `true` | ||
Setting to `false` switches the separator in generated commands from an equals sign `=` to a single space ` `. For example: | ||
```js | ||
console.log(dargs({foo: 'bar'}, {useEquals: false})); | ||
/* | ||
[ | ||
'--foo bar' | ||
] | ||
*/ | ||
``` | ||
##### ignoreFalse | ||
@@ -103,0 +146,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
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
6474
62
149
3