1
+ 'use strict' ;
2
+
3
+ exports . __esModule = true ;
4
+ exports . map = map ;
5
+ exports . forEach = forEach ;
6
+ exports . count = count ;
7
+ exports . filter = filter ;
8
+ exports . find = find ;
9
+ exports . every = every ;
10
+ exports . some = some ;
11
+ exports . toArray = toArray ;
12
+
13
+ var _react = require ( 'react' ) ;
14
+
15
+ var _react2 = _interopRequireDefault ( _react ) ;
16
+
17
+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
18
+
19
+ /**
20
+ * Iterates through children that are typically specified as `props.children`,
21
+ * but only maps over children that are "valid components".
22
+ *
23
+ * The mapFunction provided index will be normalised to the components mapped,
24
+ * so an invalid component would not increase the index.
25
+ *
26
+ * @param {?* } children Children tree container.
27
+ * @param {function(*, int) } func.
28
+ * @param {* } context Context for func.
29
+ * @return {object } Object containing the ordered map of results.
30
+ */
31
+ function map ( children , func , context ) {
32
+ var index = 0 ;
33
+
34
+ return _react2 . default . Children . map ( children , function ( child ) {
35
+ if ( ! _react2 . default . isValidElement ( child ) ) {
36
+ return child ;
37
+ }
38
+
39
+ return func . call ( context , child , index ++ ) ;
40
+ } ) ;
41
+ }
42
+
43
+ /**
44
+ * Iterates through children that are "valid components".
45
+ *
46
+ * The provided forEachFunc(child, index) will be called for each
47
+ * leaf child with the index reflecting the position relative to "valid components".
48
+ *
49
+ * @param {?* } children Children tree container.
50
+ * @param {function(*, int) } func.
51
+ * @param {* } context Context for context.
52
+ */
53
+ function forEach ( children , func , context ) {
54
+ var index = 0 ;
55
+
56
+ _react2 . default . Children . forEach ( children , function ( child ) {
57
+ if ( ! _react2 . default . isValidElement ( child ) ) {
58
+ return ;
59
+ }
60
+
61
+ func . call ( context , child , index ++ ) ;
62
+ } ) ;
63
+ }
64
+
65
+ /**
66
+ * Count the number of "valid components" in the Children container.
67
+ *
68
+ * @param {?* } children Children tree container.
69
+ * @returns {number }
70
+ */
71
+ function count ( children ) {
72
+ var result = 0 ;
73
+
74
+ _react2 . default . Children . forEach ( children , function ( child ) {
75
+ if ( ! _react2 . default . isValidElement ( child ) ) {
76
+ return ;
77
+ }
78
+
79
+ ++ result ;
80
+ } ) ;
81
+
82
+ return result ;
83
+ }
84
+
85
+ /**
86
+ * Finds children that are typically specified as `props.children`,
87
+ * but only iterates over children that are "valid components".
88
+ *
89
+ * The provided forEachFunc(child, index) will be called for each
90
+ * leaf child with the index reflecting the position relative to "valid components".
91
+ *
92
+ * @param {?* } children Children tree container.
93
+ * @param {function(*, int) } func.
94
+ * @param {* } context Context for func.
95
+ * @returns {array } of children that meet the func return statement
96
+ */
97
+ function filter ( children , func , context ) {
98
+ var result = [ ] ;
99
+
100
+ forEach ( children , function ( child , index ) {
101
+ if ( func . call ( context , child , index ) ) {
102
+ result . push ( child ) ;
103
+ }
104
+ } ) ;
105
+
106
+ return result ;
107
+ }
108
+
109
+ function find ( children , func , context ) {
110
+ var result = void 0 ;
111
+
112
+ forEach ( children , function ( child , index ) {
113
+ if ( result ) {
114
+ return ;
115
+ }
116
+ if ( func . call ( context , child , index ) ) {
117
+ result = child ;
118
+ }
119
+ } ) ;
120
+
121
+ return result ;
122
+ }
123
+
124
+ function every ( children , func , context ) {
125
+ var result = true ;
126
+
127
+ forEach ( children , function ( child , index ) {
128
+ if ( ! result ) {
129
+ return ;
130
+ }
131
+ if ( ! func . call ( context , child , index ) ) {
132
+ result = false ;
133
+ }
134
+ } ) ;
135
+
136
+ return result ;
137
+ }
138
+
139
+ function some ( children , func , context ) {
140
+ var result = false ;
141
+
142
+ forEach ( children , function ( child , index ) {
143
+ if ( result ) {
144
+ return ;
145
+ }
146
+
147
+ if ( func . call ( context , child , index ) ) {
148
+ result = true ;
149
+ }
150
+ } ) ;
151
+
152
+ return result ;
153
+ }
154
+
155
+ function toArray ( children ) {
156
+ var result = [ ] ;
157
+
158
+ forEach ( children , function ( child ) {
159
+ result . push ( child ) ;
160
+ } ) ;
161
+
162
+ return result ;
163
+ }
0 commit comments