react-itertools
Advanced tools
Comparing version 0.0.5 to 0.0.6
import { Children, isValidElement } from "react"; | ||
export function each(children, fn, options = {}) { | ||
var _a; | ||
const breadthFirst = options["visit"] === "breadthFirst"; | ||
const maxDepth = options["maxDepth"] ?? -1; | ||
const maxDepth = (_a = options["maxDepth"]) !== null && _a !== void 0 ? _a : -1; | ||
const queue = []; | ||
@@ -22,5 +23,3 @@ Children.forEach(children, (el) => queue.push([el, depth])); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
if (isValidElement(element) && element.props.children) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
Children.forEach(element.props.children, (el) => scheduler(el, depth + 1)); | ||
@@ -27,0 +26,0 @@ } |
import { ReactNode } from "react"; | ||
import { Options } from "../options"; | ||
declare type MapReturn<T, C> = C extends null | undefined ? C : Array<Exclude<T, boolean | null | undefined>>; | ||
/** | ||
* Recursively iterates through all `children` and returns the transformed result of applying `fn` to each child. | ||
* | ||
* Recurses depth first, post-order. | ||
* | ||
* @param children - the React children. | ||
* @param fn - the function that will be applied to every child element. | ||
* @param options - {@link Options} | ||
* | ||
*/ | ||
export declare function map<T, C extends ReactNode>(children: C | C[], fn: (element: ReactNode) => T, options?: Omit<Options, "visit">): MapReturn<T, C>; | ||
export {}; |
import { Children, cloneElement, isValidElement } from "react"; | ||
function _map(children, fn, maxDepth, depth) { | ||
return Children.map(children, (child) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
if (isValidElement(child) && child.props.children && depth !== maxDepth) { | ||
child = cloneElement(child, { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment | ||
children: _map(child.props.children, fn, maxDepth, depth + 1), | ||
@@ -14,15 +12,6 @@ }); | ||
} | ||
/** | ||
* Recursively iterates through all `children` and returns the transformed result of applying `fn` to each child. | ||
* | ||
* Recurses depth first, post-order. | ||
* | ||
* @param children - the React children. | ||
* @param fn - the function that will be applied to every child element. | ||
* @param options - {@link Options} | ||
* | ||
*/ | ||
export function map(children, fn, options = {}) { | ||
const maxDepth = options["maxDepth"] ?? -1; | ||
var _a; | ||
const maxDepth = (_a = options["maxDepth"]) !== null && _a !== void 0 ? _a : -1; | ||
return _map(children, fn, maxDepth, 0); | ||
} |
@@ -1,18 +0,4 @@ | ||
/** | ||
* Configuration options for traversal. | ||
*/ | ||
export declare type Options = Partial<{ | ||
/** | ||
* The maximum depth to descend to. If unspecified, every level of the tree will be visited. | ||
* | ||
* Zero-based numbering. | ||
*/ | ||
maxDepth: number; | ||
/** | ||
* The traversal startegy. Defaults to breadth first. | ||
* | ||
* @remarks | ||
* This option is not available for {@link map} or {@link filter}, which always use depth first. | ||
*/ | ||
visit: "breadthFirst" | "depthFirst"; | ||
}>; |
{ | ||
"name": "react-itertools", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "A suite of tools for manipulating React children", | ||
@@ -57,10 +57,10 @@ "license": "MIT", | ||
"keywords": [ | ||
"react", | ||
"all children", | ||
"children", | ||
"recursive", | ||
"iterate", | ||
"map", | ||
"map recursive", | ||
"all children", | ||
"iterate", | ||
"reactjs" | ||
"react", | ||
"reactjs", | ||
"recursive" | ||
], | ||
@@ -67,0 +67,0 @@ "husky": { |
@@ -96,4 +96,6 @@ # React Itertools | ||
🌲 Tree shakeable | ||
🌲 [Tree shakeable](https://webpack.js.org/guides/tree-shaking/) | ||
🪐 Isomorphic / Universal -- safe to run in any JS context: the browser or on a server | ||
## Contributing 👫 | ||
@@ -106,5 +108,1 @@ | ||
See the project's [MIT License](https://github.com/tatethurston/react-itertools/blob/master/LICENSE). | ||
``` | ||
``` |
15752
211
107