Skip to content

Commit 39cafd2

Browse files
committed
♻️ removed some functions doing the same thing
1 parent 114bd5a commit 39cafd2

1 file changed

Lines changed: 14 additions & 21 deletions

File tree

index.js

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,31 @@ function viteHTMLIncludes(options = {}) {
66
const { componentsPath = '/components/' } = options;
77
let config;
88

9-
function evaluateCondition(condition, locals) {
9+
function evaluateWithLocals(code, locals) {
1010
try {
11-
return new Function('locals', `return ${condition}`)({ ...locals });
11+
const args = Object.keys(locals);
12+
const values = Object.values(locals);
13+
const func = new Function(...args, `return ${code};`);
14+
return func(...values);
1215
} catch (e) {
13-
console.error(`Error evaluating condition: ${condition}`, e);
14-
return false; // default to false if there's an error
16+
console.error(`Error evaluating code: ${code}`, e);
17+
return false; // For conditions. For expressions, adjust as needed.
1518
}
1619
}
1720

18-
function evaluateExpression(expression, locals) {
19-
try {
20-
return new Function('locals', `with (locals) { return ${expression}; }`)({ ...locals });
21-
} catch (e) {
22-
console.error(`Error evaluating expression: ${expression}`, e);
23-
return undefined; // return undefined or a default value if there's an error
24-
}
25-
}
26-
27-
2821
function processConditionals(fragment, locals) {
2922
fragment.querySelectorAll('if').forEach(node => {
3023
const condition = node.getAttribute('condition');
3124
const elseNode = node.nextElementSibling.tagName === 'ELSE' ? node.nextElementSibling : null;
3225

33-
if (evaluateCondition(condition, locals)) {
26+
if (evaluateWithLocals(condition, locals)) {
3427
node.replaceWith(...node.childNodes);
3528
} else {
3629
node.remove();
3730
}
3831

3932
if (elseNode) {
40-
if (!evaluateCondition(condition, locals)) {
33+
if (!evaluateWithLocals(condition, locals)) {
4134
elseNode.replaceWith(...elseNode.childNodes);
4235
} else {
4336
elseNode.remove();
@@ -49,12 +42,12 @@ function viteHTMLIncludes(options = {}) {
4942
function processSwitchCases(fragment, locals) {
5043
fragment.querySelectorAll('switch').forEach(switchNode => {
5144
const expression = switchNode.getAttribute('expression');
52-
const expressionValue = evaluateExpression(expression, locals);
45+
const expressionValue = evaluateWithLocals(expression, locals);
5346
let hasMatched = false;
5447

5548
switchNode.childNodes.forEach(child => {
5649
if (child.tagName === 'CASE' && !hasMatched) {
57-
const caseValue = evaluateExpression(child.getAttribute('n'), locals);
50+
const caseValue = evaluateWithLocals(child.getAttribute('n'), locals);
5851
if (caseValue === expressionValue) {
5952
hasMatched = true;
6053
child.replaceWith(...child.childNodes);
@@ -73,8 +66,8 @@ function viteHTMLIncludes(options = {}) {
7366
function processEachLoops(fragment, locals) {
7467
fragment.querySelectorAll('each').forEach(eachNode => {
7568
const loop = eachNode.getAttribute('loop');
76-
const [item, index, arrayExpression] = /(\w+),\s*(\w+)\s*in\s*(\w+)/.exec(loop) || [];
77-
const array = evaluateExpression(arrayExpression, locals);
69+
const [match, item, index, arrayExpression] = /(\w+),\s*(\w+)\s*in\s*(\w+)/.exec(loop) || [];
70+
const array = evaluateWithLocals(arrayExpression, locals);
7871
if (!array) return;
7972

8073
const nodesToReplace = [];
@@ -112,7 +105,7 @@ function viteHTMLIncludes(options = {}) {
112105
if (localsString) {
113106
try {
114107
locals = JSON.parse(localsString);
115-
console.log('Locals:', locals); // Add this log to check locals content
108+
console.log(locals);
116109
} catch (e) {
117110
console.error(`Error parsing locals JSON: ${localsString}`, e);
118111
}

0 commit comments

Comments
 (0)