[Pkg-javascript-commits] [node-dargs] 45/54: make `useEquals: false` behave as expected

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:41:05 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-dargs.

commit 4e16e99b15c84dc204eb8c01157562c8df9064a0
Author: Sindre Sorhus <sindresorhus at gmail.com>
Date:   Fri Jul 1 00:07:48 2016 +0200

    make `useEquals: false` behave as expected
    
    ```js
    dargs({foo: 'bar'}, {useEquals: false});
    // before  => ['--foo bar']
    // now     => ['--foo', 'bar']
    ```
---
 index.js  | 44 +++++++++++++++++++++++++++++++-------------
 readme.md |  6 +++---
 test.js   | 16 ++++++++--------
 3 files changed, 42 insertions(+), 24 deletions(-)

diff --git a/index.js b/index.js
index deb3cfe..cc2afab 100644
--- a/index.js
+++ b/index.js
@@ -1,9 +1,16 @@
 'use strict';
 var numberIsNan = require('number-is-nan');
 
-function createArg(key, val, separator) {
+function createArg(key, val) {
 	key = key.replace(/[A-Z]/g, '-$&').toLowerCase();
-	return '--' + key + (val ? separator + val : '');
+
+	var ret = ['--' + key];
+
+	if (val) {
+		ret.push(val);
+	}
+
+	return ret;
 }
 
 function match(arr, val) {
@@ -12,21 +19,32 @@ function match(arr, 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 ? ' ' : '=';
+	var createAliasArg = function (key, val) {
+		args.push('-' + key);
+
+		if (val) {
+			args.push(val);
+		}
+	};
 
 	Object.keys(input).forEach(function (key) {
 		var val = input[key];
-		var argFn = createArg;
+
+		var argFn = (key, val) => {
+			var arg = createArg(key, val);
+
+			if (opts.useEquals === false) {
+				args.push.apply(args, arg);
+			} else {
+				args.push(arg.join('='));
+			}
+		};
 
 		if (Array.isArray(opts.excludes) && match(opts.excludes, key)) {
 			return;
@@ -51,24 +69,24 @@ module.exports = function (input, opts) {
 		}
 
 		if (val === true) {
-			args.push(argFn(key, ''));
+			argFn(key, '');
 		}
 
 		if (val === false && !opts.ignoreFalse) {
-			args.push(argFn('no-' + key));
+			argFn('no-' + key);
 		}
 
 		if (typeof val === 'string') {
-			args.push(argFn(key, val, separator));
+			argFn(key, val);
 		}
 
 		if (typeof val === 'number' && !numberIsNan(val)) {
-			args.push(argFn(key, String(val), separator));
+			argFn(key, String(val));
 		}
 
 		if (Array.isArray(val)) {
 			val.forEach(function (arrVal) {
-				args.push(argFn(key, arrVal, separator));
+				argFn(key, arrVal);
 			});
 		}
 	});
diff --git a/readme.md b/readme.md
index ea7ac26..aa1c6fc 100644
--- a/readme.md
+++ b/readme.md
@@ -82,7 +82,7 @@ console.log(dargs({
 [
 	'--foo=bar',
 	'--hello',
-	'-f baz'
+	'-f', 'baz'
 ]
 */
 ```
@@ -124,13 +124,13 @@ Maps keys in `input` to an aliased name. Matching keys are converted to options
 Type: `boolean`  
 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:
 
 ```js
 console.log(dargs({foo: 'bar'}, {useEquals: false}));
 /*
 [
-    '--foo bar'
+    '--foo', 'bar'
 ]
 */
 ```
diff --git a/test.js b/test.js
index 79bd7df..cb39bff 100644
--- a/test.js
+++ b/test.js
@@ -39,14 +39,14 @@ test('useEquals options', t => {
 	t.deepEqual(fn(fixture, {
 		useEquals: false
 	}), [
-		'--a foo',
+		'--a', 'foo',
 		'--b',
 		'--no-c',
-		'--d 5',
-		'--e foo',
-		'--e bar',
-		'--h with a space',
-		'--i let\'s try quotes',
+		'--d', '5',
+		'--e', 'foo',
+		'--e', 'bar',
+		'--h', 'with a space',
+		'--i', 'let\'s try quotes',
 		'--camel-case-camel',
 		'some',
 		'option'
@@ -96,7 +96,7 @@ test('aliases option', t => {
 		aliases: {file: 'f'}
 	}), [
 		'--a=foo',
-		'-f test'
+		'-f', 'test'
 	]);
 });
 
@@ -105,7 +105,7 @@ test('includes and aliases options', t => {
 		includes: ['a', 'c', 'd', 'e', 'camelCaseCamel'],
 		aliases: {a: 'a'}
 	}), [
-		'-a foo',
+		'-a', 'foo',
 		'--no-c',
 		'--d=5',
 		'--e=foo',

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-dargs.git



More information about the Pkg-javascript-commits mailing list