Skip to content

Commit

Permalink
Fix quote() with special chars
Browse files Browse the repository at this point in the history
Add some failing tests and fix them

I noticed that 'echo ' + quote(["'#"]) => echo "\'\#"
which produces:
\'\#

which is wrong.

We are escaping lots of things inside double-quotes that we don't need
to, which ends up including the \ before each escaped char.
  • Loading branch information
danielbeardsley committed Dec 10, 2014
1 parent cf747ca commit 811b5a0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ exports.quote = function (xs) {
return "'" + s.replace(/(['\\])/g, '\\$1') + "'";
}
else if (/["'\s]/.test(s)) {
return '"' + s.replace(/(["\\$`(){}!#&*|])/g, '\\$1') + '"';
return '"' + s.replace(/(["\\$`!])/g, '\\$1') + '"';
}
else {
return String(s).replace(/([\\$`(){}!#&*|])/g, '\\$1');
return String(s).replace(/([\\$`()!#&*|])/g, '\\$1');
}
}).join(' ');
};
Expand Down
5 changes: 5 additions & 0 deletions test/quote.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ test('quote', function (t) {
'\\$ \\` "\'"'
);
t.equal(quote([]), '');
t.equal(quote(["a\nb"]), "'a\nb'");
t.equal(quote([' #(){}*|][!']), "' #(){}*|][!'");
t.equal(quote(["'#(){}*|][!"]), '"\'#(){}*|][\\!"');
t.equal(quote(["X#(){}*|][!"]), "X\\#\\(\\){}\\*\\|][\\!");
t.equal(quote(["a\n#\nb"]), "'a\n#\nb'");
t.equal(quote([ 'a', 1, true, false ]), 'a 1 true false');
t.equal(quote([ 'a', 1, null, undefined ]), 'a 1 null undefined');
t.end();
Expand Down

0 comments on commit 811b5a0

Please sign in to comment.