-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
167 lines (131 loc) · 3.52 KB
/
index.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
158
159
160
161
162
163
164
165
166
167
/*
{
block : 'b1',
elem : 'e1',
mod : 'm1'
val : 'v1'
tech : 't1'
}
*/
var toString = Object.prototype.toString,
isArray = Array.isArray,
isObject = function(o) {
return toString.call(o) === '[object Object]';
};
module.exports = function normalizeDeps(deps) {
if(isArray(deps)) return deps.map(normalize);
return normalize(deps);
};
function normalize(deps) {
if(!isObject(deps)) {
throw new Error('invalid declaration ' + deps);
}
var result = [],
cache = {};
if(deps.blocks) {
var blocks = deps.blocks;
if(!isArray(blocks)) blocks = [blocks];
pushBlocks(blocks);
}
var block = deps.block,
elem = deps.elem;
walk(deps.elems, pushElems);
// XXX: workaround for bem/bem-tool#401
if(isArray(deps.elem)) {
walk(deps.elem, function(block, elem, elems) {
pushElems(block, null, elems, true);
});
}
walk(deps.mods, pushMods);
if(!result.length) {
push(extendDecl({}, deps));
}
return result;
function walk(items, cb) {
if(!items) return;
if(!isArray(items)) items = [items];
var i = 0,
slen = result.length,
decl;
do {
decl = result[i] || {};
if(decl.block) block = decl.block;
if(decl.elem) elem = decl.elem;
cb(block, elem, items);
} while(++i < slen);
}
function push(decl) {
if(isArray(decl)) {
decl.forEach(push);
return;
}
var key = identify(decl);
if(cache[key]) return;
cache[key] = true;
result.push(decl);
}
function pushOne(block, elem) {
if(block) {
var d = {
block : block
};
if(elem) d.elem = elem;
push(d);
}
}
function pushBlocks(blocks) {
blocks.reduce(function(decls, block) {
pushOne(block);
push(extendDecl({ block : block }, deps));
return decls;
}, []);
}
function pushElems(block, _, elems, single) {
single || pushOne(block);
elems.forEach(function(elem) {
var d = {};
if(block) d.block = block;
d.elem = elem;
push(extendDecl(d, deps));
});
}
function pushMods(block, elem, mods) {
pushOne(block, elem);
mods.forEach(function(mod) {
Object.keys(mod).forEach(function(name) {
var d = { mod : name };
if(block) {
d.block = block;
if(elem) d.elem = elem;
}
push(extendDecl({}, d));
var vals = mod[name];
if(!vals) {
return;
}
if(!isArray(vals)) vals = [vals];
vals.forEach(function(val) {
push(extendDecl({ val : val }, d));
});
});
});
}
}
function extendDecl(decl, src) {
extend('tech', decl, src);
extend('block', decl, src);
extend('elem', decl, src);
extend('mod', decl, src);
extend('val', decl, src);
return decl;
}
function extend(type, decl, src) {
if(decl[type]) return decl;
if(src[type]) decl[type] = src[type];
return decl;
}
function identify(decl) {
return decl.block +
(decl.elem? '__' + decl.elem : '') +
(decl.mod? '_' + decl.mod + (decl.val? '_' + decl.val : '') : '');
}