@@ -82,6 +82,25 @@ describe('Replace in file', () => {
8282 } ) ;
8383 } ) ;
8484
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+
85104 it ( 'should replace contents with a string replacement' , done => {
86105 replace ( {
87106 files : 'test1' ,
@@ -94,6 +113,23 @@ describe('Replace in file', () => {
94113 } ) ;
95114 } ) ;
96115
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+
97133 it ( 'should replace contents in a an array of files' , done => {
98134 replace ( {
99135 files : [ 'test1' , 'test2' ] ,
@@ -327,6 +363,25 @@ describe('Replace in file', () => {
327363 } ) ;
328364 } ) ;
329365
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+
330385 it ( 'should replace contents with a string replacement' , done => {
331386 replace ( {
332387 files : 'test1' ,
@@ -339,6 +394,23 @@ describe('Replace in file', () => {
339394 } ) ;
340395 } ) ;
341396
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+
342414 it ( 'should replace contents in a an array of files' , done => {
343415 replace ( {
344416 files : [ 'test1' , 'test2' ] ,
@@ -607,6 +679,23 @@ describe('Replace in file', () => {
607679 expect ( test2 ) . to . equal ( testData ) ;
608680 } ) ;
609681
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+
610699 it ( 'should replace contents with a string replacement' , function ( ) {
611700 replace . sync ( {
612701 files : 'test1' ,
@@ -617,6 +706,21 @@ describe('Replace in file', () => {
617706 expect ( test1 ) . to . equal ( 'a b c' ) ;
618707 } ) ;
619708
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+
620724 it ( 'should replace contents in a an array of files' , function ( ) {
621725 replace . sync ( {
622726 files : [ 'test1' , 'test2' ] ,
0 commit comments