Skip to content

Commit

Permalink
one test was wrong, checking for pre escapes
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Apr 15, 2013
1 parent 1b21b01 commit 42b5f83
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ exports.parse = function (s, env) {
;
}
else if (/^"/.test(s)) {
return interpolate(s.replace(/^"|"$/g, ''))
return interpolate(s.replace(/^"|"$/g, ''));
}
else return interpolate(s);
})
;

function getVar (_, x) {
return String(env[x])
}

function interpolate (s) {
return s
.replace(/\\([ "'\\$`(){}!#&*|])/g, '$1')
.replace(/\$(\w+)/g, getVar)
.replace(/\${(\w+)}/g, getVar)
.replace(/(^|[^\\])\$(\w+)/g, getVar)
.replace(/(^|[^\\])\${(\w+)}/g, getVar)
;
}
function getVar (_, pre, key) {
return pre + String(env[key]);
}
};
2 changes: 1 addition & 1 deletion test/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var parse = require('../').parse;
test('expand environment variables', function (t) {
t.same(parse('a $XYZ c', { XYZ: 'b' }), [ 'a', 'b', 'c' ]);
t.same(parse('a${XYZ}c', { XYZ: 'b' }), [ 'abc' ]);
t.same(parse('a${XYZ}c $XYZ', { XYZ: 'b' }), [ 'abcb b' ]);
t.same(parse('a${XYZ}c $XYZ', { XYZ: 'b' }), [ 'abc', 'b' ]);
t.same(parse('"-$X-$Y-"', { X: 'a', Y: 'b' }), [ '-a-b-' ]);
t.same(parse("'-$X-$Y-'", { X: 'a', Y: 'b' }), [ '-$X-$Y-' ]);
t.same(parse('qrs"$zzz"wxy', { zzz: 'tuv' }), [ 'qrstuvwxy' ]);
Expand Down

0 comments on commit 42b5f83

Please sign in to comment.