Merge pull request #142 from vjeux/minimize_quotes

Minimize string escapes
This commit is contained in:
James Long authored and GitHub committed 2017-01-12 13:39:17 -05:00
commit a1d9d2a02f
3 files changed
+16 -7

No files matched your search

+12 -1
View File
@@ -2059,7 +2059,18 @@ function swapQuotes(str) {
function nodeStr(str, options) {
isString.assert(str);
if (options.singleQuote) {
const containsSingleQuote = str.indexOf("'") !== -1;
const containsDoubleQuote = str.indexOf('"') !== -1;
let shouldUseSingleQuote = options.singleQuote;
if (options.singleQuote && containsSingleQuote && !containsDoubleQuote) {
shouldUseSingleQuote = false;
}
if (!options.singleQuote && !containsSingleQuote && containsDoubleQuote) {
shouldUseSingleQuote = true;
}
if (shouldUseSingleQuote) {
return swapQuotes(JSON.stringify(swapQuotes(str)));
} else {
return JSON.stringify(str);
@@ -164,9 +164,7 @@ var ls = child_process.spawn(\"ls\");
var wc = child_process.spawn(\"wc\", [ \"-l\" ]);
// args + options.
child_process.spawn(\"echo\", [ \"-n\", \"\\\"Testing...\\\"\" ], {
env: { TEST: \"foo\" }
});
child_process.spawn(\"echo\", [ \"-n\", \'\"Testing...\"\' ], { env: { TEST: \"foo\" } });
// options only.
child_process.spawn(\"echo\", { env: { FOO: 2 } });
@@ -26,9 +26,9 @@ exports[`test strings.js 1`] = `
\"\'\";
\"\\\"\";
\"\\\"\";
\"\\\\\\\"\";
\'\"\';
\'\"\';
\'\\\\\"\';
\"\'\";
\"\'\";