@@ -16,13 +16,15 @@ test('findAndReplace', async function (t) {
16
16
assert . throws ( function ( ) {
17
17
// @ts -expect-error: check that the runtime throws an error.
18
18
findAndReplace ( create ( ) , true )
19
- } , / ^ T y p e E r r o r : E x p e c t e d a r r a y o r o b j e c t a s s c h e m a $ / )
19
+ } , / E x p e c t e d f i n d a n d r e p l a c e t u p l e o r l i s t o f t u p l e s / )
20
20
}
21
21
)
22
22
23
23
await t . test ( 'should remove without `replace`' , async function ( ) {
24
24
const tree = create ( )
25
- findAndReplace ( tree , 'emphasis' )
25
+
26
+ findAndReplace ( tree , [ 'emphasis' ] )
27
+
26
28
assert . deepEqual (
27
29
tree ,
28
30
u ( 'paragraph' , [
@@ -38,10 +40,10 @@ test('findAndReplace', async function (t) {
38
40
} )
39
41
40
42
await t . test (
41
- 'should work when given `find` and ` replace` ' ,
43
+ 'should work when given a find- and- replace tuple ' ,
42
44
async function ( ) {
43
45
const tree = create ( )
44
- findAndReplace ( tree , 'emphasis' , '!!!' )
46
+ findAndReplace ( tree , [ 'emphasis' , '!!!' ] )
45
47
assert . deepEqual (
46
48
tree ,
47
49
u ( 'paragraph' , [
@@ -62,13 +64,12 @@ test('findAndReplace', async function (t) {
62
64
async function ( ) {
63
65
const tree = create ( )
64
66
65
- findAndReplace (
66
- tree ,
67
+ findAndReplace ( tree , [
67
68
/ e m ( \w + ) i s / ,
68
69
function ( /** @type {string } */ _ , /** @type {string } */ $1 ) {
69
70
return '[' + $1 + ']'
70
71
}
71
- )
72
+ ] )
72
73
73
74
assert . deepEqual (
74
75
tree ,
@@ -90,9 +91,12 @@ test('findAndReplace', async function (t) {
90
91
async function ( ) {
91
92
const tree = create ( )
92
93
93
- findAndReplace ( tree , 'emphasis' , function ( ) {
94
- return ''
95
- } )
94
+ findAndReplace ( tree , [
95
+ 'emphasis' ,
96
+ function ( ) {
97
+ return ''
98
+ }
99
+ ] )
96
100
97
101
assert . deepEqual (
98
102
tree ,
@@ -114,9 +118,12 @@ test('findAndReplace', async function (t) {
114
118
async function ( ) {
115
119
const tree = create ( )
116
120
117
- findAndReplace ( tree , 'emphasis' , function ( ) {
118
- return u ( 'delete' , [ u ( 'break' ) ] )
119
- } )
121
+ findAndReplace ( tree , [
122
+ 'emphasis' ,
123
+ function ( ) {
124
+ return u ( 'delete' , [ u ( 'break' ) ] )
125
+ }
126
+ ] )
120
127
121
128
assert . deepEqual (
122
129
tree ,
@@ -138,9 +145,12 @@ test('findAndReplace', async function (t) {
138
145
async function ( ) {
139
146
const tree = create ( )
140
147
141
- findAndReplace ( tree , 'emphasis' , function ( ) {
142
- return [ u ( 'delete' , [ ] ) , u ( 'break' ) ]
143
- } )
148
+ findAndReplace ( tree , [
149
+ 'emphasis' ,
150
+ function ( ) {
151
+ return [ u ( 'delete' , [ ] ) , u ( 'break' ) ]
152
+ }
153
+ ] )
144
154
145
155
assert . deepEqual (
146
156
tree ,
@@ -157,59 +167,43 @@ test('findAndReplace', async function (t) {
157
167
}
158
168
)
159
169
160
- await t . test (
161
- 'should work when given `search` as an matrix of strings' ,
162
- async function ( ) {
163
- const tree = create ( )
170
+ await t . test ( 'should work when given a list of tuples' , async function ( ) {
171
+ const tree = create ( )
164
172
165
- findAndReplace ( tree , [
166
- [ 'emphasis' , '!!!' ] ,
167
- [ 'importance' , '???' ]
168
- ] )
173
+ findAndReplace ( tree , [
174
+ [ 'emphasis' , '!!!' ] ,
175
+ [ 'importance' , '???' ]
176
+ ] )
169
177
170
- assert . deepEqual (
171
- tree ,
172
- u ( 'paragraph' , [
173
- u ( 'text' , 'Some ' ) ,
174
- u ( 'emphasis' , [ u ( 'text' , '!!!' ) ] ) ,
175
- u ( 'text' , ', ' ) ,
176
- u ( 'strong' , [ u ( 'text' , '???' ) ] ) ,
177
- u ( 'text' , ', and ' ) ,
178
- u ( 'inlineCode' , 'code' ) ,
179
- u ( 'text' , '.' )
180
- ] )
181
- )
182
- }
183
- )
178
+ assert . deepEqual (
179
+ tree ,
180
+ u ( 'paragraph' , [
181
+ u ( 'text' , 'Some ' ) ,
182
+ u ( 'emphasis' , [ u ( 'text' , '!!!' ) ] ) ,
183
+ u ( 'text' , ', ' ) ,
184
+ u ( 'strong' , [ u ( 'text' , '???' ) ] ) ,
185
+ u ( 'text' , ', and ' ) ,
186
+ u ( 'inlineCode' , 'code' ) ,
187
+ u ( 'text' , '.' )
188
+ ] )
189
+ )
190
+ } )
184
191
185
192
await t . test (
186
- 'should work when given `search` as an object of strings ' ,
193
+ 'should work when given an empty list of tuples ' ,
187
194
async function ( ) {
188
195
const tree = create ( )
189
196
190
- findAndReplace ( tree , { emp : 'hacks' , ',' : '!' } )
197
+ findAndReplace ( tree , [ ] )
191
198
192
- assert . deepEqual (
193
- tree ,
194
- u ( 'paragraph' , [
195
- u ( 'text' , 'Some ' ) ,
196
- u ( 'emphasis' , [ u ( 'text' , 'hacks' ) , u ( 'text' , 'hasis' ) ] ) ,
197
- u ( 'text' , '!' ) ,
198
- u ( 'text' , ' ' ) ,
199
- u ( 'strong' , [ u ( 'text' , 'importance' ) ] ) ,
200
- u ( 'text' , '!' ) ,
201
- u ( 'text' , ' and ' ) ,
202
- u ( 'inlineCode' , 'code' ) ,
203
- u ( 'text' , '.' )
204
- ] )
205
- )
199
+ assert . deepEqual ( tree , create ( ) )
206
200
}
207
201
)
208
202
209
203
await t . test ( 'should work on partial matches' , async function ( ) {
210
204
const tree = create ( )
211
205
212
- findAndReplace ( tree , / \B m p \B / , '[MP]' )
206
+ findAndReplace ( tree , [ / \B m p \B / , '[MP]' ] )
213
207
214
208
assert . deepEqual (
215
209
tree ,
@@ -228,12 +222,15 @@ test('findAndReplace', async function (t) {
228
222
await t . test ( 'should find-and-replace recursively' , async function ( ) {
229
223
const tree = create ( )
230
224
231
- findAndReplace ( tree , {
232
- emphasis ( ) {
233
- return u ( 'link' , { url : 'x' } , [ u ( 'text' , 'importance' ) ] )
234
- } ,
235
- importance : 'something else'
236
- } )
225
+ findAndReplace ( tree , [
226
+ [
227
+ 'emphasis' ,
228
+ function ( ) {
229
+ return u ( 'link' , { url : 'x' } , [ u ( 'text' , 'importance' ) ] )
230
+ }
231
+ ] ,
232
+ [ 'importance' , 'something else' ]
233
+ ] )
237
234
238
235
assert . deepEqual (
239
236
tree ,
@@ -259,7 +256,7 @@ test('findAndReplace', async function (t) {
259
256
u ( 'text' , '.' )
260
257
] )
261
258
262
- findAndReplace ( tree , 'importance' , '!!!' , { ignore : 'strong' } )
259
+ findAndReplace ( tree , [ 'importance' , '!!!' ] , { ignore : 'strong' } )
263
260
264
261
assert . deepEqual (
265
262
tree ,
@@ -278,17 +275,26 @@ test('findAndReplace', async function (t) {
278
275
u ( 'text' , 'Some emphasis, importance, and code.' )
279
276
] )
280
277
281
- findAndReplace ( tree , {
282
- importance ( /** @type {string } */ value ) {
283
- return u ( 'strong' , [ u ( 'text' , value ) ] )
284
- } ,
285
- code ( /** @type {string } */ value ) {
286
- return u ( 'inlineCode' , value )
287
- } ,
288
- emphasis ( /** @type {string } */ value ) {
289
- return u ( 'emphasis' , [ u ( 'text' , value ) ] )
290
- }
291
- } )
278
+ findAndReplace ( tree , [
279
+ [
280
+ 'importance' ,
281
+ function ( /** @type {string } */ value ) {
282
+ return u ( 'strong' , [ u ( 'text' , value ) ] )
283
+ }
284
+ ] ,
285
+ [
286
+ 'code' ,
287
+ function ( /** @type {string } */ value ) {
288
+ return u ( 'inlineCode' , value )
289
+ }
290
+ ] ,
291
+ [
292
+ 'emphasis' ,
293
+ function ( /** @type {string } */ value ) {
294
+ return u ( 'emphasis' , [ u ( 'text' , value ) ] )
295
+ }
296
+ ]
297
+ ] )
292
298
293
299
assert . deepEqual ( tree , create ( ) )
294
300
} )
@@ -346,9 +352,12 @@ test('findAndReplace', async function (t) {
346
352
await t . test ( 'should not replace when returning false' , async function ( ) {
347
353
const tree = create ( )
348
354
349
- findAndReplace ( tree , 'emphasis' , function ( ) {
350
- return false
351
- } )
355
+ findAndReplace ( tree , [
356
+ 'emphasis' ,
357
+ function ( ) {
358
+ return false
359
+ }
360
+ ] )
352
361
353
362
assert . deepEqual (
354
363
tree ,
@@ -367,9 +376,12 @@ test('findAndReplace', async function (t) {
367
376
await t . test ( 'should not recurse into a replaced value' , async function ( ) {
368
377
const tree = u ( 'paragraph' , [ u ( 'text' , 'asd.' ) ] )
369
378
370
- findAndReplace ( tree , 'asd' , function ( /** @type {string } */ d ) {
371
- return d
372
- } )
379
+ findAndReplace ( tree , [
380
+ 'asd' ,
381
+ function ( /** @type {string } */ d ) {
382
+ return d
383
+ }
384
+ ] )
373
385
374
386
assert . deepEqual ( tree , u ( 'paragraph' , [ u ( 'text' , 'asd' ) , u ( 'text' , '.' ) ] ) )
375
387
} )
@@ -379,9 +391,12 @@ test('findAndReplace', async function (t) {
379
391
async function ( ) {
380
392
const tree = u ( 'paragraph' , [ u ( 'text' , 'asd.' ) ] )
381
393
382
- findAndReplace ( tree , 'asd' , function ( /** @type {string } */ d ) {
383
- return u ( 'emphasis' , [ u ( 'text' , d ) ] )
384
- } )
394
+ findAndReplace ( tree , [
395
+ 'asd' ,
396
+ function ( /** @type {string } */ d ) {
397
+ return u ( 'emphasis' , [ u ( 'text' , d ) ] )
398
+ }
399
+ ] )
385
400
386
401
assert . deepEqual (
387
402
tree ,
@@ -395,9 +410,12 @@ test('findAndReplace', async function (t) {
395
410
async function ( ) {
396
411
const tree = u ( 'paragraph' , [ u ( 'text' , '.asd' ) ] )
397
412
398
- findAndReplace ( tree , 'asd' , function ( /** @type {string } */ d ) {
399
- return u ( 'emphasis' , [ u ( 'text' , d ) ] )
400
- } )
413
+ findAndReplace ( tree , [
414
+ 'asd' ,
415
+ function ( /** @type {string } */ d ) {
416
+ return u ( 'emphasis' , [ u ( 'text' , d ) ] )
417
+ }
418
+ ] )
401
419
402
420
assert . deepEqual (
403
421
tree ,
@@ -411,9 +429,12 @@ test('findAndReplace', async function (t) {
411
429
async function ( ) {
412
430
const tree = u ( 'paragraph' , [ u ( 'text' , 'asd' ) ] )
413
431
414
- findAndReplace ( tree , 'asd' , function ( /** @type {string } */ d ) {
415
- return u ( 'emphasis' , [ u ( 'text' , d ) ] )
416
- } )
432
+ findAndReplace ( tree , [
433
+ 'asd' ,
434
+ function ( /** @type {string } */ d ) {
435
+ return u ( 'emphasis' , [ u ( 'text' , d ) ] )
436
+ }
437
+ ] )
417
438
418
439
assert . deepEqual (
419
440
tree ,
@@ -425,7 +446,7 @@ test('findAndReplace', async function (t) {
425
446
await t . test ( 'security: replacer as string (safe)' , async function ( ) {
426
447
const tree = create ( )
427
448
428
- findAndReplace ( tree , 'and' , 'alert(1)' )
449
+ findAndReplace ( tree , [ 'and' , 'alert(1)' ] )
429
450
430
451
assert . deepEqual (
431
452
tree ,
0 commit comments