17
17
* @typedef {Array<FindAndReplaceTuple> } FindAndReplaceList
18
18
* Several find and replaces, in array form.
19
19
*
20
- * @typedef {Record<string, Replace> } FindAndReplaceSchema
21
- * Several find and replaces, in object form.
22
- *
23
- * @typedef {[Find, Replace] } FindAndReplaceTuple
20
+ * @typedef {[Find, Replace?] } FindAndReplaceTuple
24
21
* Find and replace in tuple form.
25
22
*
26
23
* @typedef RegExpMatchObject
32
29
* @property {[...Array<Parents>, Text] } stack
33
30
* All ancestors of the text node, where the last node is the text itself.
34
31
*
35
- * @typedef {ReplaceFunction | string } Replace
32
+ * @typedef {ReplaceFunction | string | null | undefined } Replace
36
33
* Thing to replace with.
37
34
*
38
35
* @callback ReplaceFunction
@@ -66,7 +63,8 @@ import {visitParents} from 'unist-util-visit-parents'
66
63
import { convertElement } from 'hast-util-is-element'
67
64
import escape from 'escape-string-regexp'
68
65
69
- const own = { } . hasOwnProperty
66
+ /** @type {Options } */
67
+ const emptyOptions = { }
70
68
71
69
/**
72
70
* Default tag names to ignore.
@@ -84,52 +82,19 @@ export const defaultIgnore = ['math', 'script', 'style', 'svg', 'title']
84
82
* nodes.
85
83
* Partial matches are not supported.
86
84
*
87
- * @overload
88
- * @param {Nodes } tree
89
- * @param {Find } find
90
- * @param {Replace | null | undefined } [replace]
91
- * @param {Options | null | undefined } [options]
92
- * @returns {undefined }
93
- *
94
- * @overload
95
- * @param {Nodes } tree
96
- * @param {FindAndReplaceSchema | FindAndReplaceList } schema
97
- * @param {Options | null | undefined } [options]
98
- * @returns {undefined }
99
- *
100
85
* @param {Nodes } tree
101
86
* Tree to change.
102
- * @param {Find | FindAndReplaceList | FindAndReplaceSchema } find
103
- * Patterns to find.
104
- * @param {Options | Replace | null | undefined } [replace]
105
- * Things to replace with (when `find` is `Find`) or configuration.
106
- * @param {Options | null | undefined } [options]
87
+ * @param {FindAndReplaceList | FindAndReplaceTuple } list
88
+ * One or more find-and-replace pairs.
89
+ * @param {Readonly<Options> | null | undefined } [options]
107
90
* Configuration (when `find` is not `Find`).
108
91
* @returns {undefined }
109
92
* Nothing.
110
93
*/
111
- export function findAndReplace ( tree , find , replace , options ) {
112
- /** @type {Options | null | undefined } */
113
- let settings
114
- /** @type {FindAndReplaceSchema | FindAndReplaceList } */
115
- let schema
116
-
117
- if ( typeof find === 'string' || find instanceof RegExp ) {
118
- // @ts -expect-error don’t expect options twice.
119
- schema = [ [ find , replace ] ]
120
- settings = options
121
- } else {
122
- schema = find
123
- // @ts -expect-error don’t expect replace twice.
124
- settings = replace
125
- }
126
-
127
- if ( ! settings ) {
128
- settings = { }
129
- }
130
-
94
+ export function findAndReplace ( tree , list , options ) {
95
+ const settings = options || emptyOptions
131
96
const ignored = convertElement ( settings . ignore || defaultIgnore )
132
- const pairs = toPairs ( schema )
97
+ const pairs = toPairs ( list )
133
98
let pairIndex = - 1
134
99
135
100
while ( ++ pairIndex < pairs . length ) {
@@ -243,39 +208,33 @@ export function findAndReplace(tree, find, replace, options) {
243
208
}
244
209
245
210
/**
246
- * Turn a schema into pairs.
211
+ * Turn a tuple or a list of tuples into pairs.
247
212
*
248
- * @param {FindAndReplaceList | FindAndReplaceSchema } schema
213
+ * @param {FindAndReplaceList | FindAndReplaceTuple } tupleOrList
249
214
* Schema.
250
215
* @returns {Pairs }
251
216
* Clean pairs.
252
217
*/
253
- function toPairs ( schema ) {
218
+ function toPairs ( tupleOrList ) {
254
219
/** @type {Pairs } */
255
220
const result = [ ]
256
221
257
- if ( typeof schema !== 'object' ) {
258
- throw new TypeError ( 'Expected array or object as schema ' )
222
+ if ( ! Array . isArray ( tupleOrList ) ) {
223
+ throw new TypeError ( 'Expected find and replace tuple or list of tuples ' )
259
224
}
260
225
261
- if ( Array . isArray ( schema ) ) {
262
- let index = - 1
226
+ /** @type {FindAndReplaceList } */
227
+ // @ts -expect-error: correct.
228
+ const list =
229
+ ! tupleOrList [ 0 ] || Array . isArray ( tupleOrList [ 0 ] )
230
+ ? tupleOrList
231
+ : [ tupleOrList ]
263
232
264
- while ( ++ index < schema . length ) {
265
- result . push ( [
266
- toExpression ( schema [ index ] [ 0 ] ) ,
267
- toFunction ( schema [ index ] [ 1 ] )
268
- ] )
269
- }
270
- } else {
271
- /** @type {string } */
272
- let key
233
+ let index = - 1
273
234
274
- for ( key in schema ) {
275
- if ( own . call ( schema , key ) ) {
276
- result . push ( [ toExpression ( key ) , toFunction ( schema [ key ] ) ] )
277
- }
278
- }
235
+ while ( ++ index < list . length ) {
236
+ const tuple = list [ index ]
237
+ result . push ( [ toExpression ( tuple [ 0 ] ) , toFunction ( tuple [ 1 ] ) ] )
279
238
}
280
239
281
240
return result
0 commit comments