File tree Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ "use strict" ;
2
+
3
+ const logger = require ( './log' ) ;
4
+
5
+ module . exports = function ( template , prop , data ) {
6
+ let t = template ;
7
+
8
+ if ( typeof data === 'string' ) {
9
+ return t . replace ( `{{${ prop } }}` , data ) ;
10
+ }
11
+
12
+ if ( typeof data === 'boolean' ) {
13
+ const startRE = new RegExp ( `{{\\s?#[${ prop } ]+\\s?}}` ) ;
14
+ const endRE = new RegExp ( `{{\\s?/[${ prop } ]+\\s?}}` ) ;
15
+ if ( data ) {
16
+ t = t . replace ( startRE , '' ) ;
17
+ t = t . replace ( endRE , '' ) ;
18
+ } else {
19
+ const bIdx = t . search ( startRE ) ;
20
+ const eIdxStart = t . search ( endRE ) ;
21
+ const eIdxEnd = t . indexOf ( '}}' , eIdxStart ) + 2 ;
22
+ t = t . substring ( 0 , bIdx ) + t . substring ( eIdxEnd , t . length ) ;
23
+ }
24
+ return t ;
25
+ }
26
+
27
+ console . log ( `Could not replace ${ prop } with ${ data } inside ${ template } ` ) ;
28
+ logger . warning ( `Could not replace ${ prop } with ${ data } inside ${ template } ` ) ;
29
+ return t ;
30
+ } ;
Original file line number Diff line number Diff line change
1
+ "use strict" ;
2
+
3
+ const path = require ( 'path' ) ;
4
+ const util = require ( './util/test_utils.js' ) ;
5
+ const tap = require ( 'tap' ) ;
6
+
7
+ const replaceParameter = require ( '../core/lib/replaceParameter' ) ;
8
+
9
+ tap . test ( 'replaces simple value' , function ( test ) {
10
+ const result = replaceParameter ( '{{key}}' , 'key' , 'value' ) ;
11
+ test . equals ( result , 'value' ) ;
12
+ test . end ( ) ;
13
+ } ) ;
14
+
15
+ tap . test ( 'replaces boolean true' , function ( test ) {
16
+ const result = replaceParameter ( '1{{#key}}value{{/key}}2' , 'key' , true ) ;
17
+ test . equals ( result , '1value2' ) ;
18
+ test . end ( ) ;
19
+ } ) ;
20
+
21
+ tap . only ( 'replaces boolean true with spaces' , function ( test ) {
22
+ const result = replaceParameter ( '1{{ #key }}value{{ /key }}2' , 'key' , true ) ;
23
+ test . equals ( result , '1value2' ) ;
24
+ test . end ( ) ;
25
+ } ) ;
26
+
27
+ tap . test ( 'replaces boolean false' , function ( test ) {
28
+ const result = replaceParameter ( '1{{#key}}value{{/key}}2' , 'key' , false ) ;
29
+ test . equals ( result , '12' ) ;
30
+ test . end ( ) ;
31
+ } ) ;
You can’t perform that action at this time.
0 commit comments