Skip to content

Commit 053f322

Browse files
committed
fix: enhance conditional processing with else-if support and improve number parsing
1 parent 392ec6f commit 053f322

File tree

1 file changed

+17
-33
lines changed

1 file changed

+17
-33
lines changed

src/index.js

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ const CoCreateEvents = {
512512
}
513513

514514
let ifCondition = element.getAttribute(`${prefix}-if`);
515+
let elseIfCondition = element.getAttribute(`${prefix}-else-if`);
515516
let elseCondition = element.getAttribute(`${prefix}-else`);
516517

517518
let ifValue = element.getAttribute(`${prefix}-if-value`);
@@ -530,38 +531,16 @@ const CoCreateEvents = {
530531
}
531532
}
532533

533-
//TODO: improved resize toggling of values
534-
// let hasCondition = this.elements2.get(element)
535534
if (ifCondition) {
536-
if (evaluateCondition(ifCondition, ifValue)) {
537-
// Action 1: Condition exists and evaluates true
538-
// console.log("Executing Action 1 for ifCondition:", ifCondition); // Optional debug log
539-
// Replace this comment with your actual code for Path 1
540-
// e.g., this.elements2.set(element, { condition: ifCondition });
541-
542-
// Assuming we stop processing for this element once a condition is met and actioned
543-
return;
544-
} else {
545-
// Condition existed but evaluated false. Stop processing.
546-
// Corresponds to the original `else if (ifCondition || elseCondition) { return; }` for the ifCondition case
535+
// If the 'if' condition is not met, stop processing this element.
536+
if (!evaluateCondition(ifCondition, ifValue)) {
547537
return;
548538
}
549-
}
550-
551-
// If we reach here, ifCondition was falsy (didn't exist)
552-
// Now check the 'else' condition
553-
if (elseCondition) {
554-
if (evaluateCondition(elseCondition, ifValue)) {
555-
// Action 2: Condition exists and evaluates true
556-
// console.log("Executing Action 2 for elseCondition:", elseCondition); // Optional debug log
557-
// Replace this comment with your actual code for Path 2
558-
// e.g., this.elements2.set(element, { condition: elseCondition });
559-
560-
// Assuming we stop processing for this element once a condition is met and actioned
561-
return;
562-
} else {
563-
// Condition existed but evaluated false. Stop processing.
564-
// Corresponds to the original `else if (ifCondition || elseCondition) { return; }` for the elseCondition case
539+
} else if (elseCondition) {
540+
// If there's no 'if' condition but there is an 'else' condition,
541+
// stop processing if the 'else' condition is met.
542+
// This creates an "execute unless" behavior for the 'else' attribute.
543+
if (evaluateCondition(elseIfCondition, ifValue)) {
565544
return;
566545
}
567546
}
@@ -940,7 +919,10 @@ function checkCondition(condition, value) {
940919
// TODO: why parse updated conditin to boolean false
941920
// if (parse && condition !== "false") condition = parseCondition(condition);
942921

943-
if (typeof value[0] === "number") {
922+
if (
923+
typeof condition === "number" ||
924+
(typeof condition === "string" && /\d/.test(condition))
925+
) {
944926
condition = parseNumberCondition(condition);
945927
} else if (typeof value[0] === "object" && typeof condition === "string") {
946928
condition = parseJsonCondition(condition);
@@ -996,9 +978,11 @@ function parseJsonCondition(condition) {
996978
}
997979
}
998980
function parseNumberCondition(condition) {
999-
return !isNaN(condition) && condition.trim() !== ""
1000-
? Number(condition)
1001-
: condition;
981+
if (typeof condition === "string") {
982+
const num = parseFloat(condition);
983+
return !isNaN(num) ? num : condition;
984+
}
985+
return condition;
1002986
}
1003987

1004988
// Helper function for delay using Promises

0 commit comments

Comments
 (0)