Skip to content

Commit

Permalink
passing the parse test
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed May 18, 2012
1 parent 980aa58 commit 69c0f85
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,21 @@ exports.quote = function (xs) {
};

exports.parse = function (s) {
return s.match(/(['"])((\\\1|[^\1])*?)\1|\S+/g);
return s.match(/(['"])((\\\1|[^\1])*?)\1|\S+/g)
.map(function (s) {
if (/^'/.test(s)) {
return s
.replace(/^'|'$/g, '')
.replace(/\\(['\\])/g, '$1')
;
}
else if (/^"/.test(s)) {
return s
.replace(/^"|"$/g, '')
.replace(/\\(["\\])/g, '$1')
;
}
else return s;
})
;
};
2 changes: 1 addition & 1 deletion test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var test = require('tap').test;
var parse = require('../').parse;

test('parse shell commands', function (t) {
t.same('a \'b\' "c"', [ 'a', 'b', 'c' ]);
t.same(parse('a \'b\' "c"'), [ 'a', 'b', 'c' ]);

t.same(
parse('beep "boop" \'foo bar baz\' "it\'s \\"so\\" groovy"'),
Expand Down

0 comments on commit 69c0f85

Please sign in to comment.