Skip to content

Commit

Permalink
perf: improve regexp on reearth/core (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiya01 committed Mar 13, 2023
1 parent 39bfb06 commit ca7b052
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/core/mantle/evaluator/simple/expression/variableReplacer.ts
Expand Up @@ -110,13 +110,13 @@ const RESERVED_WORDS: Record<string, string> = {

const replaceReservedWord = (word: string) => {
const wordFiltered = word.replace(/-/g, RESERVED_WORDS["-"]);
return wordFiltered.replaceAll(
/(.*)(\[|\{|\()(.*)(\]|\}|\))(?!\.|\[)(.+)/g,
(_match, prefix, openedBracket, inner, closedBracket, suffix) => {
return `${prefix}${RESERVED_WORDS[openedBracket]}${inner}${RESERVED_WORDS[closedBracket]}${suffix}`;
},
);
if (!/(\]|\)|\})[^[.]+$/.test(wordFiltered)) {
return wordFiltered;
}
return Object.entries(RESERVED_WORDS).reduce((res, [key, val]) => {
return res.replaceAll(key, val);
}, wordFiltered);
};

export const restoreReservedWord = (text: string) =>
Object.entries(RESERVED_WORDS).reduce((res, [key, val]) => res.replace(val, key), text);
Object.entries(RESERVED_WORDS).reduce((res, [key, val]) => res.replaceAll(val, key), text);

0 comments on commit ca7b052

Please sign in to comment.