Skip to content

Commit

Permalink
Return empty list when parsing an empty (or whitespace-only) string
Browse files Browse the repository at this point in the history
  • Loading branch information
timbertson authored and James Halliday committed Apr 17, 2013
1 parent eef9152 commit 1475717
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ exports.quote = function (xs) {
};

exports.parse = function parse (s, env) {
if (!env) env = {};
var chunker = /(['"])((\\\1|[^\1])*?)\1|(\\ |\S)+/g;
return s.match(chunker).map(function (s) {
var match = s.match(chunker);
if (!match) return [];
if (!env) env = {};
return match.map(function (s) {
if (/^'/.test(s)) {
return s
.replace(/^'|'$/g, '')
Expand Down
3 changes: 3 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ test('parse shell commands', function (t) {
t.same(parse('a b\\ c d'), [ 'a', 'b c', 'd' ]);
t.same(parse('\\$beep bo\\`op'), [ '$beep', 'bo`op' ]);
t.same(parse('echo "foo = \\"foo\\""'), [ 'echo', 'foo = "foo"' ]);
t.same(parse(''), []);
t.same(parse(' '), []);
t.same(parse("\t"), []);

t.end();
});
1 change: 1 addition & 0 deletions test/quote.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ test('quote', function (t) {
quote([ '$', '`', '\'' ]),
'\\$ \\` "\'"'
);
t.equal(quote([]), '');
t.end();
});

0 comments on commit 1475717

Please sign in to comment.