Skip to content

Commit 34ecdb4

Browse files
committed
Add improved docs
1 parent 31a6e01 commit 34ecdb4

File tree

3 files changed

+91
-61
lines changed

3 files changed

+91
-61
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
* @typedef {import('./lib/index.js').Options} SearchOptions
88
* Deprecated form of `Options`.
99
*/
10+
// To do: next major: remove `SearchOptions`.
1011

1112
export {search} from './lib/index.js'

lib/index.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,27 @@
1414
*
1515
* @typedef {Array<string>} PhrasesList
1616
* List of phrases.
17+
*
18+
* Each phrase is a space-separated list of words, where each word will be
19+
* normalized to remove casing, apostrophes, and dashes.
20+
* Spaces in a pattern mean one or more whitespace nodes in the tree.
21+
* Instead of a word with letters, it’s also possible to use a wildcard
22+
* symbol (`*`, an asterisk) which will match any word in a pattern
23+
* (`alpha * charlie`).
24+
*
1725
* @typedef {Record<string, unknown>} PhrasesMap
1826
* Map where the keys are phrases.
1927
*
2028
* @callback Handler
21-
* Function called when a pattern matches.
29+
* Handle a match.
2230
* @param {Array<Content>} nodes
2331
* Match.
2432
* @param {number} index
25-
* Index of first node of `nodes` in `parent.
33+
* Index of first node of `nodes` in `parent`.
2634
* @param {Parent} parent
2735
* Parent of `nodes`.
28-
* @param {string} pattern
29-
* The pattern that matched.
36+
* @param {string} phrase
37+
* The phrase that matched.
3038
* @returns {void}
3139
* Nothing.
3240
*/
@@ -38,28 +46,24 @@ import {isLiteral} from 'nlcst-is-literal'
3846
const own = {}.hasOwnProperty
3947

4048
/**
41-
* Search for patterns a tree.
49+
* Search for phrases in a tree.
4250
*
4351
* @param {Node} tree
4452
* Tree to search.
4553
* @param {PhrasesList | PhrasesMap} phrases
46-
* Patterns.
54+
* Phrases to search for.
4755
* @param {Handler} handler
48-
* Handler.
56+
* Handle a match
4957
* @param {boolean | Options} [options=false]
50-
* Configuration.
58+
* Configuration (or `allowApostrophes`).
5159
* @returns {void}
5260
* Nothing.
5361
*/
5462
// To do: next major: remove boolean overload.
5563
// To do: next major: remove `PhrasesMap` support.
5664
export function search(tree, phrases, handler, options) {
5765
const config =
58-
typeof options === 'boolean'
59-
? options
60-
? {allowApostrophes: true}
61-
: {}
62-
: options || {}
66+
typeof options === 'boolean' ? {allowApostrophes: options} : options || {}
6367

6468
if (!tree || !tree.type) {
6569
throw new Error('Expected node')

readme.md

Lines changed: 73 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[nlcst][] utility to search for patterns in a tree.
11+
[nlcst][] utility to search for phrases in a tree.
1212

1313
## Contents
1414

@@ -17,8 +17,11 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [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)
2225
* [Types](#types)
2326
* [Compatibility](#compatibility)
2427
* [Related](#related)
@@ -27,7 +30,7 @@
2730

2831
## What is this?
2932

30-
This utility can search for patterns (words and phrases) in trees.
33+
This utility can search for phrases (words and phrases) in trees.
3134

3235
## When should I use this?
3336

@@ -37,7 +40,7 @@ and phrases.
3740
## Install
3841

3942
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][]:
4144

4245
```sh
4346
npm install nlcst-search
@@ -46,14 +49,14 @@ npm install nlcst-search
4649
In Deno with [`esm.sh`][esmsh]:
4750

4851
```js
49-
import {search} from "https://esm.sh/nlcst-search@3"
52+
import {search} from 'https://esm.sh/nlcst-search@3'
5053
```
5154

5255
In browsers with [`esm.sh`][esmsh]:
5356

5457
```html
5558
<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'
5760
</script>
5861
```
5962

@@ -104,82 +107,104 @@ search(tree, ['do blocklevel'], function(nodes) {
104107

105108
## API
106109

107-
This package exports the identifier `search`.
110+
This package exports the identifier [`search`][search].
108111
There is no default export.
109112

110-
### `search(tree, patterns, handler[, allowApostrophes|options])`
113+
### `search(tree, phrases, handler[, allowApostrophes|options])`
111114

112-
Search for patterns a [tree][].
115+
Search for phrases in a tree.
113116

114117
##### Parameters
115118

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
117129

118-
[Tree][] to search in ([`Node`][node]).
130+
###### Returns
119131

120-
###### `patterns`
132+
Nothing (`void`).
121133

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`
129135

130-
###### `handler`
136+
Handle a match (TypeScript type).
131137

132-
Handler called when a match is found ([`Handler`][fn-handler]).
138+
###### Parameters
133139

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
135148

136-
Treated as `options.allowApostrophes`.
149+
###### Returns
137150

138-
###### `options.allowApostrophes`
151+
Nothing (`void`).
139152

140-
Passed to [`nlcst-normalize`][nlcst-normalize] (`boolean`, default: `false`).
153+
### `Options`
154+
155+
Configuration (TypeScript type).
141156

142-
###### `options.allowDashes`
157+
##### Fields
158+
159+
###### `allowApostrophes`
143160

144161
Passed to [`nlcst-normalize`][nlcst-normalize] (`boolean`, default: `false`).
145162

146-
###### `options.allowLiterals`
163+
###### `allowDashes`
147164

148-
Include [literal][] phrases (`boolean`, default: `false`).
165+
Passed to [`nlcst-normalize`][nlcst-normalize] (`boolean`, default: `false`).
149166

150-
### `function handler(nodes, index, parent, pattern)`
167+
###### `allowLiterals`
151168

152-
Handler called when a match is found.
169+
Include [literal][] phrases (`boolean`, default: `false`).
153170

154-
##### Parameters
171+
### `PhrasesList`
155172

156-
###### `nodes`
173+
List of phrases (TypeScript type).
157174

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`).
159180

160-
###### `index`
181+
###### Type
161182

162-
[Index][] where the match starts in `parent` (`number`).
183+
```ts
184+
type PhrasesList = Array<string>
185+
```
163186
164-
###### `parent`
187+
### `PhrasesMap`
165188
166-
[Parent][] node of `nodes` ([`Node`][node]).
189+
Map of phrases (TypeScript type).
167190
168-
###### `pattern`
191+
###### Type
169192
170-
The matched pattern (`string`).
193+
```ts
194+
type PhrasesMap = Record<string, unknown>
195+
```
171196
172197
## Types
173198
174199
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].
177202
178203
## Compatibility
179204
180205
Projects maintained by the unified collective are compatible with all maintained
181206
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+.
183208
Our projects sometimes work with older versions, but this is not guaranteed.
184209
185210
## Related
@@ -259,12 +284,12 @@ abide by its terms.
259284
260285
[nlcst-normalize]: https://github.com/syntax-tree/nlcst-normalize
261286
262-
[fn-handler]: #function-handlernodes-index-parent-pattern
287+
[search]: #searchtree-phrases-handler-allowapostrophesoptions
263288
264-
[tree]: https://github.com/syntax-tree/unist#tree
289+
[handler]: #handler
265290
266-
[sibling]: https://github.com/syntax-tree/unist#sibling
291+
[options]: #options
267292
268-
[index]: https://github.com/syntax-tree/unist#index
293+
[phraseslist]: #phraseslist
269294
270-
[parent]: https://github.com/syntax-tree/unist#parent-1
295+
[phrasesmap]: #phrasesmap

0 commit comments

Comments
 (0)