-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanupListOfValues.js
122 lines (91 loc) · 2.84 KB
/
cleanupListOfValues.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
exports.type = 'perItem';
exports.active = false;
exports.params = {
floatPrecision: 3,
leadingZero: true,
defaultPx: true
};
var regNumericValues = /^([\-+]?\d*\.?\d+([eE][\-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/,
regSeparator = /\s+,?\s*|,\s*/,
removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero;
/**
* Round list of values to the fixed precision.
*
* @example
* <svg viewBox="0 0 200.28423 200.28423" enable-background="new 0 0 200.28423 200.28423">
* ⬇
* <svg viewBox="0 0 200.284 200.284" enable-background="new 0 0 200.284 200.284">
*
*
* <polygon points="208.250977 77.1308594 223.069336 ... "/>
* ⬇
* <polygon points="208.251 77.131 223.069 ... "/>
*
*
* @param {Object} item current iteration item
* @param {Object} params plugin params
* @return {Boolean} if false, item will be filtered out
*
* @author kiyopikko
*/
exports.fn = function(item, params) {
if ( item.hasAttr('points') ) {
roundValues(item.attrs.points);
}
if ( item.hasAttr('enable-background') ) {
roundValues(item.attrs["enable-background"]);
}
if ( item.hasAttr('viewBox') ) {
roundValues(item.attrs.viewBox);
}
if ( item.hasAttr('stroke-dasharray') ) {
roundValues(item.attrs["stroke-dasharray"]);
}
if ( item.hasAttr('dx') ) {
roundValues(item.attrs.dx);
}
if ( item.hasAttr('dy') ) {
roundValues(item.attrs.dy);
}
if ( item.hasAttr('x') ) {
roundValues(item.attrs.x);
}
if ( item.hasAttr('y') ) {
roundValues(item.attrs.y);
}
function roundValues($prop){
var num, units,
match,
matchNew,
lists = $prop.value,
listsArr = lists.split(regSeparator),
roundedListArr = new Array(),
roundedList;
listsArr.forEach(function(elem){
match = elem.match(regNumericValues);
matchNew = elem.match(/new/);
// if attribute value matches regNumericValues
if(match){
// round it to the fixed precision
num = +(+match[1]).toFixed(params.floatPrecision),
units = match[3] || '';
// and remove leading zero
if (params.leadingZero) {
num = removeLeadingZero(num);
}
// remove default 'px' units
if (params.defaultPx && units === 'px') {
units = '';
}
roundedListArr.push(num+units);
}
// if attribute value is "new"(only enable-background).
else if(matchNew){
roundedListArr.push("new");
}
});
roundedList = roundedListArr.join(" ");
$prop.value = roundedList;
}
};