@@ -54,50 +54,52 @@ var hasValidChildren = function hasValidChildren(children) {
54
54
if ( ! children . every ( function ( child ) {
55
55
return child . type === If || child . type === Else || child . type === ElseIf ;
56
56
} ) ) {
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'." ) ;
59
58
}
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 ) {
73
72
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
+ }
74
79
}
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" ) ;
79
82
}
80
83
} else {
81
84
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" ) ;
84
86
}
85
87
}
86
88
} ;
87
89
88
90
var evaluateChildren = function evaluateChildren ( children ) {
89
91
if ( Array . isArray ( children ) && children . length > 0 ) {
90
- var trutyChild = children . find ( function ( child ) {
92
+ var truthyChild = children . find ( function ( child ) {
91
93
return evaluateConditionProp ( child . props . condition ) ;
92
94
} ) ;
93
- if ( trutyChild ) {
94
- return trutyChild ;
95
+ if ( truthyChild ) {
96
+ return truthyChild ;
95
97
} else {
96
98
var lastChild = children [ children . length - 1 ] ;
97
99
return lastChild . type === Else ? lastChild : null ;
98
100
}
99
101
} 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 ;
101
103
}
102
104
} ;
103
105
0 commit comments