Skip to content

Commit 4051f32

Browse files
committed
Initial
0 parents  commit 4051f32

File tree

8 files changed

+486
-0
lines changed

8 files changed

+486
-0
lines changed

.babelrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"presets": [
3+
["es2015", { "loose": true }],
4+
"stage-1",
5+
"react"
6+
],
7+
"plugins": [
8+
"add-module-exports"
9+
],
10+
}

.eslintrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
extends: 'airbnb',
3+
env: {
4+
jest: true,
5+
},
6+
rules: {
7+
"react/jsx-filename-extension": 0,
8+
}
9+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
npm-debug.log

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
react-element-children
2+
=====
3+
4+
Utilities for iterating only valid React Elements

lib/index.js

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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+
}

package.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "react-element-children",
3+
"version": "1.0.0",
4+
"description": "Utilities for iterating children elements",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "eslint src && jest",
8+
"tdd": "jest --watch=skip",
9+
"build": "rm -rf lib/* && babel src --out-dir lib",
10+
"release": "release"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git+https://github.com/react-bootstrap/react-child-elements.git"
15+
},
16+
"author": "react-bootstrap contributors",
17+
"license": "MIT",
18+
"bugs": {
19+
"url": "https://github.com/react-bootstrap/react-child-elements/issues"
20+
}
21+
"homepage": "https://github.com/react-bootstrap/react-child-elements#readme",
22+
"jest": {
23+
"automock": false,
24+
"testRegex": "tests/.*\\.js$"
25+
},
26+
"release-script": {
27+
"altPkgRootFolder": "lib"
28+
},
29+
"peerDependencies": {
30+
"react": "^15.0.0"
31+
},
32+
"devDependencies": {
33+
"babel-cli": "^6.11.4",
34+
"babel-jest": "^14.1.0",
35+
"babel-plugin-add-module-exports": "^0.2.1",
36+
"babel-polyfill": "^6.13.0",
37+
"babel-preset-es2015": "^6.13.2",
38+
"babel-preset-react": "^6.11.1",
39+
"babel-preset-stage-1": "^6.13.0",
40+
"eslint": "^3.2.2",
41+
"eslint-config-airbnb": "^10.0.0",
42+
"eslint-plugin-import": "^1.13.0",
43+
"eslint-plugin-jsx-a11y": "^2.1.0",
44+
"eslint-plugin-react": "^6.0.0",
45+
"jest-cli": "^14.1.0",
46+
"react": "^15.3.0",
47+
"release-script": "^1.0.2"
48+
}
49+
}

src/index.js

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

0 commit comments

Comments
 (0)