Skip to content

Commit

Permalink
[Refactor] parse: hoist getVar to module level
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Apr 7, 2023
1 parent fcb2e1a commit b42ac73
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ function matchAll(s, r) {
return matches;
}

function getVar(env, pre, key) {
var r = typeof env === 'function' ? env(key) : env[key];
if (typeof r === 'undefined' && key != '') {
r = '';
} else if (typeof r === 'undefined') {
r = '$';
}

if (typeof r === 'object') {
return pre + TOKEN + JSON.stringify(r) + TOKEN;
}
return pre + r;
}

function parseInternal(string, env, opts) {
if (!opts) {
opts = {};
Expand All @@ -72,20 +86,6 @@ function parseInternal(string, env, opts) {

var commented = false;

function getVar(_, pre, key) {
var r = typeof env === 'function' ? env(key) : env[key];
if (r === undefined && key != '') {
r = '';
} else if (r === undefined) {
r = '$';
}

if (typeof r === 'object') {
return pre + TOKEN + JSON.stringify(r) + TOKEN;
}
return pre + r;
}

return matches.map(function (match) {
var s = match[0];
if (!s || commented) {
Expand Down Expand Up @@ -143,7 +143,7 @@ function parseInternal(string, env, opts) {
i += varend.index - 1;
}
}
return getVar(null, '', varname);
return getVar(env, '', varname);
}

for (i = 0; i < s.length; i++) {
Expand Down

0 comments on commit b42ac73

Please sign in to comment.