Skip to content

Commit 29bac8b

Browse files
author
Rik
authored
Merge pull request #8 from paescuj/types
Add types via JSDoc
2 parents 6998870 + d042942 commit 29bac8b

File tree

5 files changed

+2244
-1235
lines changed

5 files changed

+2244
-1235
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ jspm_packages
4040
# Optional REPL history
4141
.node_repl_history
4242

43+
# Type Declarations
44+
index.d.ts
45+
4346

4447
### OSX ###
4548
*.DS_Store

index.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,34 @@
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+
818
import { visit } from 'unist-util-visit'
919
import 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
*/
1732
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')
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-
*/
3749
export default find

0 commit comments

Comments
 (0)