Skip to content

Commit 355596a

Browse files
committed
Refactor code-style
* Add support for `null` as input in API types * Add more docs to JSDoc
1 parent bc5a7ba commit 355596a

File tree

1 file changed

+66
-26
lines changed

1 file changed

+66
-26
lines changed

lib/index.js

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,52 @@
11
/**
2-
* @typedef Options
3-
* @property {Test} [ignore]
4-
*
52
* @typedef {import('hast').Text} Text
6-
* @typedef {import('hast').Parent} Parent
73
* @typedef {import('hast').Root} Root
84
* @typedef {import('hast').Element} Element
95
* @typedef {import('hast').Content} Content
10-
* @typedef {Root|Content} Node
11-
*
126
* @typedef {import('hast-util-is-element').Test} Test
137
* @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult
8+
*/
9+
10+
/**
11+
* @typedef {Root | Content} Node
1412
*
1513
* @typedef RegExpMatchObject
14+
* Info on the match.
1615
* @property {number} index
16+
* The index of the search at which the result was found.
1717
* @property {string} input
18+
* A copy of the search string in the text node.
1819
* @property {[Root, ...Array<Element>, Text]} stack
20+
* All ancestors of the text node, where the last node is the text itself.
1921
*
20-
* @typedef {string|RegExp} Find
21-
* @typedef {string|ReplaceFunction} Replace
22+
* @callback ReplaceFunction
23+
* Callback called when a search matches.
24+
* @param {...any} parameters
25+
* The parameters are the result of the search expressions, which can be
26+
* several if a regex captures groups.
27+
*
28+
* The last parameter is a `RegExpMatchObject`.
29+
* @returns {Array<Content> | Content | string | false | undefined | null}
30+
* Resulting nodes (where `string` is turned into a `Text` node).
2231
*
32+
* The values `null` and `undefined` and an empty string mean the match is
33+
* removed.
34+
*
35+
* The value `false` means that this isn’t a match after all, and it is not
36+
* replaced.
37+
*
38+
* @typedef {string | RegExp} Find
39+
* @typedef {string | ReplaceFunction} Replace
2340
* @typedef {[Find, Replace]} FindAndReplaceTuple
2441
* @typedef {Record<string, Replace>} FindAndReplaceSchema
2542
* @typedef {Array<FindAndReplaceTuple>} FindAndReplaceList
26-
*
2743
* @typedef {[RegExp, ReplaceFunction]} Pair
2844
* @typedef {Array<Pair>} Pairs
29-
*/
30-
31-
/**
32-
* @callback ReplaceFunction
33-
* @param {...any} parameters
34-
* @returns {Array<Content>|Content|string|false|undefined|null}
45+
*
46+
* @typedef Options
47+
* Configuration.
48+
* @property {Test | null | undefined} [ignore]
49+
* Test for which elements to ignore.
3550
*/
3651

3752
import {visitParents} from 'unist-util-visit-parents'
@@ -43,15 +58,25 @@ const own = {}.hasOwnProperty
4358
export const defaultIgnore = ['title', 'script', 'style', 'svg', 'math']
4459

4560
/**
61+
* Find patterns in a tree and replace them.
62+
*
63+
* The algorithm searches the tree in *preorder* for complete values in `Text`
64+
* nodes.
65+
* Partial matches are not supported.
66+
*
4667
* @param {Node} tree
47-
* @param {Find|FindAndReplaceSchema|FindAndReplaceList} find
48-
* @param {Replace|Options} [replace]
49-
* @param {Options} [options]
68+
* Tree to change.
69+
* @param {Find | FindAndReplaceSchema | FindAndReplaceList} find
70+
* Patterns to find.
71+
* @param {Replace | Options | null | undefined} [replace]
72+
* `Replace` (when `find` is `Find`) or configuration.
73+
* @param {Options | null | undefined} [options]
74+
* Configuration (when `find` is not `Find`).
5075
*/
5176
export function findAndReplace(tree, find, replace, options) {
52-
/** @type {Options|undefined} */
77+
/** @type {Options | null | undefined} */
5378
let settings
54-
/** @type {FindAndReplaceSchema|FindAndReplaceList} */
79+
/** @type {FindAndReplaceSchema | FindAndReplaceList} */
5580
let schema
5681

5782
if (typeof find === 'string' || find instanceof RegExp) {
@@ -81,7 +106,7 @@ export function findAndReplace(tree, find, replace, options) {
81106
/** @type {import('unist-util-visit-parents/complex-types.js').BuildVisitor<Node, 'text'>} */
82107
function visitor(node, parents) {
83108
let index = -1
84-
/** @type {Root|Element|undefined} */
109+
/** @type {Root | Element | undefined} */
85110
let grandparent
86111

87112
while (++index < parents.length) {
@@ -107,9 +132,14 @@ export function findAndReplace(tree, find, replace, options) {
107132
}
108133

109134
/**
135+
* Handle a text node which is not in an ignored parent.
136+
*
110137
* @param {Text} node
111-
* @param {Array<Root|Element>} parents
138+
* Text node.
139+
* @param {Array<Root | Element>} parents
140+
* Parents.
112141
* @returns {VisitorResult}
142+
* Result.
113143
*/
114144
function handler(node, parents) {
115145
const parent = parents[parents.length - 1]
@@ -120,15 +150,13 @@ export function findAndReplace(tree, find, replace, options) {
120150
let change = false
121151
/** @type {Array<Content>} */
122152
let nodes = []
123-
/** @type {number|undefined} */
124-
let position
125153

126154
find.lastIndex = 0
127155

128156
let match = find.exec(node.value)
129157

130158
while (match) {
131-
position = match.index
159+
const position = match.index
132160
/** @type {RegExpMatchObject} */
133161
const matchObject = {
134162
index: match.index,
@@ -180,8 +208,12 @@ export function findAndReplace(tree, find, replace, options) {
180208
}
181209

182210
/**
183-
* @param {FindAndReplaceSchema|FindAndReplaceList} schema
211+
* Turn a schema into pairs.
212+
*
213+
* @param {FindAndReplaceSchema | FindAndReplaceList} schema
214+
* Schema.
184215
* @returns {Pairs}
216+
* Clean pairs.
185217
*/
186218
function toPairs(schema) {
187219
/** @type {Pairs} */
@@ -215,16 +247,24 @@ function toPairs(schema) {
215247
}
216248

217249
/**
250+
* Turn a find into an expression.
251+
*
218252
* @param {Find} find
253+
* Find.
219254
* @returns {RegExp}
255+
* Expression.
220256
*/
221257
function toExpression(find) {
222258
return typeof find === 'string' ? new RegExp(escape(find), 'g') : find
223259
}
224260

225261
/**
262+
* Turn a replace into a function.
263+
*
226264
* @param {Replace} replace
265+
* Replace.
227266
* @returns {ReplaceFunction}
267+
* Function.
228268
*/
229269
function toFunction(replace) {
230270
return typeof replace === 'function' ? replace : () => replace

0 commit comments

Comments
 (0)