@@ -5,44 +5,48 @@ function escape(str) {
5
5
return str . replace ( / [ - [ \] / { } ( ) * + ? . \\ ^ $ | ] / g, '\\$&' ) ;
6
6
}
7
7
8
- function functor ( thing ) {
9
- if ( typeof thing === 'function' ) return thing ;
10
- return ( ) => thing ;
8
+ function ensureFunction ( functionOrValue ) {
9
+ if ( typeof functionOrValue === 'function' ) return functionOrValue ;
10
+ return ( ) => functionOrValue ;
11
11
}
12
12
13
13
function longest ( a , b ) {
14
14
return b . length - a . length ;
15
15
}
16
16
17
- export default function replace ( options = { } ) {
18
- const filter = createFilter ( options . include , options . exclude ) ;
19
- const { delimiters } = options ;
20
-
21
- let values ;
22
-
17
+ function getReplacements ( options ) {
23
18
if ( options . values ) {
24
- values = Object . assign ( { } , options . values ) ;
19
+ return Object . assign ( { } , options . values ) ;
25
20
} else {
26
- values = Object . assign ( { } , options ) ;
21
+ const values = Object . assign ( { } , options ) ;
27
22
delete values . delimiters ;
28
23
delete values . include ;
29
24
delete values . exclude ;
25
+ delete values . sourcemap ;
26
+ delete values . sourceMap ;
27
+ return values ;
30
28
}
29
+ }
30
+
31
+ function mapToFunctions ( object ) {
32
+ return Object . keys ( object ) . reduce ( ( functions , key ) => {
33
+ functions [ key ] = ensureFunction ( object [ key ] ) ;
34
+ return functions ;
35
+ } , { } ) ;
36
+ }
31
37
32
- const keys = Object . keys ( values )
38
+ export default function replace ( options = { } ) {
39
+ const filter = createFilter ( options . include , options . exclude ) ;
40
+ const { delimiters } = options ;
41
+ const functionValues = mapToFunctions ( getReplacements ( options ) ) ;
42
+ const keys = Object . keys ( functionValues )
33
43
. sort ( longest )
34
44
. map ( escape ) ;
35
45
36
46
const pattern = delimiters
37
47
? new RegExp ( `${ escape ( delimiters [ 0 ] ) } (${ keys . join ( '|' ) } )${ escape ( delimiters [ 1 ] ) } ` , 'g' )
38
48
: new RegExp ( `\\b(${ keys . join ( '|' ) } )\\b` , 'g' ) ;
39
49
40
- // convert all values to functions
41
- const functionValues = Object . keys ( values ) . reduce ( ( acc , key ) => {
42
- acc [ key ] = functor ( values [ key ] ) ;
43
- return acc ;
44
- } , { } ) ;
45
-
46
50
return {
47
51
name : 'replace' ,
48
52
0 commit comments