forked from svg/svgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortAttrs.js
56 lines (43 loc) · 1.03 KB
/
sortAttrs.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
'use strict';
exports.type = 'perItem';
exports.active = false;
exports.description = 'sorts element attributes (disabled by default)';
exports.params = {
order: [
'xmlns',
'id',
'width', 'height',
'x', 'x1', 'x2',
'y', 'y1', 'y2',
'cx', 'cy', 'r',
'fill', 'fill-opacity', 'fill-rule',
'stroke', 'stroke-opacity', 'stroke-width', 'stroke-miterlimit', 'stroke-dashoffset',
'd', 'points'
]
};
/**
* Sort element attributes for epic readability.
*
* @param {Object} item current iteration item
* @param {Object} params plugin params
*
* @author Nikolay Frantsev
*/
exports.fn = function(item, params) {
var attrs = [],
sorted = {},
orderlen = params.order.length + 1;
if (item.elem) {
item.eachAttr(function(attr) {
attrs.push(attr);
});
attrs.sort(function(a, b) {
return ((a = params.order.indexOf(a.name)) > -1 ? a : orderlen) -
((b = params.order.indexOf(b.name)) > -1 ? b : orderlen);
});
attrs.forEach(function (attr) {
sorted[attr.name] = attr;
});
item.attrs = sorted;
}
};