Skip to content

Commit

Permalink
chore: improve variable evaluation performances
Browse files Browse the repository at this point in the history
  • Loading branch information
brunosabot committed Sep 28, 2024
1 parent 34d8997 commit 1017c09
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
54 changes: 40 additions & 14 deletions src/evaluateVariables-helper.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,57 @@
import formatVariables from "./formatVariables-helper";

const primitiveRegexMap = new Map();
const objectQuotesRegexMap = new Map();
const objectRegexMap = new Map();
const basicRegexMap = new Map();
const escapeQuoteRegex = /"/gmu;

export const replaceWithKeyValue = (stringTemplate, key, value) => {
if (typeof value === "number" || typeof value === "boolean") {
return stringTemplate
.replaceAll(`'[[${key}]]'`, value)
.replaceAll(`"[[${key}]]"`, value)
.replaceAll(`\`[[${key}]]\``, value);
let rxp = primitiveRegexMap.get(key);
if (rxp === undefined) {
rxp = new RegExp(`["'\`]\\[\\[${key}\\]\\]["'\`]`, "gmu");
primitiveRegexMap.set(key, rxp);
}

return stringTemplate.replace(rxp, value);
} else if (typeof value === "object") {
const valueString = JSON.stringify(value);

let rxpQuotes = objectQuotesRegexMap.get(key);
if (rxpQuotes === undefined) {
rxpQuotes = new RegExp(`"\\[\\[${key}\\]\\]"`, "gmu");
objectQuotesRegexMap.set(key, rxpQuotes);
}

let rxp = objectRegexMap.get(key);
if (rxp === undefined) {
rxp = new RegExp(`['\`]\\[\\[${key}\\]\\]['\`]`, "gmu");
objectRegexMap.set(key, rxp);
}

return stringTemplate
.replaceAll(`"[[${key}]]"`, JSON.stringify(value))
.replaceAll(`'[[${key}]]'`, JSON.stringify(value).replaceAll('"', '\\"'))
.replaceAll(
`\`[[${key}]]\``,
JSON.stringify(value).replaceAll('"', '\\"'),
);
.replace(rxpQuotes, valueString)
.replace(rxp, valueString.replace(escapeQuoteRegex, '\\"'));
}

let rxp = basicRegexMap.get(key);
if (rxp === undefined) {
rxp = new RegExp(`\\[\\[${key}\\]\\]`, "gmu");
basicRegexMap.set(key, rxp);
}
return stringTemplate.replaceAll(`[[${key}]]`, value);

return stringTemplate.replace(rxp, value);
};

export default function evaluateVariables(templateConfig, variables) {
if (!variables && !templateConfig.default) {
return templateConfig.card;
}

let stringTemplate = JSON.stringify(
templateConfig.card ?? templateConfig.element,
);
let stringTemplate = templateConfig.card
? JSON.stringify(templateConfig.card)
: JSON.stringify(templateConfig.element);

const variablesObject = {
...formatVariables(templateConfig.default ?? {}),
Expand Down
4 changes: 1 addition & 3 deletions src/formatVariables-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export default function formatVariables(variables) {
});
});
} else {
Object.entries(variables).forEach(([key, value]) => {
formattedVariables[key] = value;
});
return variables;
}

return formattedVariables;
Expand Down

0 comments on commit 1017c09

Please sign in to comment.