8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** unist** ] [ unist ] utility to find a node before another node.
11
+ [ unist] [ ] utility to find a node before another node.
12
12
13
- ## Install
13
+ ## Contents
14
+
15
+ * [ What is this?] ( #what-is-this )
16
+ * [ When should I use this?] ( #when-should-i-use-this )
17
+ * [ Install] ( #install )
18
+ * [ Use] ( #use )
19
+ * [ API] ( #api )
20
+ * [ ` findBefore(parent, node|index[, test]) ` ] ( #findbeforeparent-nodeindex-test )
21
+ * [ Types] ( #types )
22
+ * [ Compatibility] ( #compatibility )
23
+ * [ Related] ( #related )
24
+ * [ Contribute] ( #contribute )
25
+ * [ License] ( #license )
26
+
27
+ ## What is this?
28
+
29
+ This is a tiny utility that you can use to find a node before another node or
30
+ before an index in a parent.
14
31
15
- This package is [ ESM only] ( https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c ) :
16
- Node 12+ is needed to use it and it must be ` import ` ed instead of ` require ` d.
32
+ ## When should I use this?
33
+
34
+ This is super tiny.
35
+ You can of course do it yourself.
36
+ But this helps when integrating with the rest of unified and unist.
37
+
38
+ ## Install
17
39
18
- [ npm] [ ] :
40
+ This package is [ ESM only] [ esm ] .
41
+ In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [ npm] [ ] :
19
42
20
43
``` sh
21
44
npm install unist-util-find-before
22
45
```
23
46
47
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
48
+
49
+ ``` js
50
+ import {findBefore } from " https://esm.sh/unist-util-find-before@3"
51
+ ```
52
+
53
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
54
+
55
+ ``` html
56
+ <script type =" module" >
57
+ import {findBefore } from " https://esm.sh/unist-util-find-before@3?bundle"
58
+ </script >
59
+ ```
60
+
24
61
## Use
25
62
26
63
``` js
@@ -29,76 +66,81 @@ import {findBefore} from 'unist-util-find-before'
29
66
30
67
const tree = u (' tree' , [
31
68
u (' leaf' , ' leaf 1' ),
32
- u (' node ' , [u (' leaf' , ' leaf 2' ), u (' leaf' , ' leaf 3' )]),
69
+ u (' parent ' , [u (' leaf' , ' leaf 2' ), u (' leaf' , ' leaf 3' )]),
33
70
u (' leaf' , ' leaf 4' ),
34
- u (' node ' , [u (' leaf' , ' leaf 5' )]),
71
+ u (' parent ' , [u (' leaf' , ' leaf 5' )]),
35
72
u (' leaf' , ' leaf 6' ),
36
- u (' void ' ),
73
+ u (' empty ' ),
37
74
u (' leaf' , ' leaf 7' )
38
75
])
39
76
40
77
const empty = tree .children [5 ]
41
78
42
- console .log (findBefore (tree, empty, ' node ' ))
79
+ console .log (findBefore (tree, empty, ' parent ' ))
43
80
```
44
81
45
82
Yields:
46
83
47
84
``` js
48
- {type: ' node ' , children: [{type: ' leaf' , value: ' leaf 5' }]}
85
+ {type: ' parent ' , children: [{type: ' leaf' , value: ' leaf 5' }]}
49
86
```
50
87
51
88
## API
52
89
53
- This package exports the following identifiers: ` findBefore ` .
90
+ This package exports the identifier ` findBefore ` .
54
91
There is no default export.
55
92
56
93
### ` findBefore(parent, node|index[, test]) `
57
94
58
- Find the first [ child] [ ] before ` index ` (or ` node ` ) in ` parent ` , that passes
59
- ` test ` .
95
+ Find the first node in ` parent ` ([ ` Parent ` ] [ parent ] ) before another node
96
+ ([ ` Node ` ] [ node ] ) or before an index, that passes ` test ` (` Test ` from
97
+ [ ` unist-util-is ` ] [ test ] ).
60
98
61
- ###### Parameters
99
+ ###### Returns
62
100
63
- * ` parent ` ([ ` Node ` ] [ node ] ) — [ Parent] [ ] node
64
- * ` node ` ([ ` Node ` ] [ node ] ) — [ Child] [ ] of ` parent `
65
- * ` index ` (` number ` , optional) — [ Index] [ ] in ` parent `
66
- * ` test ` (` Function ` , ` string ` , ` Object ` , ` Array ` , optional)
67
- — See [ ` unist-util-is ` ] [ is ]
101
+ Child of ` parent ` that passes ` test ` , if found ([ ` Node? ` ] [ node ] ).
68
102
69
- ###### Returns
103
+ ## Types
104
+
105
+ This package is fully typed with [ TypeScript] [ ] .
106
+ It exports no additional types (types for the test are in ` unist-util-is ` ).
107
+
108
+ ## Compatibility
70
109
71
- [ ` Node? ` ] [ node ] — [ Child] [ ] of ` parent ` passing ` test ` .
110
+ Projects maintained by the unified collective are compatible with all maintained
111
+ versions of Node.js.
112
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
113
+ Our projects sometimes work with older versions, but this is not guaranteed.
72
114
73
115
## Related
74
116
75
117
* [ ` unist-util-find-after ` ] ( https://github.com/syntax-tree/unist-util-find-after )
76
- — Find a node after another node
118
+ — find a node after another node
77
119
* [ ` unist-util-find-all-after ` ] ( https://github.com/syntax-tree/unist-util-find-all-after )
78
- — Find all nodes after another node
120
+ — find all nodes after another node
79
121
* [ ` unist-util-find-all-before ` ] ( https://github.com/syntax-tree/unist-util-find-all-before )
80
- — Find all nodes before another node
122
+ — find all nodes before another node
81
123
* [ ` unist-util-find-all-between ` ] ( https://github.com/mrzmmr/unist-util-find-all-between )
82
- — Find all nodes between two nodes
124
+ — find all nodes between two nodes
83
125
* [ ` unist-util-visit ` ] ( https://github.com/syntax-tree/unist-util-visit )
84
- — Recursively walk over nodes
126
+ — walk the tree
85
127
* [ ` unist-util-visit-parents ` ] ( https://github.com/syntax-tree/unist-util-visit-parents )
86
- — Like ` visit ` , but with a stack of parents
128
+ — walk the tree with a stack of parents
87
129
* [ ` unist-util-filter ` ] ( https://github.com/syntax-tree/unist-util-filter )
88
- — Create a new tree with all nodes that pass a test
130
+ — create a new tree with all nodes that pass a test
89
131
* [ ` unist-util-map ` ] ( https://github.com/syntax-tree/unist-util-map )
90
- — Create a new tree with all nodes mapped by a given function
132
+ — create a new tree with all nodes mapped by a given function
91
133
* [ ` unist-util-flatmap ` ] ( https://gitlab.com/staltz/unist-util-flatmap )
92
- — Create a new tree by mapping (to an array) by a given function
134
+ — create a new tree by mapping (to an array) by a given function
93
135
* [ ` unist-util-remove ` ] ( https://github.com/syntax-tree/unist-util-remove )
94
- — Remove nodes from a tree that pass a test
136
+ — remove nodes from a tree that pass a test
95
137
* [ ` unist-util-select ` ] ( https://github.com/syntax-tree/unist-util-select )
96
- — Select nodes with CSS-like selectors
138
+ — select nodes with CSS-like selectors
97
139
98
140
## Contribute
99
141
100
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
101
- started.
142
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
143
+ ways to get started.
102
144
See [ ` support.md ` ] [ support ] for ways to get help.
103
145
104
146
This project has a [ Code of Conduct] [ coc ] .
@@ -139,24 +181,28 @@ abide by its terms.
139
181
140
182
[ npm ] : https://docs.npmjs.com/cli/install
141
183
184
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
185
+
186
+ [ esmsh ] : https://esm.sh
187
+
188
+ [ typescript ] : https://www.typescriptlang.org
189
+
142
190
[ license ] : license
143
191
144
192
[ author ] : https://wooorm.com
145
193
146
- [ unist ] : https://github.com/syntax-tree/unist
147
-
148
- [ node ] : https://github.com/syntax-tree/unist#node
194
+ [ health ] : https://github.com/syntax-tree/.github
149
195
150
- [ parent ] : https://github.com/syntax-tree/unist#parent-1
196
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing.md
151
197
152
- [ child ] : https://github.com/syntax-tree/unist#child
198
+ [ support ] : https://github.com/syntax-tree/.github/blob/main/support.md
153
199
154
- [ index ] : https://github.com/syntax-tree/unist#index
200
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
155
201
156
- [ is ] : https://github.com/syntax-tree/unist-util-is
202
+ [ unist ] : https://github.com/syntax-tree/unist
157
203
158
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
204
+ [ node ] : https://github.com/syntax-tree/unist#node
159
205
160
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support.md
206
+ [ parent ] : https://github.com/syntax-tree/unist#parent-1
161
207
162
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
208
+ [ test ] : https://github.com/syntax-tree/unist-util-is#test
0 commit comments