Skip to content

Commit efe5ef6

Browse files
committed
fix(Decide Component): Fixed child not defined bug, And Removed unrechable statements
- Removed child not defined bug, when single child is passed to Decide. - Erroreanous statements now throws error instead of just console logging errors. - Removed possibly unrechable conditions.
1 parent eae0302 commit efe5ef6

File tree

2 files changed

+51
-49
lines changed

2 files changed

+51
-49
lines changed

dist/index.js

+27-25
Original file line numberDiff line numberDiff line change
@@ -54,50 +54,52 @@ var hasValidChildren = function hasValidChildren(children) {
5454
if (!children.every(function (child) {
5555
return child.type === If || child.type === Else || child.type === ElseIf;
5656
})) {
57-
console.error("Invalid children found, Child for 'Decide' should be one of 'If', 'Else', 'ElseIf'.");
58-
return false;
57+
throw new Error("Invalid children found, Child for 'Decide' should be one of 'If', 'Else', 'ElseIf'.");
5958
}
60-
var elseChildren = children.filter(function (child) {
61-
return child.type === Else;
62-
});
63-
if (elseChildren.length > 1) {
64-
console.error("Else should be used only once.");
65-
return false;
66-
} else if (elseChildren.length === 0) {
67-
return true;
68-
} else {
69-
if (children[children.length - 1].type !== Else) {
70-
console.error("Else should be used at last.");
71-
return false;
72-
} else {
59+
if (children[0].type === If) {
60+
var elseChildren = children.filter(function (child) {
61+
return child.type === Else;
62+
});
63+
var ifChildren = children.filter(function (child) {
64+
return child.type === If;
65+
});
66+
67+
if (ifChildren.length > 1) throw new Error("'If' should be used only once.");
68+
69+
if (elseChildren.length > 1) {
70+
throw new Error("Else should be used only once.");
71+
} else if (elseChildren.length === 0) {
7372
return true;
73+
} else {
74+
if (children[children.length - 1].type !== Else) {
75+
throw new Error("Else should be used at last.");
76+
} else {
77+
return true;
78+
}
7479
}
75-
}
76-
if (children[0].type === If) return true;else {
77-
console.error("First Children must be an 'If' component");
78-
return false;
80+
} else {
81+
throw new Error("First Children must be an 'If' component");
7982
}
8083
} else {
8184
if (children.type === If) return true;else {
82-
console.error("First Children must be an 'If' component");
83-
return false;
85+
throw new Error("First Children must be an 'If' component");
8486
}
8587
}
8688
};
8789

8890
var evaluateChildren = function evaluateChildren(children) {
8991
if (Array.isArray(children) && children.length > 0) {
90-
var trutyChild = children.find(function (child) {
92+
var truthyChild = children.find(function (child) {
9193
return evaluateConditionProp(child.props.condition);
9294
});
93-
if (trutyChild) {
94-
return trutyChild;
95+
if (truthyChild) {
96+
return truthyChild;
9597
} else {
9698
var lastChild = children[children.length - 1];
9799
return lastChild.type === Else ? lastChild : null;
98100
}
99101
} else {
100-
return typeof children.props.condition !== "undefined" && evaluateConditionProp(child.props.condition) ? children : null;
102+
return typeof children.props.condition !== "undefined" && evaluateConditionProp(children.props.condition) ? children : null;
101103
}
102104
};
103105

src/Decide/Decide.jsx

+24-24
Original file line numberDiff line numberDiff line change
@@ -26,53 +26,53 @@ const hasValidChildren = children => {
2626
child.type === If || child.type === Else || child.type === ElseIf
2727
)
2828
) {
29-
console.error(
29+
throw new Error(
3030
"Invalid children found, Child for 'Decide' should be one of 'If', 'Else', 'ElseIf'."
3131
);
32-
return false;
3332
}
34-
let elseChildren = children.filter(child => child.type === Else);
35-
if (elseChildren.length > 1) {
36-
console.error("Else should be used only once.");
37-
return false;
38-
} else if (elseChildren.length === 0) {
39-
return true;
40-
} else {
41-
if (children[children.length - 1].type !== Else) {
42-
console.error("Else should be used at last.");
43-
return false;
44-
} else {
33+
if (children[0].type === If) {
34+
let elseChildren = children.filter(child => child.type === Else);
35+
let ifChildren = children.filter(child => child.type === If);
36+
37+
if (ifChildren.length > 1)
38+
throw new Error("'If' should be used only once.");
39+
40+
if (elseChildren.length > 1) {
41+
throw new Error("Else should be used only once.");
42+
} else if (elseChildren.length === 0) {
4543
return true;
44+
} else {
45+
if (children[children.length - 1].type !== Else) {
46+
throw new Error("Else should be used at last.");
47+
} else {
48+
return true;
49+
}
4650
}
47-
}
48-
if (children[0].type === If) return true;
49-
else {
50-
console.error("First Children must be an 'If' component");
51-
return false;
51+
} else {
52+
throw new Error("First Children must be an 'If' component");
5253
}
5354
} else {
5455
if (children.type === If) return true;
5556
else {
56-
console.error("First Children must be an 'If' component");
57-
return false;
57+
throw new Error("First Children must be an 'If' component");
5858
}
5959
}
6060
};
6161

6262
const evaluateChildren = children => {
6363
if (Array.isArray(children) && children.length > 0) {
64-
let trutyChild = children.find(child =>
64+
let truthyChild = children.find(child =>
6565
evaluateConditionProp(child.props.condition)
6666
);
67-
if (trutyChild) {
68-
return trutyChild;
67+
if (truthyChild) {
68+
return truthyChild;
6969
} else {
7070
let lastChild = children[children.length - 1];
7171
return lastChild.type === Else ? lastChild : null;
7272
}
7373
} else {
7474
return typeof children.props.condition !== "undefined" &&
75-
evaluateConditionProp(child.props.condition)
75+
evaluateConditionProp(children.props.condition)
7676
? children
7777
: null;
7878
}

0 commit comments

Comments
 (0)