From fcb2e1acd5312a1a1a4e6c66ec688aab383023b5 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 7 Apr 2023 13:37:11 -0700 Subject: [PATCH] [Refactor] `parse`: use `slice` over `substr`, cache some values --- parse.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/parse.js b/parse.js index d0ce368..967d580 100644 --- a/parse.js +++ b/parse.js @@ -116,28 +116,30 @@ function parseInternal(string, env, opts) { i += 1; var varend; var varname; - // debugger - if (s.charAt(i) === '{') { + var char = s.charAt(i); + + if (char === '{') { i += 1; if (s.charAt(i) === '}') { - throw new Error('Bad substitution: ' + s.substr(i - 2, 3)); + throw new Error('Bad substitution: ' + s.slice(i - 2, i + 1)); } varend = s.indexOf('}', i); if (varend < 0) { - throw new Error('Bad substitution: ' + s.substr(i)); + throw new Error('Bad substitution: ' + s.slice(i)); } - varname = s.substr(i, varend - i); + varname = s.slice(i, varend); i = varend; - } else if ((/[*@#?$!_-]/).test(s.charAt(i))) { - varname = s.charAt(i); + } else if ((/[*@#?$!_-]/).test(char)) { + varname = char; i += 1; } else { - varend = s.substr(i).match(/[^\w\d_]/); + var slicedFromI = s.slice(i); + varend = slicedFromI.match(/[^\w\d_]/); if (!varend) { - varname = s.substr(i); + varname = slicedFromI; i = s.length; } else { - varname = s.substr(i, varend.index); + varname = slicedFromI.slice(0, varend.index); i += varend.index - 1; } }