3
3
const { removeLeadingZero } = require ( '../lib/svgo/tools.js' ) ;
4
4
5
5
exports . name = 'cleanupListOfValues' ;
6
-
7
- exports . type = 'perItem' ;
8
-
6
+ exports . type = 'visitor' ;
9
7
exports . active = false ;
10
-
11
8
exports . description = 'rounds list of values to the fixed precision' ;
12
9
13
- exports . params = {
14
- floatPrecision : 3 ,
15
- leadingZero : true ,
16
- defaultPx : true ,
17
- convertToPx : true ,
18
- } ;
19
-
20
10
const regNumericValues = / ^ ( [ - + ] ? \d * \. ? \d + ( [ e E ] [ - + ] ? \d + ) ? ) ( p x | p t | p c | m m | c m | m | i n | f t | e m | e x | % ) ? $ / ;
21
11
const regSeparator = / \s + , ? \s * | , \s * / ;
22
12
const absoluteLengths = {
@@ -42,88 +32,48 @@ const absoluteLengths = {
42
32
* <polygon points="208.251 77.131 223.069 ... "/>
43
33
*
44
34
*
45
- * @param {Object } item current iteration item
46
- * @param {Object } params plugin params
47
- * @return {Boolean } if false, item will be filtered out
48
- *
49
35
* @author kiyopikko
50
36
*/
51
- exports . fn = function ( item , params ) {
52
- if ( item . type !== 'element' ) {
53
- return ;
54
- }
55
-
56
- if ( item . attributes . points != null ) {
57
- item . attributes . points = roundValues ( item . attributes . points ) ;
58
- }
59
-
60
- if ( item . attributes [ 'enable-background' ] != null ) {
61
- item . attributes [ 'enable-background' ] = roundValues (
62
- item . attributes [ 'enable-background' ]
63
- ) ;
64
- }
65
-
66
- if ( item . attributes . viewBox != null ) {
67
- item . attributes . viewBox = roundValues ( item . attributes . viewBox ) ;
68
- }
69
-
70
- if ( item . attributes [ 'stroke-dasharray' ] != null ) {
71
- item . attributes [ 'stroke-dasharray' ] = roundValues (
72
- item . attributes [ 'stroke-dasharray' ]
73
- ) ;
74
- }
75
-
76
- if ( item . attributes . dx != null ) {
77
- item . attributes . dx = roundValues ( item . attributes . dx ) ;
78
- }
79
-
80
- if ( item . attributes . dy != null ) {
81
- item . attributes . dy = roundValues ( item . attributes . dy ) ;
82
- }
83
-
84
- if ( item . attributes . x != null ) {
85
- item . attributes . x = roundValues ( item . attributes . x ) ;
86
- }
87
-
88
- if ( item . attributes . y != null ) {
89
- item . attributes . y = roundValues ( item . attributes . y ) ;
90
- }
91
-
92
- function roundValues ( lists ) {
93
- var num ,
94
- units ,
95
- match ,
96
- matchNew ,
97
- listsArr = lists . split ( regSeparator ) ,
98
- roundedList = [ ] ;
99
-
100
- for ( const elem of listsArr ) {
101
- match = elem . match ( regNumericValues ) ;
102
- matchNew = elem . match ( / n e w / ) ;
37
+ exports . fn = ( root , params ) => {
38
+ const {
39
+ floatPrecision = 3 ,
40
+ leadingZero = true ,
41
+ defaultPx = true ,
42
+ convertToPx = true ,
43
+ } = params ;
44
+
45
+ const roundValues = ( lists ) => {
46
+ const roundedList = [ ] ;
47
+
48
+ for ( const elem of lists . split ( regSeparator ) ) {
49
+ const match = elem . match ( regNumericValues ) ;
50
+ const matchNew = elem . match ( / n e w / ) ;
103
51
104
52
// if attribute value matches regNumericValues
105
53
if ( match ) {
106
54
// round it to the fixed precision
107
- ( num = + ( + match [ 1 ] ) . toFixed ( params . floatPrecision ) ) ,
108
- ( units = match [ 3 ] || '' ) ;
55
+ let num = Number ( Number ( match [ 1 ] ) . toFixed ( floatPrecision ) ) ;
56
+ let units = match [ 3 ] || '' ;
109
57
110
58
// convert absolute values to pixels
111
- if ( params . convertToPx && units && units in absoluteLengths ) {
112
- var pxNum = + ( absoluteLengths [ units ] * match [ 1 ] ) . toFixed (
113
- params . floatPrecision
59
+ if ( convertToPx && units && units in absoluteLengths ) {
60
+ const pxNum = Number (
61
+ ( absoluteLengths [ units ] * match [ 1 ] ) . toFixed ( floatPrecision )
114
62
) ;
115
63
116
- if ( String ( pxNum ) . length < match [ 0 ] . length )
117
- ( num = pxNum ) , ( units = 'px' ) ;
64
+ if ( pxNum . toString ( ) . length < match [ 0 ] . length ) {
65
+ num = pxNum ;
66
+ units = 'px' ;
67
+ }
118
68
}
119
69
120
70
// and remove leading zero
121
- if ( params . leadingZero ) {
71
+ if ( leadingZero ) {
122
72
num = removeLeadingZero ( num ) ;
123
73
}
124
74
125
75
// remove default 'px' units
126
- if ( params . defaultPx && units === 'px' ) {
76
+ if ( defaultPx && units === 'px' ) {
127
77
units = '' ;
128
78
}
129
79
@@ -138,5 +88,47 @@ exports.fn = function (item, params) {
138
88
}
139
89
140
90
return roundedList . join ( ' ' ) ;
141
- }
91
+ } ;
92
+
93
+ return {
94
+ element : {
95
+ enter : ( node ) => {
96
+ if ( node . attributes . points != null ) {
97
+ node . attributes . points = roundValues ( node . attributes . points ) ;
98
+ }
99
+
100
+ if ( node . attributes [ 'enable-background' ] != null ) {
101
+ node . attributes [ 'enable-background' ] = roundValues (
102
+ node . attributes [ 'enable-background' ]
103
+ ) ;
104
+ }
105
+
106
+ if ( node . attributes . viewBox != null ) {
107
+ node . attributes . viewBox = roundValues ( node . attributes . viewBox ) ;
108
+ }
109
+
110
+ if ( node . attributes [ 'stroke-dasharray' ] != null ) {
111
+ node . attributes [ 'stroke-dasharray' ] = roundValues (
112
+ node . attributes [ 'stroke-dasharray' ]
113
+ ) ;
114
+ }
115
+
116
+ if ( node . attributes . dx != null ) {
117
+ node . attributes . dx = roundValues ( node . attributes . dx ) ;
118
+ }
119
+
120
+ if ( node . attributes . dy != null ) {
121
+ node . attributes . dy = roundValues ( node . attributes . dy ) ;
122
+ }
123
+
124
+ if ( node . attributes . x != null ) {
125
+ node . attributes . x = roundValues ( node . attributes . x ) ;
126
+ }
127
+
128
+ if ( node . attributes . y != null ) {
129
+ node . attributes . y = roundValues ( node . attributes . y ) ;
130
+ }
131
+ } ,
132
+ } ,
133
+ } ;
142
134
} ;
0 commit comments