@@ -12,55 +12,60 @@ const cleanClone = require('./cleanClone');
12
12
*/
13
13
module . exports = function tidyFunction ( declaration , tidy ) {
14
14
const FUNCTION_REGEX = / t i d y - ( s p a n | o f f s e t ) ( | - f u l l ) \( ( [ \d . - ] + ) \) / ;
15
+ const globalRegExp = new RegExp ( FUNCTION_REGEX , 'g' ) ;
16
+ const localRegExp = new RegExp ( FUNCTION_REGEX ) ;
15
17
16
- if ( FUNCTION_REGEX . test ( declaration . value ) ) {
18
+ if ( localRegExp . test ( declaration . value ) ) {
17
19
const { grid } = tidy ;
18
- /**
19
- * match: The full function expression.
20
- * slug: One of either `span` or `offset`.
21
- * modifier: One of either `undefined` or `-full`.
22
- * value: The function's argument.
23
- */
24
- const [ match , slug , modifier , value ] = declaration . value . match ( FUNCTION_REGEX ) ;
20
+ const fullMatch = declaration . value . match ( globalRegExp ) ;
25
21
26
22
/**
27
- * Get the span or offset `calc()` value(s) .
23
+ * Find all matches in the declaration value.
28
24
*
29
- * fluid: calc() function based on 100vw base.
30
- * full: calc() function based on `siteMax` base.
25
+ * @param {String } acc The accumulator, based on declaration.value
26
+ * @param {String } tidyMatch The full tidy function match(es)
27
+ *
28
+ * @return {String } The replacement value for the declaration
31
29
*/
32
- let { fluid, full } = ( 'span' === slug ) ?
33
- grid . spanCalc ( value ) :
34
- grid . offsetCalc ( value ) ;
30
+ const replaceWithValue = fullMatch . reduce ( ( acc , tidyMatch ) => {
31
+ /**
32
+ * match: The full function expression.
33
+ * slug: One of either `span` or `offset`.
34
+ * modifier: One of either `undefined` or `-full`.
35
+ * value: The function's argument.
36
+ */
37
+ const [ match , slug , modifier , value ] = tidyMatch . match ( localRegExp ) ;
35
38
36
- /**
37
- * If the tidy- function is nested in a calc() function, remove 'calc'
38
- * from the span/offset values.
39
- */
40
- if ( / ^ c a l c \( .* \) $ / . test ( declaration . value ) ) {
41
- [ fluid , full ] = [ fluid , full ] . map ( calc => (
42
- ( undefined !== calc ) ? calc . replace ( 'calc' , '' ) : calc ) ) ;
43
- }
39
+ /**
40
+ * Get the span or offset `calc()` value(s).
41
+ *
42
+ * fluid: calc() function based on 100vw base.
43
+ * full: calc() function based on `siteMax` base.
44
+ */
45
+ let { fluid, full } = ( 'span' === slug ) ?
46
+ grid . spanCalc ( value ) :
47
+ grid . offsetCalc ( value ) ;
48
+
49
+ acc = ( '-full' === modifier ) ?
50
+ // tidy-[span|offset]-full()
51
+ acc . replace ( match , full ) :
52
+ // tidy-[span|offset] ()
53
+ acc . replace ( match , fluid ) ;
54
+
55
+ /**
56
+ * Remove nested calc() function resulting from the tidy-* function replacement.
57
+ */
58
+ const NESTED_CALC_REGEX = / ( c a l c [ ( \s ] + ) ( c a l c \( ) / ;
59
+ return ( NESTED_CALC_REGEX . test ( acc ) ) ? acc . replace ( NESTED_CALC_REGEX , '$1(' ) : acc ;
60
+ } , declaration . value ) ;
44
61
45
62
// Replace declaration(s) with cloned and updated declarations.
46
- if ( '-full' === modifier ) {
47
- // tidy-[span|offset]-full()
48
- declaration . replaceWith ( cleanClone (
49
- declaration ,
50
- {
51
- prop : declaration . prop ,
52
- value : declaration . value . replace ( match , full ) ,
53
- } ,
54
- ) ) ;
55
- } else {
56
- // tidy-[span|offset] ()
57
- declaration . replaceWith ( cleanClone (
58
- declaration ,
59
- {
60
- prop : declaration . prop ,
61
- value : declaration . value . replace ( match , fluid ) ,
62
- } ,
63
- ) ) ;
64
- }
63
+ declaration . replaceWith ( cleanClone (
64
+ declaration ,
65
+ {
66
+ prop : declaration . prop ,
67
+ value : replaceWithValue ,
68
+ } ,
69
+ ) ) ;
65
70
}
66
71
} ;
0 commit comments