[Pkg-javascript-commits] [node-dargs] 10/54: Close GH-7: Wrap option value with strong quotes and escape it.

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 f352e394512dfcaafc2624fa7c11be98dcee25cc
Author: Radko Dinev <radko.dinev at gmail.com>
Date:   Tue Aug 26 18:10:16 2014 +0200

    Close GH-7: Wrap option value with strong quotes and escape it.
---
 index.js | 28 ++++++++++++++++++----------
 test.js  | 24 ++++++++++++++----------
 2 files changed, 32 insertions(+), 20 deletions(-)

diff --git a/index.js b/index.js
index b736886..0bb67c2 100644
--- a/index.js
+++ b/index.js
@@ -1,32 +1,40 @@
 'use strict';
+
+var escape = function (value) {
+	return value.replace(/'/g, "'\''");
+};
+
+var constructOption = function (name, value) {
+	name = name.replace(/[A-Z]/g, '-$&').toLowerCase();
+
+	return '--' + name + (value ? "='" + escape(value) + "'": '');
+};
+
 module.exports = function (options, excludes) {
 	var args = [];
 
-	Object.keys(options).forEach(function (key) {
-		var flag;
-		var val = options[key];
+	Object.keys(options).forEach(function (name) {
+		var val = options[name];
 
-		if (Array.isArray(excludes) && excludes.indexOf(key) !== -1) {
+		if (Array.isArray(excludes) && excludes.indexOf(name) !== -1) {
 			return;
 		}
 
-		flag = key.replace(/[A-Z]/g, '-$&').toLowerCase();
-
 		if (val === true) {
-			args.push('--' + flag);
+			args.push(constructOption(name));
 		}
 
 		if (typeof val === 'string') {
-			args.push('--' + flag + '=' + val);
+			args.push(constructOption(name, val));
 		}
 
 		if (typeof val === 'number' && isNaN(val) === false) {
-			args.push('--' + flag + '=' + ('' + val));
+			args.push(constructOption(name, '' + val));
 		}
 
 		if (Array.isArray(val)) {
 			val.forEach(function (arrVal) {
-				args.push('--' + flag + '=' + arrVal);
+				args.push(constructOption(name, arrVal));
 			});
 		}
 	});
diff --git a/test.js b/test.js
index 3c48d04..e3b6207 100644
--- a/test.js
+++ b/test.js
@@ -10,6 +10,8 @@ var fixture = {
 	e: ['foo', 'bar'],
 	f: null,
 	g: undefined,
+	h: 'with a space',
+	i: "let's try quotes",
 	camelCaseCamel: true
 };
 
@@ -17,22 +19,24 @@ 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',
-			'--camel-case-camel'
+			"--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']);
+		var actual = dargs(fixture, ['b', 'e', 'h', 'i']);
 		var expected = [
-			'--a=foo',
-			'--d=5',
-			'--camel-case-camel'
+			"--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