8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ nlcst] [ ] utility to search for patterns in a tree.
11
+ [ nlcst] [ ] utility to search for phrases in a tree.
12
12
13
13
## Contents
14
14
17
17
* [ Install] ( #install )
18
18
* [ Use] ( #use )
19
19
* [ API] ( #api )
20
- * [ ` search(tree, patterns, handler[, allowApostrophes|options]) ` ] ( #searchtree-patterns-handler-allowapostrophesoptions )
21
- * [ ` function handler(nodes, index, parent, pattern) ` ] ( #function-handlernodes-index-parent-pattern )
20
+ * [ ` search(tree, phrases, handler[, allowApostrophes|options]) ` ] ( #searchtree-phrases-handler-allowapostrophesoptions )
21
+ * [ ` Handler ` ] ( #handler )
22
+ * [ ` Options ` ] ( #options )
23
+ * [ ` PhrasesList ` ] ( #phraseslist )
24
+ * [ ` PhrasesMap ` ] ( #phrasesmap )
22
25
* [ Types] ( #types )
23
26
* [ Compatibility] ( #compatibility )
24
27
* [ Related] ( #related )
27
30
28
31
## What is this?
29
32
30
- This utility can search for patterns (words and phrases) in trees.
33
+ This utility can search for phrases (words and phrases) in trees.
31
34
32
35
## When should I use this?
33
36
@@ -37,7 +40,7 @@ and phrases.
37
40
## Install
38
41
39
42
This package is [ ESM only] [ esm ] .
40
- In Node.js (version 12.20+, 14.14+, 16.0+, 18 .0+), install with [ npm] [ ] :
43
+ In Node.js (version 14.14+ and 16 .0+), install with [ npm] [ ] :
41
44
42
45
``` sh
43
46
npm install nlcst-search
@@ -46,14 +49,14 @@ npm install nlcst-search
46
49
In Deno with [ ` esm.sh ` ] [ esmsh ] :
47
50
48
51
``` js
49
- import {search } from " https://esm.sh/nlcst-search@3"
52
+ import {search } from ' https://esm.sh/nlcst-search@3'
50
53
```
51
54
52
55
In browsers with [ ` esm.sh ` ] [ esmsh ] :
53
56
54
57
``` html
55
58
<script type =" module" >
56
- import {search } from " https://esm.sh/nlcst-search@3?bundle"
59
+ import {search } from ' https://esm.sh/nlcst-search@3?bundle'
57
60
</script >
58
61
```
59
62
@@ -104,82 +107,104 @@ search(tree, ['do blocklevel'], function(nodes) {
104
107
105
108
## API
106
109
107
- This package exports the identifier ` search ` .
110
+ This package exports the identifier [ ` search ` ] [ search ] .
108
111
There is no default export.
109
112
110
- ### ` search(tree, patterns , handler[, allowApostrophes|options]) `
113
+ ### ` search(tree, phrases , handler[, allowApostrophes|options]) `
111
114
112
- Search for patterns a [ tree] [ ] .
115
+ Search for phrases in a tree.
113
116
114
117
##### Parameters
115
118
116
- ###### ` node `
119
+ * ` tree ` ([ ` Node ` ] [ node ] )
120
+ — tree to search
121
+ * ` phrases ` ([ ` PhrasesList ` ] [ phraseslist ] or [ ` PhrasesMap ` ] [ phrasesmap ] )
122
+ — phrases to search for
123
+ * ` handler ` ([ ` Handler ` ] [ handler ] )
124
+ — handle a match
125
+ * ` allowApostrophes ` (` boolean ` )
126
+ — shortcut for ` options ` of ` {allowApostrophes: boolean} `
127
+ * ` options ` ([ ` Options ` ] [ options ] )
128
+ — configuration
117
129
118
- [ Tree ] [ ] to search in ( [ ` Node ` ] [ node ] ).
130
+ ###### Returns
119
131
120
- ###### ` patterns `
132
+ Nothing ( ` void ` ).
121
133
122
- Patterns to search for (` Array<string> ` or ` Record<string, unknown> ` ).
123
- If an ` Object ` , uses its keys as patterns.
124
- Each pattern is a space-separated list of words, where each word is
125
- [ normalized] [ nlcst-normalize ] to remove casing, apostrophes, and dashes.
126
- Spaces in a pattern mean zero or more white space nodes in the tree.
127
- Instead of a word, it’s also possible to use a wildcard symbol (` * ` , an
128
- asterisk), that matches any word in a pattern (` alpha * charlie ` ).
134
+ ### ` Handler `
129
135
130
- ###### ` handler `
136
+ Handle a match (TypeScript type).
131
137
132
- Handler called when a match is found ( [ ` Handler ` ] [ fn-handler ] ).
138
+ ###### Parameters
133
139
134
- ###### ` allowApostrophes `
140
+ * ` nodes ` ([ ` Array<Node> ` ] [ node ] )
141
+ — match
142
+ * ` index ` (` number ` )
143
+ — index of first node of ` nodes ` in ` parent `
144
+ * ` parent ` ([ ` Node ` ] [ node ] )
145
+ — parent of ` nodes `
146
+ * ` phrase ` (` string ` )
147
+ — the phrase that matched
135
148
136
- Treated as ` options.allowApostrophes ` .
149
+ ###### Returns
137
150
138
- ###### ` options.allowApostrophes `
151
+ Nothing ( ` void ` ).
139
152
140
- Passed to [ ` nlcst-normalize ` ] [ nlcst-normalize ] (` boolean ` , default: ` false ` ).
153
+ ### ` Options `
154
+
155
+ Configuration (TypeScript type).
141
156
142
- ###### ` options.allowDashes `
157
+ ##### Fields
158
+
159
+ ###### ` allowApostrophes `
143
160
144
161
Passed to [ ` nlcst-normalize ` ] [ nlcst-normalize ] (` boolean ` , default: ` false ` ).
145
162
146
- ###### ` options.allowLiterals `
163
+ ###### ` allowDashes `
147
164
148
- Include [ literal ] [ ] phrases (` boolean ` , default: ` false ` ).
165
+ Passed to [ ` nlcst-normalize ` ] [ nlcst-normalize ] (` boolean ` , default: ` false ` ).
149
166
150
- ### ` function handler(nodes, index, parent, pattern) `
167
+ ###### ` allowLiterals `
151
168
152
- Handler called when a match is found .
169
+ Include [ literal ] [ ] phrases ( ` boolean ` , default: ` false ` ) .
153
170
154
- ##### Parameters
171
+ ### ` PhrasesList `
155
172
156
- ###### ` nodes `
173
+ List of phrases (TypeScript type).
157
174
158
- List of [ sibling] [ ] s that match ` pattern ` ([ ` Array<Node> ` ] [ node ] ).
175
+ Each phrase is a space-separated list of words, where each word will be
176
+ [ normalized] [ nlcst-normalize ] to remove casing, apostrophes, and dashes.
177
+ Spaces in a pattern mean one or more whitespace nodes in the tree.
178
+ Instead of a word with letters, it’s also possible to use a wildcard symbol
179
+ (` * ` , an asterisk) which will match any word in a pattern (` alpha * charlie ` ).
159
180
160
- ###### ` index `
181
+ ###### Type
161
182
162
- [ Index] [ ] where the match starts in ` parent ` (` number ` ).
183
+ ``` ts
184
+ type PhrasesList = Array <string >
185
+ ` ` `
163
186
164
- ###### ` parent `
187
+ ### ` PhrasesMap `
165
188
166
- [ Parent ] [ ] node of ` nodes ` ( [ ` Node ` ] [ node ] ).
189
+ Map of phrases (TypeScript type ).
167
190
168
- ###### ` pattern `
191
+ ###### Type
169
192
170
- The matched pattern (` string ` ).
193
+ ` ` ` ts
194
+ type PhrasesMap = Record <string , unknown >
195
+ ` ` `
171
196
172
197
## Types
173
198
174
199
This package is fully typed with [TypeScript][].
175
- It exports the additional types ` Options ` , ` PhrasesList ` , ` PhrasesMap ` , and
176
- ` Handler ` .
200
+ It exports the additional types [ ` Handler ` ][handler], [ ` Options ` ][options],
201
+ [ ` PhrasesList ` ][phraseslist], and [ ` PhrasesMap ` ][phrasesmap] .
177
202
178
203
## Compatibility
179
204
180
205
Projects maintained by the unified collective are compatible with all maintained
181
206
versions of Node.js.
182
- As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18 .0+.
207
+ As of now, that is Node.js 14.14+ and 16 .0+.
183
208
Our projects sometimes work with older versions, but this is not guaranteed.
184
209
185
210
## Related
@@ -259,12 +284,12 @@ abide by its terms.
259
284
260
285
[nlcst-normalize]: https://github.com/syntax-tree/nlcst-normalize
261
286
262
- [ fn-handler ] : #function-handlernodes-index-parent-pattern
287
+ [search ]: #searchtree-phrases-handler-allowapostrophesoptions
263
288
264
- [ tree ] : https://github.com/syntax-tree/unist#tree
289
+ [handler ]: #handler
265
290
266
- [ sibling ] : https://github.com/syntax-tree/unist#sibling
291
+ [options ]: #options
267
292
268
- [ index ] : https://github.com/syntax-tree/unist#index
293
+ [phraseslist ]: #phraseslist
269
294
270
- [ parent ] : https://github.com/syntax-tree/unist#parent-1
295
+ [phrasesmap ]: #phrasesmap
0 commit comments