|
26 | 26 | * @typedef {Array<FindAndReplaceTuple>} FindAndReplaceList |
27 | 27 | * Several find and replaces, in array form. |
28 | 28 | * |
29 | | - * @typedef {Record<string, Replace>} FindAndReplaceSchema |
30 | | - * Several find and replaces, in object form. |
31 | | - * |
32 | | - * @typedef {[Find, Replace]} FindAndReplaceTuple |
| 29 | + * @typedef {[Find, Replace?]} FindAndReplaceTuple |
33 | 30 | * Find and replace in tuple form. |
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 |
@@ -67,62 +64,26 @@ import escape from 'escape-string-regexp' |
67 | 64 | import {visitParents} from 'unist-util-visit-parents' |
68 | 65 | import {convert} from 'unist-util-is' |
69 | 66 |
|
70 | | -const own = {}.hasOwnProperty |
71 | | - |
72 | 67 | /** |
73 | 68 | * Find patterns in a tree and replace them. |
74 | 69 | * |
75 | 70 | * The algorithm searches the tree in *preorder* for complete values in `Text` |
76 | 71 | * nodes. |
77 | 72 | * Partial matches are not supported. |
78 | 73 | * |
79 | | - * @overload |
80 | | - * @param {Nodes} tree |
81 | | - * @param {Find} find |
82 | | - * @param {Replace | null | undefined} [replace] |
83 | | - * @param {Options | null | undefined} [options] |
84 | | - * @returns {undefined} |
85 | | - * |
86 | | - * @overload |
87 | | - * @param {Nodes} tree |
88 | | - * @param {FindAndReplaceSchema | FindAndReplaceList} schema |
89 | | - * @param {Options | null | undefined} [options] |
90 | | - * @returns {undefined} |
91 | | - * |
92 | 74 | * @param {Nodes} tree |
93 | 75 | * Tree to change. |
94 | | - * @param {Find | FindAndReplaceList | FindAndReplaceSchema} find |
| 76 | + * @param {FindAndReplaceList | FindAndReplaceTuple} list |
95 | 77 | * Patterns to find. |
96 | | - * @param {Options | Replace | null | undefined} [replace] |
97 | | - * Things to replace with (when `find` is `Find`) or configuration. |
98 | 78 | * @param {Options | null | undefined} [options] |
99 | 79 | * Configuration (when `find` is not `Find`). |
100 | 80 | * @returns {undefined} |
101 | 81 | * Nothing. |
102 | 82 | */ |
103 | | -// To do: next major: remove `find` & `replace` combo, remove schema. |
104 | | -export function findAndReplace(tree, find, replace, options) { |
105 | | - /** @type {Options | null | undefined} */ |
106 | | - let settings |
107 | | - /** @type {FindAndReplaceList | FindAndReplaceSchema} */ |
108 | | - let schema |
109 | | - |
110 | | - if (typeof find === 'string' || find instanceof RegExp) { |
111 | | - // @ts-expect-error don’t expect options twice. |
112 | | - schema = [[find, replace]] |
113 | | - settings = options |
114 | | - } else { |
115 | | - schema = find |
116 | | - // @ts-expect-error don’t expect replace twice. |
117 | | - settings = replace |
118 | | - } |
119 | | - |
120 | | - if (!settings) { |
121 | | - settings = {} |
122 | | - } |
123 | | - |
| 83 | +export function findAndReplace(tree, list, options) { |
| 84 | + const settings = options || {} |
124 | 85 | const ignored = convert(settings.ignore || []) |
125 | | - const pairs = toPairs(schema) |
| 86 | + const pairs = toPairs(list) |
126 | 87 | let pairIndex = -1 |
127 | 88 |
|
128 | 89 | while (++pairIndex < pairs.length) { |
@@ -239,39 +200,33 @@ export function findAndReplace(tree, find, replace, options) { |
239 | 200 | } |
240 | 201 |
|
241 | 202 | /** |
242 | | - * Turn a schema into pairs. |
| 203 | + * Turn a tuple or a list of tuples into pairs. |
243 | 204 | * |
244 | | - * @param {FindAndReplaceList | FindAndReplaceSchema} schema |
| 205 | + * @param {FindAndReplaceList | FindAndReplaceTuple} tupleOrList |
245 | 206 | * Schema. |
246 | 207 | * @returns {Pairs} |
247 | 208 | * Clean pairs. |
248 | 209 | */ |
249 | | -function toPairs(schema) { |
| 210 | +function toPairs(tupleOrList) { |
250 | 211 | /** @type {Pairs} */ |
251 | 212 | const result = [] |
252 | 213 |
|
253 | | - if (typeof schema !== 'object') { |
254 | | - throw new TypeError('Expected array or object as schema') |
| 214 | + if (!Array.isArray(tupleOrList)) { |
| 215 | + throw new TypeError('Expected find and replace tuple or list of tuples') |
255 | 216 | } |
256 | 217 |
|
257 | | - if (Array.isArray(schema)) { |
258 | | - let index = -1 |
| 218 | + /** @type {FindAndReplaceList} */ |
| 219 | + // @ts-expect-error: correct. |
| 220 | + const list = |
| 221 | + !tupleOrList[0] || Array.isArray(tupleOrList[0]) |
| 222 | + ? tupleOrList |
| 223 | + : [tupleOrList] |
259 | 224 |
|
260 | | - while (++index < schema.length) { |
261 | | - result.push([ |
262 | | - toExpression(schema[index][0]), |
263 | | - toFunction(schema[index][1]) |
264 | | - ]) |
265 | | - } |
266 | | - } else { |
267 | | - /** @type {string} */ |
268 | | - let key |
| 225 | + let index = -1 |
269 | 226 |
|
270 | | - for (key in schema) { |
271 | | - if (own.call(schema, key)) { |
272 | | - result.push([toExpression(key), toFunction(schema[key])]) |
273 | | - } |
274 | | - } |
| 227 | + while (++index < list.length) { |
| 228 | + const tuple = list[index] |
| 229 | + result.push([toExpression(tuple[0]), toFunction(tuple[1])]) |
275 | 230 | } |
276 | 231 |
|
277 | 232 | return result |
|
0 commit comments