Skip to content

Commit

Permalink
externalize the regex declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Apr 15, 2013
1 parent ed0c1c6 commit 37d6058
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ exports.quote = function (xs) {
}).join(' ');
};

var re = {
chunk: /(['"])((\\\1|[^\1])*?)\1|(\\ |\S)+/g
};

exports.parse = function (s, env) {
if (!env) env = {};
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 interpolate(s.replace(/^"|"$/g, ''));
}
else return interpolate(s);
})
;
return s.match(re.chunk).map(function (s) {
if (/^'/.test(s)) {
return s
.replace(/^'|'$/g, '')
.replace(/\\(["'\\$`(){}!#&*|])/g, '$1')
;
}
else if (/^"/.test(s)) {
return interpolate(s.replace(/^"|"$/g, ''));
}
else return interpolate(s);
});

function interpolate (s) {
return s
Expand Down

0 comments on commit 37d6058

Please sign in to comment.