Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e6b441a

Browse files
authored
Convert cleanupListOfValues and cleanupNumericValues to visitor (svg#1521)
SLightly refactored
1 parent 7b6e730 commit e6b441a

File tree

2 files changed

+140
-151
lines changed

2 files changed

+140
-151
lines changed

plugins/cleanupListOfValues.js

Lines changed: 69 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,10 @@
33
const { removeLeadingZero } = require('../lib/svgo/tools.js');
44

55
exports.name = 'cleanupListOfValues';
6-
7-
exports.type = 'perItem';
8-
6+
exports.type = 'visitor';
97
exports.active = false;
10-
118
exports.description = 'rounds list of values to the fixed precision';
129

13-
exports.params = {
14-
floatPrecision: 3,
15-
leadingZero: true,
16-
defaultPx: true,
17-
convertToPx: true,
18-
};
19-
2010
const regNumericValues = /^([-+]?\d*\.?\d+([eE][-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/;
2111
const regSeparator = /\s+,?\s*|,\s*/;
2212
const absoluteLengths = {
@@ -42,88 +32,48 @@ const absoluteLengths = {
4232
* <polygon points="208.251 77.131 223.069 ... "/>
4333
*
4434
*
45-
* @param {Object} item current iteration item
46-
* @param {Object} params plugin params
47-
* @return {Boolean} if false, item will be filtered out
48-
*
4935
* @author kiyopikko
5036
*/
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(/new/);
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(/new/);
10351

10452
// if attribute value matches regNumericValues
10553
if (match) {
10654
// 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] || '';
10957

11058
// 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)
11462
);
11563

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+
}
11868
}
11969

12070
// and remove leading zero
121-
if (params.leadingZero) {
71+
if (leadingZero) {
12272
num = removeLeadingZero(num);
12373
}
12474

12575
// remove default 'px' units
126-
if (params.defaultPx && units === 'px') {
76+
if (defaultPx && units === 'px') {
12777
units = '';
12878
}
12979

@@ -138,5 +88,47 @@ exports.fn = function (item, params) {
13888
}
13989

14090
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+
};
142134
};

plugins/cleanupNumericValues.js

Lines changed: 71 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,91 @@
11
'use strict';
22

3-
exports.name = 'cleanupNumericValues';
4-
5-
exports.type = 'perItem';
3+
const { removeLeadingZero } = require('../lib/svgo/tools');
64

5+
exports.name = 'cleanupNumericValues';
6+
exports.type = 'visitor';
77
exports.active = true;
8-
98
exports.description =
109
'rounds numeric values to the fixed precision, removes default ‘px’ units';
1110

12-
exports.params = {
13-
floatPrecision: 3,
14-
leadingZero: true,
15-
defaultPx: true,
16-
convertToPx: true,
11+
const regNumericValues = /^([-+]?\d*\.?\d+([eE][-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/;
12+
const absoluteLengths = {
13+
// relative to px
14+
cm: 96 / 2.54,
15+
mm: 96 / 25.4,
16+
in: 96,
17+
pt: 4 / 3,
18+
pc: 16,
1719
};
1820

19-
var regNumericValues = /^([-+]?\d*\.?\d+([eE][-+]?\d+)?)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/,
20-
removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero,
21-
absoluteLengths = {
22-
// relative to px
23-
cm: 96 / 2.54,
24-
mm: 96 / 25.4,
25-
in: 96,
26-
pt: 4 / 3,
27-
pc: 16,
28-
};
29-
3021
/**
3122
* Round numeric values to the fixed precision,
3223
* remove default 'px' units.
3324
*
34-
* @param {Object} item current iteration item
35-
* @param {Object} params plugin params
36-
* @return {Boolean} if false, item will be filtered out
37-
*
3825
* @author Kir Belevich
3926
*/
40-
exports.fn = function (item, params) {
41-
if (item.type === 'element') {
42-
var floatPrecision = params.floatPrecision;
43-
44-
if (item.attributes.viewBox != null) {
45-
var nums = item.attributes.viewBox.split(/\s,?\s*|,\s*/g);
46-
item.attributes.viewBox = nums
47-
.map(function (value) {
48-
var num = +value;
49-
return isNaN(num) ? value : +num.toFixed(floatPrecision);
50-
})
51-
.join(' ');
52-
}
53-
54-
for (const [name, value] of Object.entries(item.attributes)) {
55-
// The `version` attribute is a text string and cannot be rounded
56-
if (name === 'version') {
57-
continue;
58-
}
59-
60-
var match = value.match(regNumericValues);
61-
62-
// if attribute value matches regNumericValues
63-
if (match) {
64-
// round it to the fixed precision
65-
var num = +(+match[1]).toFixed(floatPrecision),
66-
units = match[3] || '';
67-
68-
// convert absolute values to pixels
69-
if (params.convertToPx && units && units in absoluteLengths) {
70-
var pxNum = +(absoluteLengths[units] * match[1]).toFixed(
71-
floatPrecision
72-
);
73-
74-
if (String(pxNum).length < match[0].length) {
75-
num = pxNum;
76-
units = 'px';
77-
}
27+
exports.fn = (root, params) => {
28+
const {
29+
floatPrecision = 3,
30+
leadingZero = true,
31+
defaultPx = true,
32+
convertToPx = true,
33+
} = params;
34+
35+
return {
36+
element: {
37+
enter: (node) => {
38+
if (node.attributes.viewBox != null) {
39+
const nums = node.attributes.viewBox.split(/\s,?\s*|,\s*/g);
40+
node.attributes.viewBox = nums
41+
.map((value) => {
42+
const num = Number(value);
43+
return Number.isNaN(num)
44+
? value
45+
: Number(num.toFixed(floatPrecision));
46+
})
47+
.join(' ');
7848
}
7949

80-
// and remove leading zero
81-
if (params.leadingZero) {
82-
num = removeLeadingZero(num);
83-
}
50+
for (const [name, value] of Object.entries(node.attributes)) {
51+
// The `version` attribute is a text string and cannot be rounded
52+
if (name === 'version') {
53+
continue;
54+
}
8455

85-
// remove default 'px' units
86-
if (params.defaultPx && units === 'px') {
87-
units = '';
56+
const match = value.match(regNumericValues);
57+
58+
// if attribute value matches regNumericValues
59+
if (match) {
60+
// round it to the fixed precision
61+
let num = Number(Number(match[1]).toFixed(floatPrecision));
62+
let units = match[3] || '';
63+
64+
// convert absolute values to pixels
65+
if (convertToPx && units && units in absoluteLengths) {
66+
const pxNum = Number(
67+
(absoluteLengths[units] * match[1]).toFixed(floatPrecision)
68+
);
69+
if (pxNum.toString().length < match[0].length) {
70+
num = pxNum;
71+
units = 'px';
72+
}
73+
}
74+
75+
// and remove leading zero
76+
if (leadingZero) {
77+
num = removeLeadingZero(num);
78+
}
79+
80+
// remove default 'px' units
81+
if (defaultPx && units === 'px') {
82+
units = '';
83+
}
84+
85+
node.attributes[name] = num + units;
86+
}
8887
}
89-
90-
item.attributes[name] = num + units;
91-
}
92-
}
93-
}
88+
},
89+
},
90+
};
9491
};

0 commit comments

Comments
 (0)