Skip to content

Commit

Permalink
half the env tests are working with basic interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Apr 15, 2013
1 parent 601b340 commit 5891471
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,34 @@ exports.quote = function (xs) {
}).join(' ');
};

exports.parse = function (s) {
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');
.replace(/\\(["'\\$`(){}!#&*|])/g, '$1')
;
}
else if (/^"/.test(s)) {
return s
.replace(/^"|"$/g, '')
.replace(/\\(["'\\$`(){}!#&*|])/g, '$1');
.replace(/\\(["'\\$`(){}!#&*|])/g, '$1')
.replace(/\$(\w+)/g, getVar)
.replace(/\${(\w+)}/g, getVar)
;
}
else {
return s
.replace(/\\([ "'\\$`(){}!#&*|])/g, '$1')
.replace(/\$(\w+)/g, getVar)
.replace(/\${(\w+)}/g, getVar)
;
}
else return s.replace(/\\([ "'\\$`(){}!#&*|])/g, '$1');
})
;
function getVar (_, x) {
return String(env[x])
}
};

0 comments on commit 5891471

Please sign in to comment.