-
Notifications
You must be signed in to change notification settings - Fork 3
/
v2.js
157 lines (118 loc) · 3.62 KB
/
v2.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
const BemCell = require('@bem/cell');
const BemEntityName = require('@bem/entity-name');
function belongsTo(child, entity) {
return entity && child.id.startsWith(entity.id) &&
child.block === entity.block &&
(!entity.elem || child.elem === entity.elem) &&
(!entity.mod || !entity.mod.name || child.mod.name === entity.mod.name) &&
(!entity.mod || !entity.mod.val || entity.mod.val === true || child.mod.val === entity.mod.val);
}
function isEqual(originalCell, newCell) {
if (originalCell.tech !== newCell.tech) {
return false;
}
if (!originalCell.entity.isEqual(newCell.entity)) {
return false;
}
return true;
}
function getItem(entity, tech) {
const item = {block: entity.block};
item.includeBlock = Object.keys(entity).length === 1;
tech && (item.tech = tech);
entity.elem && (item.elems = [entity.elem]);
entity.modName && (item.mods = {[entity.modName]: entity.modVal});
return item;
}
function getState(entity, tech) {
return {
tech,
block: entity.block,
elem: entity.elem,
mod: entity.modName,
val: entity.modVal
}
}
function beauty(item = {}, context) {
const declDep = {block: item.block};
// if only block name return block name
if (Object.keys(item).length === 1) {
return item.block;
}
// if this is block deps remove it self
if (item.block === context) {
delete declDep.block;
}
if (item.elems) {
const elemKey = item.includeBlock ? 'elems' : 'elem';
if (item.elems.length === 1) {
declDep[elemKey] = item.elems[0];
} else {
declDep[elemKey] = item.elems;
}
}
if (item.mods) {
if (Object.keys(item.mods).length === 1 && !item.includeBlock) {
const modName = Object.keys(item.mods)[0];
declDep.mod = modName;
declDep.val = item.mods[modName];
} else {
declDep.mods = item.mods;
}
}
if (item.tech) {
declDep.tech = item.tech;
}
return declDep;
}
function splitByScopes(cells) {
const acc = [];
while (cells.length) {
const cell = cells.shift();
const group = traverseCell(cell, cells);
// acc.push(group.map(item => item.id || item.map(item2 => item2.id)));
acc.push(group);
}
return acc;
}
function traverseCell(cell, list) {
let acc = [cell];
while (list.length) {
const nextCell = list[0];
if (cell.tech === nextCell.tech) {
if (isEqual(cell, nextCell)) {
acc.push(list.shift());
continue;
}
if (belongsTo(nextCell.entity, cell.entity)) {
// acc = [...acc, ...traverseCell(list.shift(), list)];
const traversed = traverseCell(list.shift(), list);
console.log('>>>>', cell.entity.id);
console.log('>>>>', JSON.stringify(traversed.map(item=>item.id),null, 2));
console.log('>>>>', nextCell.entity.id);
acc.push(traversed);
continue;
}
}
break;
}
return acc;
}
function traverseDecl(decl = []) {
if (!decl.length) {
return [];
}
const cells = decl.map(dep => new BemCell({entity: new BemEntityName(dep.entity), tech: dep.tech}));
const scopes = splitByScopes(cells);
return scopes;
}
/**
* Formats normalized declaration to v2
*
* @param {Array} decl Source declaration
* @return {Array}
*/
module.exports = function (decl = []) {
return traverseDecl(decl);
};