44 * @license MIT
55 * @module unist:find
66 * @fileoverview Unist node finder
7+ *
8+ * @typedef {import('unist').Node } Node
9+ *
10+ * @typedef {string } TestStr
11+ * Finds first node with a truthy property matching string.
12+ * @typedef {Object.<string, unknown> } TestObj
13+ * Finds first node that has matching values for all properties of object.
14+ * @typedef {<V extends Node>(node: V) => boolean } TestFn
15+ * Finds first node for which function returns true when passed node as argument.
716 */
17+
818import { visit } from 'unist-util-visit'
919import iteratee from 'lodash.iteratee'
1020
1121/**
12- * Find
22+ * Unist node finder utility.
1323 *
14- * @param {Node } tree - Root node
15- * @param {string|object|function } [condition] - Condition to match node.
24+ * @param tree
25+ * Node to search.
26+ * @param condition
27+ * Condition used to test each node.
28+ * @returns
29+ * The first node that matches condition, or undefined if no node matches.
30+ * @type {<V extends Node>(tree: Node, condition: TestStr | TestObj | TestFn) => V | undefined }
1631 */
1732function find ( tree , condition ) {
18- if ( ! tree ) throw new Error ( 'unist-find requires a tree to search' )
19- if ( ! condition ) throw new Error ( 'unist-find requires a condition' )
33+ if ( ! tree ) throw new Error ( 'unist-util- find requires a tree to search' )
34+ if ( ! condition ) throw new Error ( 'unist-util- find requires a condition' )
2035
2136 const predicate = iteratee ( condition )
2237 let result
@@ -31,7 +46,4 @@ function find (tree, condition) {
3146 return result
3247}
3348
34- /*
35- * Expose.
36- */
3749export default find
0 commit comments