Skip to content

Commit 227d474

Browse files
committed
[Refactor] parse: tweak the regex to not match nothing
1 parent 7bcd90e commit 227d474

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

parse.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var DQ = '"';
1616
var DS = '$';
1717

1818
var TOKEN = '';
19-
var mult = Math.pow(16, 8);
19+
var mult = 0x100000000; // Math.pow(16, 8);
2020
for (var i = 0; i < 4; i++) {
2121
TOKEN += (mult * Math.random()).toString(16);
2222
}
@@ -31,11 +31,12 @@ function parseInternal(s, env, opts) {
3131

3232
var chunker = new RegExp([
3333
'(' + CONTROL + ')', // control chars
34-
'(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')*'
34+
'(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')+'
3535
].join('|'), 'g');
36-
var match = s.match(chunker).filter(Boolean);
3736

38-
if (!match) {
37+
var matches = s.match(chunker);
38+
39+
if (!matches) {
3940
return [];
4041
}
4142
if (!env) {
@@ -58,7 +59,7 @@ function parseInternal(s, env, opts) {
5859
return pre + r;
5960
}
6061

61-
return match.map(function (s, j) {
62+
return matches.filter(Boolean).map(function (s, j, match) {
6263
if (commented) {
6364
return void undefined;
6465
}

test/parse.js

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var test = require('tape');
44
var parse = require('../').parse;
55

66
test('parse shell commands', function (t) {
7+
t.same(parse(''), [], 'parses an empty string');
8+
79
t.same(parse('a \'b\' "c"'), ['a', 'b', 'c']);
810
t.same(
911
parse('beep "boop" \'foo bar baz\' "it\'s \\"so\\" groovy"'),

0 commit comments

Comments
 (0)