Skip to content

Commit 40871fe

Browse files
committed
Change API to accept only a tuple, or list of tuples
1 parent 35d35b1 commit 40871fe

File tree

4 files changed

+139
-189
lines changed

4 files changed

+139
-189
lines changed

index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
* @typedef {import('./lib/index.js').Find} Find
55
* @typedef {import('./lib/index.js').Replace} Replace
66
* @typedef {import('./lib/index.js').ReplaceFunction} ReplaceFunction
7-
* @typedef {import('./lib/index.js').FindAndReplaceTuple} FindAndReplaceTuple
8-
* @typedef {import('./lib/index.js').FindAndReplaceSchema} FindAndReplaceSchema
97
* @typedef {import('./lib/index.js').FindAndReplaceList} FindAndReplaceList
8+
* @typedef {import('./lib/index.js').FindAndReplaceTuple} FindAndReplaceTuple
109
*/
1110

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

lib/index.js

+21-66
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@
2626
* @typedef {Array<FindAndReplaceTuple>} FindAndReplaceList
2727
* Several find and replaces, in array form.
2828
*
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
3330
* Find and replace in tuple form.
3431
*
35-
* @typedef {ReplaceFunction | string} Replace
32+
* @typedef {ReplaceFunction | string | null | undefined} Replace
3633
* Thing to replace with.
3734
*
3835
* @callback ReplaceFunction
@@ -67,62 +64,26 @@ import escape from 'escape-string-regexp'
6764
import {visitParents} from 'unist-util-visit-parents'
6865
import {convert} from 'unist-util-is'
6966

70-
const own = {}.hasOwnProperty
71-
7267
/**
7368
* Find patterns in a tree and replace them.
7469
*
7570
* The algorithm searches the tree in *preorder* for complete values in `Text`
7671
* nodes.
7772
* Partial matches are not supported.
7873
*
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-
*
9274
* @param {Nodes} tree
9375
* Tree to change.
94-
* @param {Find | FindAndReplaceList | FindAndReplaceSchema} find
76+
* @param {FindAndReplaceList | FindAndReplaceTuple} list
9577
* Patterns to find.
96-
* @param {Options | Replace | null | undefined} [replace]
97-
* Things to replace with (when `find` is `Find`) or configuration.
9878
* @param {Options | null | undefined} [options]
9979
* Configuration (when `find` is not `Find`).
10080
* @returns {undefined}
10181
* Nothing.
10282
*/
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 || {}
12485
const ignored = convert(settings.ignore || [])
125-
const pairs = toPairs(schema)
86+
const pairs = toPairs(list)
12687
let pairIndex = -1
12788

12889
while (++pairIndex < pairs.length) {
@@ -239,39 +200,33 @@ export function findAndReplace(tree, find, replace, options) {
239200
}
240201

241202
/**
242-
* Turn a schema into pairs.
203+
* Turn a tuple or a list of tuples into pairs.
243204
*
244-
* @param {FindAndReplaceList | FindAndReplaceSchema} schema
205+
* @param {FindAndReplaceList | FindAndReplaceTuple} tupleOrList
245206
* Schema.
246207
* @returns {Pairs}
247208
* Clean pairs.
248209
*/
249-
function toPairs(schema) {
210+
function toPairs(tupleOrList) {
250211
/** @type {Pairs} */
251212
const result = []
252213

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')
255216
}
256217

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]
259224

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
269226

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])])
275230
}
276231

277232
return result

readme.md

+7-32
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
* [Install](#install)
1818
* [Use](#use)
1919
* [API](#api)
20-
* [`findAndReplace(tree, find, replace[, options])`](#findandreplacetree-find-replace-options)
20+
* [`findAndReplace(tree, list[, options])`](#findandreplacetree-list-options)
2121
* [`Find`](#find)
2222
* [`FindAndReplaceList`](#findandreplacelist)
23-
* [`FindAndReplaceSchema`](#findandreplaceschema)
2423
* [`FindAndReplaceTuple`](#findandreplacetuple)
2524
* [`Options`](#options)
2625
* [`RegExpMatchObject`](#regexpmatchobject)
@@ -123,30 +122,21 @@ paragraph[8]
123122
This package exports the identifier [`findAndReplace`][api-findandreplace].
124123
There is no default export.
125124

126-
### `findAndReplace(tree, find, replace[, options])`
125+
### `findAndReplace(tree, list[, options])`
127126

128127
Find patterns in a tree and replace them.
129128

130129
The algorithm searches the tree in *[preorder][]* for complete values in
131130
[`Text`][text] nodes.
132131
Partial matches are not supported.
133132

134-
###### Signatures
135-
136-
* `findAndReplace(tree, find, replace[, options])`
137-
* `findAndReplace(tree, search[, options])`
138-
139133
###### Parameters
140134

141135
* `tree` ([`Node`][node])
142136
— tree to change
143-
* `find` ([`Find`][api-find])
144-
— value to find and remove
145-
* `replace` ([`Replace`][api-replace])
146-
— thing to replace with
147-
* `search` ([`FindAndReplaceSchema`][api-findandreplaceschema] or
148-
[`FindAndReplaceList`][api-findandreplacelist])
149-
— several find and replaces
137+
* `list` ([`FindAndReplaceList`][api-findandreplacelist] or
138+
[`FindAndReplaceTuple`][api-findandreplacetuple])
139+
— one or more find-and-replace pairs
150140
* `options` ([`Options`][api-options])
151141
— configuration
152142

@@ -178,26 +168,14 @@ type FindAndReplaceList = Array<FindAndReplaceTuple>
178168
179169
See [`FindAndReplaceTuple`][api-findandreplacetuple].
180170
181-
### `FindAndReplaceSchema`
182-
183-
Several find and replaces, in object form (TypeScript type).
184-
185-
###### Type
186-
187-
```ts
188-
type FindAndReplaceSchema = Record<string, Replace>
189-
```
190-
191-
See [`Replace`][api-replace].
192-
193171
### `FindAndReplaceTuple`
194172
195173
Find and replace in tuple form (TypeScript type).
196174
197175
###### Type
198176
199177
```ts
200-
type FindAndReplaceTuple = [Find, Replace]
178+
type FindAndReplaceTuple = [Find, Replace?]
201179
```
202180
203181
See [`Find`][api-find] and [`Replace`][api-replace].
@@ -265,7 +243,6 @@ Thing to replace with:
265243
This package is fully typed with [TypeScript][].
266244
It exports the additional types [`Find`][api-find],
267245
[`FindAndReplaceList`][api-findandreplacelist],
268-
[`FindAndReplaceSchema`][api-findandreplaceschema],
269246
[`FindAndReplaceTuple`][api-findandreplacetuple],
270247
[`Options`][api-options],
271248
[`RegExpMatchObject`][api-regexpmatchobject],
@@ -371,7 +348,7 @@ abide by its terms.
371348
372349
[hast-util-find-and-replace]: https://github.com/syntax-tree/hast-util-find-and-replace
373350
374-
[api-findandreplace]: #findandreplacetree-find-replace-options
351+
[api-findandreplace]: #findandreplacetree-list-options
375352
376353
[api-options]: #options
377354
@@ -383,8 +360,6 @@ abide by its terms.
383360
384361
[api-findandreplacelist]: #findandreplacelist
385362
386-
[api-findandreplaceschema]: #findandreplaceschema
387-
388363
[api-findandreplacetuple]: #findandreplacetuple
389364
390365
[api-regexpmatchobject]: #regexpmatchobject

0 commit comments

Comments
 (0)