-
Notifications
You must be signed in to change notification settings - Fork 61
/
createExpectedOrder.js
45 lines (35 loc) · 938 Bytes
/
createExpectedOrder.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
const _ = require('lodash');
module.exports = function createExpectedOrder(input) {
const order = {};
let expectedPosition = 0;
let separatedGroup = 1;
appendGroup(input);
function appendGroup(items) {
items.forEach(item => appendItem(item, false));
}
function appendItem(item, inFlexibleGroup) {
if (_.isString(item)) {
// In flexible groups, the expectedPosition does not ascend
// to make that flexibility work;
// otherwise, it will always ascend
if (!inFlexibleGroup) {
expectedPosition += 1;
}
order[item] = { separatedGroup, expectedPosition };
return;
}
// If item is not a string, it's a group...
if (item.emptyLineBefore) {
separatedGroup += 1;
}
if (item.order && item.order === 'flexible') {
expectedPosition += 1;
item.properties.forEach(property => {
appendItem(property, true);
});
} else {
appendGroup(item.properties);
}
}
return order;
};