Skip to content

Commit ffb0e1a

Browse files
committed
Add improved docs
1 parent 69aeb26 commit ffb0e1a

File tree

2 files changed

+82
-20
lines changed

2 files changed

+82
-20
lines changed

index.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
* @typedef {import('nlcst-normalize').NormalizeOptions} NormalizeOptions
99
*
1010
* @typedef {boolean} AllowApostrophes
11-
* @typedef {NormalizeOptions & {allowLiterals?: boolean}} SearchOptions
12-
*
11+
* @typedef {NormalizeOptions & {allowLiterals?: boolean}} Options
12+
* Configuration (optional).
1313
* @typedef {Array<string>} PhrasesList
14+
* List of phrases.
1415
* @typedef {Record<string, unknown>} PhrasesMap
15-
*
16+
* Map where the keys are phrases.
1617
* @typedef {(nodes: Array<Content>, index: number, parent: Parent, pattern: string) => void} Handler
18+
* Function called when a pattern matches.
19+
*
20+
* @typedef {Options} SearchOptions
21+
* Deprecated form of `Options`.
1722
*/
1823

1924
import {visit} from 'unist-util-visit'
@@ -26,15 +31,15 @@ const own = {}.hasOwnProperty
2631
* @param {Node} tree
2732
* @param {PhrasesList|PhrasesMap} phrases
2833
* @param {Handler} handler
29-
* @param {AllowApostrophes|SearchOptions} [options=false]
34+
* @param {AllowApostrophes|Options} [options=false]
3035
*/
3136
export function search(tree, phrases, handler, options) {
3237
/** @type {Record<string, Array<string>>} */
3338
const byWord = {'*': []}
3439
let index = -1
3540
/** @type {string} */
3641
let key
37-
/** @type {SearchOptions} */
42+
/** @type {Options} */
3843
let config
3944

4045
if (typeof options === 'boolean') {

readme.md

+72-15
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,55 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

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

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+
* [`search(tree, patterns, handler[, allowApostrophes|options])`](#searchtree-patterns-handler-allowapostrophesoptions)
21+
* [`function handler(nodes, index, parent, pattern)`](#function-handlernodes-index-parent-pattern)
22+
* [Types](#types)
23+
* [Compatibility](#compatibility)
24+
* [Related](#related)
25+
* [Contribute](#contribute)
26+
* [License](#license)
27+
28+
## What is this?
1429

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.
30+
This utility can search for patterns (words and phrases) in trees.
1731

18-
[npm][]:
32+
## When should I use this?
33+
34+
This package is a tiny utility that helps when you’re searching for words
35+
and phrases.
36+
37+
## Install
38+
39+
This package is [ESM only][esm].
40+
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
1941

2042
```sh
2143
npm install nlcst-search
2244
```
2345

46+
In Deno with [`esm.sh`][esmsh]:
47+
48+
```js
49+
import {search} from "https://esm.sh/nlcst-search@3"
50+
```
51+
52+
In browsers with [`esm.sh`][esmsh]:
53+
54+
```html
55+
<script type="module">
56+
import {search} from "https://esm.sh/nlcst-search@3?bundle"
57+
</script>
58+
```
59+
2460
## Use
2561

2662
```js
@@ -68,7 +104,7 @@ search(tree, ['do blocklevel'], function(nodes) {
68104

69105
## API
70106

71-
This package exports the following identifiers: `search`.
107+
This package exports the identifier `search`.
72108
There is no default export.
73109

74110
### `search(tree, patterns, handler[, allowApostrophes|options])`
@@ -93,7 +129,7 @@ asterisk), that matches any word in a pattern (`alpha * charlie`).
93129

94130
###### `handler`
95131

96-
Handler invoked when a match is found ([`Function`][fn-handler]).
132+
Handler called when a match is found ([`Handler`][fn-handler]).
97133

98134
###### `allowApostrophes`
99135

@@ -113,7 +149,7 @@ Include [literal][] phrases (`boolean`, default: `false`).
113149

114150
### `function handler(nodes, index, parent, pattern)`
115151

116-
Handler invoked when a match is found.
152+
Handler called when a match is found.
117153

118154
##### Parameters
119155

@@ -133,17 +169,30 @@ List of [sibling][]s that match `pattern` ([`Array<Node>`][node]).
133169

134170
The matched pattern (`string`).
135171

172+
## Types
173+
174+
This package is fully typed with [TypeScript][].
175+
It exports the additional types `Options`, `PhrasesList`, `PhrasesMap`, and
176+
`Handler`.
177+
178+
## Compatibility
179+
180+
Projects maintained by the unified collective are compatible with all maintained
181+
versions of Node.js.
182+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
183+
Our projects sometimes work with older versions, but this is not guaranteed.
184+
136185
## Related
137186

138187
* [`nlcst-normalize`](https://github.com/syntax-tree/nlcst-normalize)
139-
Normalize a word for easier comparison
188+
normalize a word for easier comparison
140189
* [`nlcst-is-literal`](https://github.com/syntax-tree/nlcst-is-literal)
141-
Check whether a node is meant literally
190+
check whether a node is meant literally
142191

143192
## Contribute
144193

145-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
146-
started.
194+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
195+
ways to get started.
147196
See [`support.md`][support] for ways to get help.
148197

149198
This project has a [code of conduct][coc].
@@ -184,15 +233,23 @@ abide by its terms.
184233

185234
[npm]: https://docs.npmjs.com/cli/install
186235

236+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
237+
238+
[esmsh]: https://esm.sh
239+
240+
[typescript]: https://www.typescriptlang.org
241+
187242
[license]: license
188243

189244
[author]: https://wooorm.com
190245

191-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
246+
[health]: https://github.com/syntax-tree/.github
247+
248+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
192249

193-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
250+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
194251

195-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
252+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
196253

197254
[nlcst]: https://github.com/syntax-tree/nlcst
198255

0 commit comments

Comments
 (0)