diff --git a/src/index.js b/src/index.js index cd51054..4325246 100644 --- a/src/index.js +++ b/src/index.js @@ -1,21 +1,9 @@ -import React from 'react'; - -/** - * Iterates through children that are typically specified as `props.children`, - * but only maps over children that are "valid components". - * - * The mapFunction provided index will be normalised to the components mapped, - * so an invalid component would not increase the index. - * - * @param {?*} children Children tree container. - * @param {function(*, int)} func. - * @param {*} context Context for func. - * @return {object} Object containing the ordered map of results. - */ +import React from "react"; + export function map(children, func, context) { let index = 0; - return React.Children.map(children, child => { + return React.Children.map(children, (child) => { if (!React.isValidElement(child)) { return child; } @@ -24,20 +12,10 @@ export function map(children, func, context) { }); } -/** - * Iterates through children that are "valid components". - * - * The provided forEachFunc(child, index) will be called for each - * leaf child with the index reflecting the position relative to "valid components". - * - * @param {?*} children Children tree container. - * @param {function(*, int)} func. - * @param {*} context Context for context. - */ export function forEach(children, func, context) { let index = 0; - React.Children.forEach(children, child => { + React.Children.forEach(children, (child) => { if (!React.isValidElement(child)) { return; } @@ -46,38 +24,18 @@ export function forEach(children, func, context) { }); } -/** - * Count the number of "valid components" in the Children container. - * - * @param {?*} children Children tree container. - * @returns {number} - */ export function count(children) { let result = 0; - React.Children.forEach(children, child => { - if (!React.isValidElement(child)) { - return; + React.Children.forEach(children, (child) => { + if (React.isValidElement(child)) { + result++; } - - ++result; }); return result; } -/** - * Finds children that are typically specified as `props.children`, - * but only iterates over children that are "valid components". - * - * The provided forEachFunc(child, index) will be called for each - * leaf child with the index reflecting the position relative to "valid components". - * - * @param {?*} children Children tree container. - * @param {function(*, int)} func. - * @param {*} context Context for func. - * @returns {array} of children that meet the func return statement - */ export function filter(children, func, context) { const result = []; @@ -94,10 +52,7 @@ export function find(children, func, context) { let result; forEach(children, (child, index) => { - if (result) { - return; - } - if (func.call(context, child, index)) { + if (!result && func.call(context, child, index)) { result = child; } }); @@ -109,9 +64,6 @@ export function every(children, func, context) { let result = true; forEach(children, (child, index) => { - if (!result) { - return; - } if (!func.call(context, child, index)) { result = false; } @@ -124,10 +76,6 @@ export function some(children, func, context) { let result = false; forEach(children, (child, index) => { - if (result) { - return; - } - if (func.call(context, child, index)) { result = true; } @@ -139,7 +87,7 @@ export function some(children, func, context) { export function toArray(children) { const result = []; - forEach(children, child => { + forEach(children, (child) => { result.push(child); }); diff --git a/tests/index.js b/tests/index.js index b7ee2ca..1ba1e7f 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,32 +1,31 @@ -import React from 'react'; - -import * as ElementChildren from '../src'; +import React from "react"; +import * as ElementChildren from "../src"; const Component = () => ; -describe('ElementChildren', () => { +describe("ElementChildren", () => { let children; beforeEach(() => { children = [ -
, + , false, 0, - ['string', null, ], - [[