-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsorting.js
91 lines (77 loc) · 2.67 KB
/
sorting.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
const postcss = require('postcss');
const _ = require('lodash');
module.exports = {
sortDeclarations,
sortDeclarationsAlphabetically,
sortByIndexes,
};
function sortDeclarations(a, b) {
// If unprefixed prop names are the same, compare the prefixed versions
if (a.node.type === 'decl' && b.node.type === 'decl' && a.unprefixedName === b.unprefixedName) {
// If first property has no prefix and second property has prefix
if (!postcss.vendor.prefix(a.name).length && postcss.vendor.prefix(b.name).length) {
return 1;
}
}
if (!_.isUndefined(a.orderData) && !_.isUndefined(b.orderData)) {
// If a and b have the same group index, and a's property index is
// higher than b's property index, in a sorted list a appears after
// b:
if (a.orderData.propertyIndex !== b.orderData.propertyIndex) {
return a.orderData.propertyIndex - b.orderData.propertyIndex;
}
}
if (
a.unspecifiedPropertiesPosition === 'bottom' ||
a.unspecifiedPropertiesPosition === 'bottomAlphabetical' ||
b.unspecifiedPropertiesPosition === 'bottomAlphabetical'
) {
if (!_.isUndefined(a.orderData) && _.isUndefined(b.orderData)) {
return -1;
}
if (_.isUndefined(a.orderData) && !_.isUndefined(b.orderData)) {
return 1;
}
}
if (a.unspecifiedPropertiesPosition === 'top') {
if (!_.isUndefined(a.orderData) && _.isUndefined(b.orderData)) {
return 1;
}
if (_.isUndefined(a.orderData) && !_.isUndefined(b.orderData)) {
return -1;
}
}
if (a.unspecifiedPropertiesPosition === 'bottomAlphabetical') {
if (_.isUndefined(a.orderData) && _.isUndefined(b.orderData)) {
return sortDeclarationsAlphabetically(a, b);
}
}
// If a and b have the same group index and the same property index,
// in a sorted list they appear in the same order they were in
// original array:
return a.initialIndex - b.initialIndex;
}
function sortDeclarationsAlphabetically(a, b) {
if (a.unprefixedName === b.unprefixedName) {
if (a.node.type === 'decl' && b.node.type === 'decl') {
// If first property has no prefix and second property has prefix
if (!postcss.vendor.prefix(a.name).length && postcss.vendor.prefix(b.name).length) {
return 1;
}
}
return a.initialIndex - b.initialIndex;
}
return a.unprefixedName <= b.unprefixedName ? -1 : 1;
}
function sortByIndexes(a, b) {
// If a and b have the same group index, and a's property index is
// higher than b's property index, in a sorted list a appears after
// b:
if (a.position !== b.position) {
return a.position - b.position;
}
// If a and b have the same group index and the same property index,
// in a sorted list they appear in the same order they were in
// original array:
return a.initialIndex - b.initialIndex;
}