Skip to content

Commit

Permalink
denormalize the interpolate logic to make room for special cases
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Apr 15, 2013
1 parent 37d6058 commit c669d2e
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var re = {
chunk: /(['"])((\\\1|[^\1])*?)\1|(\\ |\S)+/g
};

exports.parse = function (s, env) {
exports.parse = function parse (s, env) {
if (!env) env = {};
return s.match(re.chunk).map(function (s) {
if (/^'/.test(s)) {
Expand All @@ -26,18 +26,20 @@ exports.parse = function (s, env) {
;
}
else if (/^"/.test(s)) {
return interpolate(s.replace(/^"|"$/g, ''));
return s
.replace(/^"|"$/g, '')
.replace(/(^|[^\\])\$(\w+)/g, getVar)
.replace(/(^|[^\\])\${(\w+)}/g, getVar)
.replace(/\\([ "'\\$`(){}!#&*|])/g, '$1')
;
}
else return interpolate(s);
});

function interpolate (s) {
return s
else return s
.replace(/(^|[^\\])\$(\w+)/g, getVar)
.replace(/(^|[^\\])\${(\w+)}/g, getVar)
.replace(/\\([ "'\\$`(){}!#&*|])/g, '$1')
;
}
});

function getVar (_, pre, key) {
return pre + String(env[key] || '');
}
Expand Down

0 comments on commit c669d2e

Please sign in to comment.