-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
321 lines (291 loc) · 10.5 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
'use strict';
const assert = require('assert');
const util = require('util');
const deprecate = require('depd')(require('./package.json').name);
const BemEntityName = require('@bem/entity-name');
module.exports = class BemCell {
/**
* @param {Object} obj — representation of cell.
* @param {BemEntityName} obj.entity — representation of entity name.
* @param {String} [obj.tech] - tech of cell.
* @param {String} [obj.layer] - layer of cell.
*/
constructor(obj) {
assert(obj && obj.entity, 'Required `entity` field');
assert(BemEntityName.isBemEntityName(obj.entity), 'The `entity` field should be an instance of BemEntityName');
this._entity = obj.entity;
this._layer = obj.layer;
this._tech = obj.tech;
}
/**
* Returns the name of entity.
*
* @example
* const BemCell = require('@bem/cell');
* const BemEntityName = require('@bem/entity-name');
*
* const cell = new BemCell({
* entity: new BemEntityName({ block: 'button', elem: 'text' })
* });
*
* cell.entity; // ➜ BemEntityName { block: 'button', elem: 'text' }
*
* @returns {BemEntityName} name of entity.
*/
get entity() { return this._entity; }
/**
* Returns the tech of cell.
*
* @example
* const BemCell = require('@bem/cell');
* const BemEntityName = require('@bem/entity-name');
*
* const cell = new BemCell({
* entity: new BemEntityName({ block: 'button', elem: 'text' }),
* tech: 'css'
* });
*
* cell.tech; // ➜ css
*
* @returns {String} tech of cell.
*/
get tech() { return this._tech; }
/**
* Returns the layer of this cell.
*
* @example
* const BemCell = require('@bem/cell');
* const BemEntityName = require('@bem/entity-name');
*
* const cell = new BemCell({
* entity: new BemEntityName({ block: 'button', elem: 'text' }),
* layer: 'desktop'
* });
*
* cell.layer; // ➜ desktop
*
* @returns {String} layer of cell.
*/
get layer() { return this._layer; }
/**
* Proxies `block` field from entity.
*
* @returns {String}
*/
get block() { return this._entity.block; }
/**
* Proxies `elem` field from entity.
*
* @returns {String|undefined}
*/
get elem() { return this._entity.elem; }
/**
* Proxies `mod` field from entity.
*
* @returns {Object|undefined} - field with `name` and `val`
*/
get mod() { return this._entity.mod; }
/**
* Proxies `modVal` field from entity.
*
* @deprecated - just for compatibility. Use {@link BemCell#mod.name}
* @returns {String|undefined} - modifier name
*/
get modName() {
deprecate('modName: just for compatibility and can be dropped in future. Instead use \'mod.name\'');
return this._entity.modName;
}
/**
* Proxies `modVal` field from entity.
*
* @deprecated - just for compatibility. Use {@link BemCell#mod.val}
* @returns {String|true|undefined} - modifier value
*/
get modVal() {
deprecate('modVal: just for compatibility and can be dropped in future. Instead use \'mod.val\'');
return this._entity.modVal;
}
/**
* Returns the identifier of this cell.
*
* Important: should only be used to determine uniqueness of cell.
*
* @example
* const BemCell = require('@bem/cell');
* const BemEntityName = require('@bem/entity-name');
*
* const cell = new BemCell({
* entity: new BemEntityName({ block: 'button', elem: 'text' }),
* tech: 'css',
* layer: 'desktop'
* });
*
* cell.id; // ➜ "button__text@desktop.css"
*
* @returns {String} identifier of cell.
*/
get id() {
if (this._id) {
return this._id;
}
const layer = this._layer ? `@${this._layer}` : '';
const tech = this._tech ? `.${this._tech}` : '';
this._id = `${this._entity}${layer}${tech}`;
return this._id;
}
/**
* Returns string representing the bem cell.
*
* Important: If you want to get string representation in accordance with the provisions naming convention
* you should use `bem-naming` package.
*
* @example
* const BemCell = require('@bem/cell');
* const BemEntityName§ = require('@bem/entity-name');
* const cell = new BemCell({ entity: new BemEntityName({ block: 'button', mod: 'focused' }),
* tech: 'css', layer: 'desktop' });
*
* cell.toString(); // button_focused@desktop.css
*
* @returns {String}
*/
toString() { return this.id; }
/**
* Returns object representing the bem cell. Is needed for debug in Node.js.
*
* In some browsers `console.log()` calls `valueOf()` on each argument.
* This method will be called to get custom string representation of the object.
*
* The representation object contains only `entity`, `tech` and `layer`
* without private and deprecated fields (`modName` and `modVal`).
*
* @example
* const BemCell = require('@bem/cell');
* const BemEntityName = require('@bem/entity-name');
* const cell = new BemCell({ entity: new BemEntityName({ block: 'button', mod: 'focused' }),
* tech: 'css', layer: 'desktop' });
*
* cell.valueOf();
*
* // ➜ { entity: { block: 'button', mod: { name: 'focused', value: true } },
* // tech: 'css',
* // layer: 'desktop' }
*
* @returns {{ entity: {block: String, elem: ?String, mod: ?{name: String, val: *}}, tech: *, layer: *}}
*/
valueOf() {
const res = { entity: this._entity.valueOf() };
this._tech && (res.tech = this._tech);
this._layer && (res.layer = this._layer);
return res;
}
/**
* Returns object representing the bem cell. Is needed for debug in Node.js.
*
* In Node.js, `console.log()` calls `util.inspect()` on each argument without a formatting placeholder.
* This method will be called to get custom string representation of the object.
*
* The representation object contains only `entity`, `tech` and `layer` fields
* without private fields.
*
* @example
* const BemCell = require('@bem/cell');
* const BemEntityName = require('@bem/entity-name');
* const cell = new BemCell({ entity: new BemEntityName({ block: 'button' }), tech: 'css', layer: 'desktop' });
*
* console.log(cell); // BemCell { entity: { block: 'button' }, tech: 'css', layer: 'desktop' }
*
* @param {Number} depth — tells inspect how many times to recurse while formatting the object.
* @param {Object} [options] — An optional `options` object may be passed
* that alters certain aspects of the formatted string.
* @returns {String}
*/
inspect(depth, options) {
const stringRepresentation = util.inspect(this.valueOf(), options);
return `BemCell ${stringRepresentation}`;
}
/**
* Return raw data for `JSON.stringify()`.
*
* @returns {{ entity: {block: String, elem: ?String, mod: ?{name: String, val: *}}, tech: *, layer: *}}
*/
toJSON() {
return this.valueOf();
}
/**
* Determines whether specified cell is deep equal to cell or not
*
* @example
* const BemCell = require('@bem/cell');
* const buttonCell1 = BemCell.create({ block: 'button', tech: 'css', layer: 'desktop' });
* const buttonCell2 = BemCell.create({ block: 'button', tech: 'css', layer: 'desktop' });
* const inputCell = BemCell.create({ block: 'input', tech: 'css', layer: 'common' });
*
* buttonCell1.isEqual(buttonCell2); // true
* buttonCell1.isEqual(inputCell); // false
*
* @param {BemCell} cell - the cell to compare
* @returns {Boolean}
*/
isEqual(cell) {
return (cell.tech === this.tech) && (cell.layer === this.layer) && cell.entity.isEqual(this.entity);
}
/**
* Determines whether specified cell is instance of BemCell.
*
* @example
* const BemCell = require('@bem/cell');
* const BemEntityName = require('@bem/entity-name');
*
* const cell = new BemCell({
* entity: new BemEntityName({ block: 'button', elem: 'text' })
* });
*
* BemCell.isBemCell(cell); // true
* BemCell.isBemCell({}); // false
*
* @param {BemCell} cell - the cell to check.
* @returns {boolean} A Boolean indicating whether or not specified entity is instance of BemCell.
*/
static isBemCell(cell) {
return cell && this.name === cell.constructor.name;
}
/**
* Creates BemCell instance by any object representation.
*
* @example
* const BemCell = require('@bem/cell');
*
* BemCell.create({ block: 'my-button', mod: 'theme', val: 'red', tech: 'css' });
* BemCell.create({ block: 'my-button', modName: 'theme', modVal: 'red', tech: 'css' });
* BemCell.create({ entity: { block: 'my-button', modName: 'theme', modVal: 'red' }, tech: 'css' });
* // BemCell { block: 'my-button', mod: { name: 'theme', val: 'red' }, tech: 'css' }
*
* @param {Object} obj — representation of cell.
* @param {String} obj.block — the block name of entity.
* @param {String} [obj.elem] — the element name of entity.
* @param {Object|String} [obj.mod] — the modifier of entity.
* @param {String} [obj.val] — The modifier value of entity. Used if `mod` is a string.
* @param {String} obj.mod.name — the modifier name of entity.
* @param {String} [obj.mod.val] — the modifier value of entity.
* @param {String} [obj.modName] — the modifier name of entity. Used if `mod.name` wasn't specified.
* @param {String} [obj.modVal] — the modifier value of entity. Used if neither `mod.val` nor `val` were not
* specified.
* @param {String} [obj.tech] — technology of cell.
* @param {String} [obj.layer] — layer of cell.
* @returns {BemCell} An object representing cell.
*/
static create(obj) {
if (BemEntityName.isBemEntityName(obj)) {
return new BemCell({ entity: obj });
}
if (BemCell.isBemCell(obj)) {
return obj;
}
const data = {};
data.entity = BemEntityName.create(obj.entity || obj);
obj.tech && (data.tech = obj.tech);
obj.layer && (data.layer = obj.layer);
return new BemCell(data);
}
};