[Pkg-javascript-commits] [node-dargs] 17/54: various tweaks

Bastien Roucariès rouca at moszumanska.debian.org
Wed Sep 6 09:41:02 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 a29230e8ec215768dd14d3c3c5e7d42a011dabe8
Author: Sindre Sorhus <sindresorhus at gmail.com>
Date:   Wed Aug 27 22:52:28 2014 +0200

    various tweaks
---
 index.js     | 28 +++++++++++-----------------
 package.json |  8 ++++++--
 readme.md    |  6 ++----
 test.js      | 46 ++++++++++++++++++++++------------------------
 4 files changed, 41 insertions(+), 47 deletions(-)

diff --git a/index.js b/index.js
index 47ddb41..c547c1d 100644
--- a/index.js
+++ b/index.js
@@ -1,41 +1,35 @@
 'use strict';
 
-var escape = function (value) {
-	return value.replace(/'/g, "'\''");
+function createArg(key, val) {
+	key = key.replace(/[A-Z]/g, '-$&').toLowerCase();
+	return '--' + key + (val ? '=' + val : '');
 };
 
-var constructOption = function (name, value) {
-	name = name.replace(/[A-Z]/g, '-$&').toLowerCase();
-	//var quotedVal = /\s/.test(value) ? ("'" + escape(value) + "'") : value;
-
-	return '--' + name + (value ? "=" + value : '');
-};
-
-module.exports = function (options, excludes) {
+module.exports = function (opts, excludes) {
 	var args = [];
 
-	Object.keys(options).forEach(function (name) {
-		var val = options[name];
+	Object.keys(opts).forEach(function (key) {
+		var val = opts[key];
 
-		if (Array.isArray(excludes) && excludes.indexOf(name) !== -1) {
+		if (Array.isArray(excludes) && excludes.indexOf(key) !== -1) {
 			return;
 		}
 
 		if (val === true) {
-			args.push(constructOption(name));
+			args.push(createArg(key));
 		}
 
 		if (typeof val === 'string') {
-			args.push(constructOption(name, val));
+			args.push(createArg(key, val));
 		}
 
 		if (typeof val === 'number' && isNaN(val) === false) {
-			args.push(constructOption(name, '' + val));
+			args.push(createArg(key, '' + val));
 		}
 
 		if (Array.isArray(val)) {
 			val.forEach(function (arrVal) {
-				args.push(constructOption(name, arrVal));
+				args.push(createArg(key, arrVal));
 			});
 		}
 	});
diff --git a/package.json b/package.json
index da88aaf..ee256e4 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
 {
   "name": "dargs",
   "version": "2.0.3",
-  "description": "Converts an object of options into an array of command-line arguments",
+  "description": "Convert an object of options into an array of command-line arguments",
   "repository": "sindresorhus/dargs",
   "keywords": [
     "options",
@@ -10,7 +10,11 @@
     "flags",
     "cli",
     "nopt",
-    "minimist"
+    "minimist",
+    "bin",
+    "binary",
+    "command",
+    "cmd"
   ],
   "author": {
     "name": "Sindre Sorhus",
diff --git a/readme.md b/readme.md
index 2ca8929..d600efe 100644
--- a/readme.md
+++ b/readme.md
@@ -1,8 +1,8 @@
 # dargs [![Build Status](https://travis-ci.org/sindresorhus/dargs.svg?branch=master)](https://travis-ci.org/sindresorhus/dargs)
 
-> Converts an object of options into an array of command-line arguments
+> Convert an object of options into an array of command-line arguments
 
-Basically the inverse of an argument parser like nopt or minimist. Useful when calling command-line tools.
+Basically the inverse of an argument parser like minimist. Useful when spawning command-line tools.
 
 
 ## Install
@@ -19,7 +19,6 @@ var dargs = require('dargs');
 
 var options = {
 	foo: 'bar',
-	unicorn: 'rainbows and ponies', // quoted when more than just a word
 	hello: true,                    // results in only the key being used
 	cake: false,                    // ignored
 	camelCase: 5,                   // camelCase is slugged to `camel-case`
@@ -34,7 +33,6 @@ console.log(dargs(options, excludes));
 /*
 [
 	"--foo=bar",
-	"--unicorn='rainbows and ponies'"
 	"--hello",
 	"--camel-case=5",
 	"--multiple=value",
diff --git a/test.js b/test.js
index b910aad..281fafa 100644
--- a/test.js
+++ b/test.js
@@ -15,29 +15,27 @@ var fixture = {
 	camelCaseCamel: true
 };
 
-describe('dargs()', function () {
-	it('convert options to cli flags', function () {
-		var actual = dargs(fixture);
-		var expected = [
-			"--a=foo",
-			"--b",
-			"--d=5",
-			"--e=foo",
-			"--e=bar",
-			"--h='with a space'",
-			"--i='let'\''s try quotes'",
-			"--camel-case-camel"
-		];
-		assert.deepEqual(actual, expected);
-	});
+it('convert options to cli flags', function () {
+	var actual = dargs(fixture);
+	var expected = [
+		"--a=foo",
+		"--b",
+		"--d=5",
+		"--e=foo",
+		"--e=bar",
+		"--h=with a space",
+		"--i=let's try quotes",
+		"--camel-case-camel"
+	];
+	assert.deepEqual(actual, expected);
+});
 
-	it('exclude options', function () {
-		var actual = dargs(fixture, ['b', 'e', 'h', 'i']);
-		var expected = [
-			"--a=foo",
-			"--d=5",
-			"--camel-case-camel"
-		];
-		assert.deepEqual(actual, expected);
-	});
+it('exclude options', function () {
+	var actual = dargs(fixture, ['b', 'e', 'h', 'i']);
+	var expected = [
+		"--a=foo",
+		"--d=5",
+		"--camel-case-camel"
+	];
+	assert.deepEqual(actual, expected);
 });

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