Skip to content

Commit 4890c82

Browse files
committed
Change API to accept only a tuple, or list of tuples
1 parent 3652e60 commit 4890c82

File tree

4 files changed

+126
-183
lines changed

4 files changed

+126
-183
lines changed

index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
/**
2-
* @typedef {import('./lib/index.js').Options} Options
32
* @typedef {import('./lib/index.js').Find} Find
3+
* @typedef {import('./lib/index.js').FindAndReplaceList} FindAndReplaceList
4+
* @typedef {import('./lib/index.js').FindAndReplaceTuple} FindAndReplaceTuple
5+
* @typedef {import('./lib/index.js').Options} Options
46
* @typedef {import('./lib/index.js').Replace} Replace
57
* @typedef {import('./lib/index.js').ReplaceFunction} ReplaceFunction
68
* @typedef {import('./lib/index.js').RegExpMatchObject} RegExpMatchObject
7-
* @typedef {import('./lib/index.js').FindAndReplaceTuple} FindAndReplaceTuple
8-
* @typedef {import('./lib/index.js').FindAndReplaceSchema} FindAndReplaceSchema
9-
* @typedef {import('./lib/index.js').FindAndReplaceList} FindAndReplaceList
109
*/
1110

1211
export {defaultIgnore, findAndReplace} from './lib/index.js'

lib/index.js

Lines changed: 25 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
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
@@ -32,7 +29,7 @@
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'
6663
import {convertElement} from 'hast-util-is-element'
6764
import 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

readme.md

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
* [Use](#use)
1919
* [API](#api)
2020
* [`defaultIgnore`](#defaultignore)
21-
* [`findAndReplace(tree, find, replace[, options])`](#findandreplacetree-find-replace-options)
21+
* [`findAndReplace(tree, list[, options])`](#findandreplacetree-list-options)
2222
* [`Find`](#find)
2323
* [`FindAndReplaceList`](#findandreplacelist)
24-
* [`FindAndReplaceSchema`](#findandreplaceschema)
2524
* [`FindAndReplaceTuple`](#findandreplacetuple)
2625
* [`Options`](#options)
2726
* [`RegExpMatchObject`](#regexpmatchobject)
@@ -137,30 +136,21 @@ Default tag names to ignore (`Array<string>`).
137136

138137
The defaults are `math`, `script`, `style`, `svg`, and `title`.
139138

140-
### `findAndReplace(tree, find, replace[, options])`
139+
### `findAndReplace(tree, list[, options])`
141140

142141
Find patterns in a tree and replace them.
143142

144143
The algorithm searches the tree in *[preorder][]* for complete values in
145144
[`Text`][text] nodes.
146145
Partial matches are not supported.
147146

148-
###### Signatures
149-
150-
* `findAndReplace(tree, find, replace[, options])`
151-
* `findAndReplace(tree, search[, options])`
152-
153147
###### Parameters
154148

155149
* `tree` ([`Node`][node])
156150
— tree to change
157-
* `find` ([`Find`][api-find])
158-
— value to find and remove
159-
* `replace` ([`Replace`][api-replace])
160-
— thing to replace with
161-
* `search` ([`FindAndReplaceSchema`][api-find-and-replace-schema] or
162-
[`FindAndReplaceList`][api-find-and-replace-list])
163-
— several find and replaces
151+
* `list` ([`FindAndReplaceList`][api-find-and-replace-list] or
152+
[`FindAndReplaceTuple`][api-find-and-replace-tuple])
153+
— one or more find-and-replace pairs
164154
* `options` ([`Options`][api-options])
165155
— configuration
166156

@@ -192,26 +182,14 @@ type FindAndReplaceList = Array<FindAndReplaceTuple>
192182
193183
See [`FindAndReplaceTuple`][api-find-and-replace-tuple].
194184
195-
### `FindAndReplaceSchema`
196-
197-
Several find and replaces, in object form (TypeScript type).
198-
199-
###### Type
200-
201-
```ts
202-
type FindAndReplaceSchema = Record<string, Replace>
203-
```
204-
205-
See [`Replace`][api-replace].
206-
207185
### `FindAndReplaceTuple`
208186
209187
Find and replace in tuple form (TypeScript type).
210188
211189
###### Type
212190
213191
```ts
214-
type FindAndReplaceTuple = [Find, Replace]
192+
type FindAndReplaceTuple = [Find, Replace?]
215193
```
216194
217195
See [`Find`][api-find] and [`Replace`][api-replace].
@@ -245,7 +223,7 @@ Thing to replace with (TypeScript type).
245223
###### Type
246224
247225
```ts
248-
type Replace = ReplaceFunction | string
226+
type Replace = ReplaceFunction | string | null | undefined
249227
```
250228
251229
See [`ReplaceFunction`][api-replace-function].
@@ -279,7 +257,6 @@ Thing to replace with:
279257
This package is fully typed with [TypeScript][].
280258
It exports the additional types [`Find`][api-find],
281259
[`FindAndReplaceList`][api-find-and-replace-list],
282-
[`FindAndReplaceSchema`][api-find-and-replace-schema],
283260
[`FindAndReplaceTuple`][api-find-and-replace-tuple],
284261
[`Options`][api-options],
285262
[`RegExpMatchObject`][api-regexp-match-object],
@@ -406,7 +383,7 @@ abide by its terms.
406383

407384
[api-default-ignore]: #defaultignore
408385

409-
[api-find-and-replace]: #findandreplacetree-find-replace-options
386+
[api-find-and-replace]: #findandreplacetree-list-options
410387

411388
[api-options]: #options
412389

@@ -418,8 +395,6 @@ abide by its terms.
418395

419396
[api-find-and-replace-list]: #findandreplacelist
420397

421-
[api-find-and-replace-schema]: #findandreplaceschema
422-
423398
[api-find-and-replace-tuple]: #findandreplacetuple
424399

425400
[api-regexp-match-object]: #regexpmatchobject

0 commit comments

Comments
 (0)