[Pkg-javascript-commits] [node-dargs] 27/54: [breaking] change method signature and add `ignoreFalse` option

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:41:04 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 5483bc24215c6fd70a5a402de1fd4eed02b22998
Author: Sindre Sorhus <sindresorhus at gmail.com>
Date:   Fri Feb 6 23:47:48 2015 +0700

    [breaking] change method signature and add `ignoreFalse` option
    
    method signature changed from `dargs(input, excludes, includes)` to `dargs(input, options)`, where options can be an object with `exclues`, `includes`, `ignoreFalse`.
---
 index.js  | 14 ++++++++------
 readme.md | 36 +++++++++++++++++++++++++-----------
 test.js   | 17 +++++++++++++----
 3 files changed, 46 insertions(+), 21 deletions(-)

diff --git a/index.js b/index.js
index 3a27b5d..53288e6 100644
--- a/index.js
+++ b/index.js
@@ -5,17 +5,19 @@ function createArg(key, val) {
 	return '--' + key + (val ? '=' + val : '');
 };
 
-module.exports = function (opts, excludes, includes) {
+module.exports = function (input, opts) {
 	var args = [];
 
-	Object.keys(opts).forEach(function (key) {
-		var val = opts[key];
+	opts = opts || {};
 
-		if (Array.isArray(excludes) && excludes.indexOf(key) !== -1) {
+	Object.keys(input).forEach(function (key) {
+		var val = input[key];
+
+		if (Array.isArray(opts.excludes) && opts.excludes.indexOf(key) !== -1) {
 			return;
 		}
 
-		if (Array.isArray(includes) && includes.indexOf(key) === -1) {
+		if (Array.isArray(opts.includes) && opts.includes.indexOf(key) === -1) {
 			return;
 		}
 
@@ -23,7 +25,7 @@ module.exports = function (opts, excludes, includes) {
 			args.push(createArg(key));
 		}
 
-		if (val === false) {
+		if (val === false && !opts.ignoreFalse) {
 			args.push(createArg('no-' + key));
 		}
 
diff --git a/readme.md b/readme.md
index f424da3..f9d990e 100644
--- a/readme.md
+++ b/readme.md
@@ -17,7 +17,7 @@ $ npm install --save dargs
 ```js
 var dargs = require('dargs');
 
-var options = {
+var input = {
 	foo: 'bar',
 	hello: true,                    // results in only the key being used
 	cake: false,                    // prepends `no-` before the key
@@ -29,7 +29,7 @@ var options = {
 var excludes = ['sad'];
 var includes = ['camelCase', 'multiple', 'sad'];
 
-console.log(dargs(options, excludes));
+console.log(dargs(input, {excludes: excludes}));
 /*
 [
 	'--foo=bar',
@@ -41,7 +41,10 @@ console.log(dargs(options, excludes));
 ]
 */
 
-console.log(dargs(options, excludes, includes));
+console.log(dargs(input, {
+	excludes: excludes,
+	includes: includes
+}));
 /*
 [
 	'--camel-case=5',
@@ -51,7 +54,7 @@ console.log(dargs(options, excludes, includes));
 */
 
 
-console.log(dargs(options, [], includes));
+console.log(dargs(input, {includes: includes}));
 /*
 [
 	'--camel-case=5',
@@ -64,27 +67,38 @@ console.log(dargs(options, [], includes));
 
 ## API
 
-### dargs(options, excludes, includes)
+### dargs(input, options)
 
-#### options
+#### input
 
+*Required*  
 Type: `object`
 
-Options to convert to command-line arguments.
+Object to convert to command-line arguments.
+
+#### options
+
+Type: `object`
 
-#### excludes
+##### excludes
 
 Type: `array`
 
-Keys to exclude.  
-Takes precedence over `includes`.
+Keys to exclude. Takes precedence over `includes`.
 
-#### includes
+##### includes
 
 Type: `array`
 
 Keys to include.
 
+##### ignoreFalse
+
+Type: `boolean`  
+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`.
+
 
 ## License
 
diff --git a/test.js b/test.js
index a2e231a..ff0e1a5 100644
--- a/test.js
+++ b/test.js
@@ -12,7 +12,7 @@ var fixture = {
 	f: null,
 	g: undefined,
 	h: 'with a space',
-	i: "let's try quotes",
+	i: 'let\'s try quotes',
 	camelCaseCamel: true
 };
 
@@ -34,7 +34,7 @@ test('convert options to cli flags', function (t) {
 });
 
 test('exclude options', function (t) {
-	var actual = dargs(fixture, ['b', 'e', 'h', 'i']);
+	var actual = dargs(fixture, {excludes: ['b', 'e', 'h', 'i']});
 	var expected = [
 		'--a=foo',
 		'--no-c',
@@ -46,7 +46,7 @@ test('exclude options', function (t) {
 });
 
 test('includes options', function (t) {
-	var actual = dargs(fixture, [], ['a', 'c', 'd', 'e', 'camelCaseCamel']);
+	var actual = dargs(fixture, {includes: ['a', 'c', 'd', 'e', 'camelCaseCamel']});
 	var expected = [
 		'--a=foo',
 		'--no-c',
@@ -60,7 +60,10 @@ test('includes options', function (t) {
 });
 
 test('excludes and includes options', function (t) {
-	var actual = dargs(fixture, ['a', 'd'], ['a', 'c', 'd', 'e', 'camelCaseCamel']);
+	var actual = dargs(fixture, {
+		excludes: ['a', 'd'],
+		includes: ['a', 'c', 'd', 'e', 'camelCaseCamel']
+	});
 	var expected = [
 		'--no-c',
 		'--e=foo',
@@ -70,3 +73,9 @@ test('excludes and includes options', function (t) {
 	t.assert(deepEqual(actual, expected));
 	t.end();
 });
+
+test('option to ignore false values', function (t) {
+	var actual = dargs({foo: false}, {ignoreFalse: true});
+	t.assert(deepEqual(actual, []));
+	t.end();
+});

-- 
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