1717 * @typedef {Array<FindAndReplaceTuple> } FindAndReplaceList
1818 * Several find and replaces, in array form.
1919 *
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
2421 * Find and replace in tuple form.
2522 *
2623 * @typedef RegExpMatchObject
3229 * @property {[...Array<Parents>, Text] } stack
3330 * All ancestors of the text node, where the last node is the text itself.
3431 *
35- * @typedef {ReplaceFunction | string } Replace
32+ * @typedef {ReplaceFunction | string | null | undefined } Replace
3633 * Thing to replace with.
3734 *
3835 * @callback ReplaceFunction
@@ -66,7 +63,8 @@ import {visitParents} from 'unist-util-visit-parents'
6663import { convertElement } from 'hast-util-is-element'
6764import escape from 'escape-string-regexp'
6865
69- const own = { } . hasOwnProperty
66+ /** @type {Options } */
67+ const emptyOptions = { }
7068
7169/**
7270 * Default tag names to ignore.
@@ -84,52 +82,19 @@ export const defaultIgnore = ['math', 'script', 'style', 'svg', 'title']
8482 * nodes.
8583 * Partial matches are not supported.
8684 *
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- *
10085 * @param {Nodes } tree
10186 * 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]
10790 * Configuration (when `find` is not `Find`).
10891 * @returns {undefined }
10992 * Nothing.
11093 */
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
13196 const ignored = convertElement ( settings . ignore || defaultIgnore )
132- const pairs = toPairs ( schema )
97+ const pairs = toPairs ( list )
13398 let pairIndex = - 1
13499
135100 while ( ++ pairIndex < pairs . length ) {
@@ -243,39 +208,33 @@ export function findAndReplace(tree, find, replace, options) {
243208}
244209
245210/**
246- * Turn a schema into pairs.
211+ * Turn a tuple or a list of tuples into pairs.
247212 *
248- * @param {FindAndReplaceList | FindAndReplaceSchema } schema
213+ * @param {FindAndReplaceList | FindAndReplaceTuple } tupleOrList
249214 * Schema.
250215 * @returns {Pairs }
251216 * Clean pairs.
252217 */
253- function toPairs ( schema ) {
218+ function toPairs ( tupleOrList ) {
254219 /** @type {Pairs } */
255220 const result = [ ]
256221
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 ' )
259224 }
260225
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 ]
263232
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
273234
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 ] ) ] )
279238 }
280239
281240 return result
0 commit comments