@@ -82,6 +82,25 @@ describe('Replace in file', () => {
82
82
} ) ;
83
83
} ) ;
84
84
85
+ it ( 'should pass the match as first arg and file as last arg to a replacer function replace contents in a single file with regex' , done => {
86
+ replace ( {
87
+ files : 'test1' ,
88
+ from : / r e \s p l a c e / g,
89
+ to : ( match , ...args ) => {
90
+ const file = args . pop ( ) ;
91
+ expect ( match ) . to . equal ( 're place' ) ;
92
+ expect ( file ) . to . equal ( 'test1' ) ;
93
+ return 'b' ;
94
+ }
95
+ } ) . then ( ( ) => {
96
+ const test1 = fs . readFileSync ( 'test1' , 'utf8' ) ;
97
+ const test2 = fs . readFileSync ( 'test2' , 'utf8' ) ;
98
+ expect ( test1 ) . to . equal ( 'a b c' ) ;
99
+ expect ( test2 ) . to . equal ( testData ) ;
100
+ done ( ) ;
101
+ } ) ;
102
+ } ) ;
103
+
85
104
it ( 'should replace contents with a string replacement' , done => {
86
105
replace ( {
87
106
files : 'test1' ,
@@ -94,6 +113,23 @@ describe('Replace in file', () => {
94
113
} ) ;
95
114
} ) ;
96
115
116
+ it ( 'should pass the match as first arg and file as last arg to a replacer function and replace contents with a string replacement' , done => {
117
+ replace ( {
118
+ files : 'test1' ,
119
+ from : 're place' ,
120
+ to : ( match , ...args ) => {
121
+ const file = args . pop ( ) ;
122
+ expect ( match ) . to . equal ( 're place' ) ;
123
+ expect ( file ) . to . equal ( 'test1' ) ;
124
+ return 'b' ;
125
+ }
126
+ } ) . then ( ( ) => {
127
+ const test1 = fs . readFileSync ( 'test1' , 'utf8' ) ;
128
+ expect ( test1 ) . to . equal ( 'a b c' ) ;
129
+ done ( ) ;
130
+ } ) ;
131
+ } ) ;
132
+
97
133
it ( 'should replace contents in a an array of files' , done => {
98
134
replace ( {
99
135
files : [ 'test1' , 'test2' ] ,
@@ -327,6 +363,25 @@ describe('Replace in file', () => {
327
363
} ) ;
328
364
} ) ;
329
365
366
+ it ( 'should pass the match as first arg and file as last arg to a replacer function replace contents in a single file with regex' , done => {
367
+ replace ( {
368
+ files : 'test1' ,
369
+ from : / r e \s p l a c e / g,
370
+ to : ( match , ...args ) => {
371
+ const file = args . pop ( ) ;
372
+ expect ( match ) . to . equal ( 're place' ) ;
373
+ expect ( file ) . to . equal ( 'test1' ) ;
374
+ return 'b' ;
375
+ }
376
+ } , ( ) => {
377
+ const test1 = fs . readFileSync ( 'test1' , 'utf8' ) ;
378
+ const test2 = fs . readFileSync ( 'test2' , 'utf8' ) ;
379
+ expect ( test1 ) . to . equal ( 'a b c' ) ;
380
+ expect ( test2 ) . to . equal ( testData ) ;
381
+ done ( ) ;
382
+ } ) ;
383
+ } ) ;
384
+
330
385
it ( 'should replace contents with a string replacement' , done => {
331
386
replace ( {
332
387
files : 'test1' ,
@@ -339,6 +394,23 @@ describe('Replace in file', () => {
339
394
} ) ;
340
395
} ) ;
341
396
397
+ it ( 'should pass the match as first arg and file as last arg to a replacer function and replace contents with a string replacement' , done => {
398
+ replace ( {
399
+ files : 'test1' ,
400
+ from : 're place' ,
401
+ to : ( match , ...args ) => {
402
+ const file = args . pop ( ) ;
403
+ expect ( match ) . to . equal ( 're place' ) ;
404
+ expect ( file ) . to . equal ( 'test1' ) ;
405
+ return 'b' ;
406
+ }
407
+ } , ( ) => {
408
+ const test1 = fs . readFileSync ( 'test1' , 'utf8' ) ;
409
+ expect ( test1 ) . to . equal ( 'a b c' ) ;
410
+ done ( ) ;
411
+ } ) ;
412
+ } ) ;
413
+
342
414
it ( 'should replace contents in a an array of files' , done => {
343
415
replace ( {
344
416
files : [ 'test1' , 'test2' ] ,
@@ -607,6 +679,23 @@ describe('Replace in file', () => {
607
679
expect ( test2 ) . to . equal ( testData ) ;
608
680
} ) ;
609
681
682
+ it ( 'should pass the match as first arg and file as last arg to a replacer function replace contents in a single file with regex' , function ( ) {
683
+ replace . sync ( {
684
+ files : 'test1' ,
685
+ from : / r e \s p l a c e / g,
686
+ to : ( match , ...args ) => {
687
+ const file = args . pop ( ) ;
688
+ expect ( match ) . to . equal ( 're place' ) ;
689
+ expect ( file ) . to . equal ( 'test1' ) ;
690
+ return 'b' ;
691
+ }
692
+ } ) ;
693
+ const test1 = fs . readFileSync ( 'test1' , 'utf8' ) ;
694
+ const test2 = fs . readFileSync ( 'test2' , 'utf8' ) ;
695
+ expect ( test1 ) . to . equal ( 'a b c' ) ;
696
+ expect ( test2 ) . to . equal ( testData ) ;
697
+ } ) ;
698
+
610
699
it ( 'should replace contents with a string replacement' , function ( ) {
611
700
replace . sync ( {
612
701
files : 'test1' ,
@@ -617,6 +706,21 @@ describe('Replace in file', () => {
617
706
expect ( test1 ) . to . equal ( 'a b c' ) ;
618
707
} ) ;
619
708
709
+ it ( 'should pass the match as first arg and file as last arg to a replacer function and replace contents with a string replacement' , function ( ) {
710
+ replace . sync ( {
711
+ files : 'test1' ,
712
+ from : 're place' ,
713
+ to : ( match , ...args ) => {
714
+ const file = args . pop ( ) ;
715
+ expect ( match ) . to . equal ( 're place' ) ;
716
+ expect ( file ) . to . equal ( 'test1' ) ;
717
+ return 'b' ;
718
+ }
719
+ } ) ;
720
+ const test1 = fs . readFileSync ( 'test1' , 'utf8' ) ;
721
+ expect ( test1 ) . to . equal ( 'a b c' ) ;
722
+ } ) ;
723
+
620
724
it ( 'should replace contents in a an array of files' , function ( ) {
621
725
replace . sync ( {
622
726
files : [ 'test1' , 'test2' ] ,
0 commit comments