4
4
* @license MIT
5
5
* @module unist:find
6
6
* @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.
7
16
*/
17
+
8
18
import { visit } from 'unist-util-visit'
9
19
import iteratee from 'lodash.iteratee'
10
20
11
21
/**
12
- * Find
22
+ * Unist node finder utility.
13
23
*
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 }
16
31
*/
17
32
function 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' )
20
35
21
36
const predicate = iteratee ( condition )
22
37
let result
@@ -31,7 +46,4 @@ function find (tree, condition) {
31
46
return result
32
47
}
33
48
34
- /*
35
- * Expose.
36
- */
37
49
export default find
0 commit comments