Skip to content

Commit

Permalink
[Refactor] hoist some vars to module level
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 2, 2023
1 parent 508e2f9 commit 8f0c5c3
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@
var CONTROL = '(?:' + [
'\\|\\|', '\\&\\&', ';;', '\\|\\&', '\\<\\(', '\\<\\<\\<', '>>', '>\\&', '<\\&', '[&;()|<>]'
].join('|') + ')';
var controlRE = new RegExp('^' + CONTROL + '$');
var META = '|&;()<> \\t';
var BAREWORD = '(\\\\[\'"' + META + ']|[^\\s\'"' + META + '])+';
var SINGLE_QUOTE = '"((\\\\"|[^"])*?)"';
var DOUBLE_QUOTE = '\'((\\\\\'|[^\'])*?)\'';
var hash = /^#$/;

var SQ = "'";
var DQ = '"';
var DS = '$';

var TOKEN = '';
var mult = Math.pow(16, 8);
for (var i = 0; i < 4; i++) {
TOKEN += (Math.pow(16, 8) * Math.random()).toString(16);
TOKEN += (mult * Math.random()).toString(16);
}
var startsWithToken = new RegExp('^' + TOKEN);

function parseInternal(s, env, opts) {
var chunker = new RegExp([
Expand Down Expand Up @@ -52,7 +60,7 @@ function parseInternal(s, env, opts) {
if (commented) {
return void undefined;
}
if (RegExp('^' + CONTROL + '$').test(s)) {
if (controlRE.test(s)) {
return { op: s };
}

Expand All @@ -67,9 +75,6 @@ function parseInternal(s, env, opts) {
// 4. quote context can switch mid-token if there is no whitespace
// between the two quote contexts (e.g. all'one'"token" parses as
// "allonetoken")
var SQ = "'";
var DQ = '"';
var DS = '$';
var BS = opts.escape || '\\';
var quote = false;
var esc = false;
Expand Down Expand Up @@ -137,9 +142,9 @@ function parseInternal(s, env, opts) {
}
} else if (c === DQ || c === SQ) {
quote = c;
} else if (RegExp('^' + CONTROL + '$').test(c)) {
} else if (controlRE.test(c)) {
return { op: s };
} else if ((/^#$/).test(c)) {
} else if (hash.test(c)) {
commented = true;
if (out.length) {
return [out, { comment: s.slice(i + 1) + match.slice(j + 1).join(' ') }];
Expand All @@ -160,10 +165,7 @@ function parseInternal(s, env, opts) {

return out;
}).reduce(function (prev, arg) { // finalize parsed aruments
if (arg === undefined) {
return prev;
}
return prev.concat(arg);
return typeof arg === 'undefined' ? prev : prev.concat(arg);
}, []);
}

Expand All @@ -181,7 +183,7 @@ module.exports = function parse(s, env, opts) {
return acc.concat(xs[0]);
}
return acc.concat(xs.filter(Boolean).map(function (x) {
if (RegExp('^' + TOKEN).test(x)) {
if (startsWithToken.test(x)) {
return JSON.parse(x.split(TOKEN)[1]);
}
return x;
Expand Down

0 comments on commit 8f0c5c3

Please sign in to comment.