forked from bem-sdk-archive/bem-entity-name
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
368 lines (331 loc) · 12.3 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
'use strict';
const util = require('util');
const stringifyEntity = require('bem-naming').stringify;
/**
* Enum for types of BEM entities.
*
* @readonly
* @enum {String}
*/
const TYPES = {
BLOCK: 'block',
BLOCK_MOD: 'blockMod',
ELEM: 'elem',
ELEM_MOD: 'elemMod'
};
module.exports = class BemEntityName {
/**
* @param {object} obj — representation of entity name.
* @param {string} obj.block — the block name of entity.
* @param {string} [obj.elem] — the element name of entity.
* @param {object} [obj.mod] — the modifier of entity.
* @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.
*/
constructor(obj) {
if (!obj.block) {
throw new Error('This is not valid BEM entity: the field `block` is undefined.');
}
const data = this._data = { block: obj.block };
obj.elem && (data.elem = obj.elem);
const modObj = obj.mod;
const modName = (typeof modObj === 'string' ? modObj : modObj && modObj.name) || obj.modName;
const hasModVal = modObj && modObj.hasOwnProperty('val') || obj.hasOwnProperty('modVal');
if (modName) {
data.mod = {
name: modName,
val: hasModVal ? modObj && modObj.val || obj.modVal : true
};
} else if (modObj || hasModVal) {
throw new Error('This is not valid BEM entity: the field `mod.name` is undefined.');
}
this.__isBemEntityName__ = true;
}
/**
* Returns the name of block to which this entity belongs.
*
* @example
* const BemEntityName = require('@bem/entity-name');
* const name = new BemEntityName({ block: 'button' });
*
* name.block; // button
*
* @returns {string} name of entity block.
*/
get block() { return this._data.block; }
/**
* Returns the element name of this entity.
*
* If entity is not element or modifier of element then returns empty string.
*
* @example
* const BemEntityName = require('@bem/entity-name');
* const name = new BemEntityName({ block: 'button', elem: 'text' });
*
* name.elem; // text
*
* @returns {string|undefined} - name of entity element.
*/
get elem() { return this._data.elem; }
/**
* Returns the modifier of this entity.
*
* Important: If entity is not a modifier then returns `undefined`.
*
* @example
* const BemEntityName = require('@bem/entity-name');
*
* const blockName = new BemEntityName({ block: 'button' });
* const modName = new BemEntityName({ block: 'button', mod: 'disabled' });
*
* modName.mod; // { name: 'disabled', val: true }
* blockName.mod; // undefined
*
* @returns {{mod: string, val: (string|true)}|undefined} - entity modifier.
*/
get mod() { return this._data.mod; }
/**
* Returns the modifier name of this entity.
*
* If entity is not modifier then returns `undefined`.
*
* @returns {string|undefined} - entity modifier name.
* @deprecated - use `mod.name` instead.
*/
get modName() { return this.mod && this.mod.name; }
/**
* Returns the modifier value of this entity.
*
* If entity is not modifier then returns `undefined`.
*
* @returns {string|undefined} - entity modifier name.
* @deprecated - use `mod.val` instead.
*/
get modVal() { return this.mod && this.mod.val; }
/**
* Returns id for this entity.
*
* Important: should only be used to determine uniqueness of entity.
*
* If you want to get string representation in accordance with the provisions naming convention
* you should use `bem-naming` package.
*
* @example
* const BemEntityName = require('@bem/entity-name');
* const name = new BemEntityName({ block: 'button', mod: 'disabled' });
*
* name.id; // button_disabled
*
* @returns {string} - id of entity.
*/
get id() {
if (this._id) { return this._id; }
const entity = { block: this._data.block };
this.elem && (entity.elem = this.elem);
this.modName && (entity.modName = this.modName);
this.modVal && (entity.modVal = this.modVal);
this._id = stringifyEntity(entity);
return this._id;
}
/**
* Returns type for this entity.
*
* @example <caption>type of element</caption>
* const BemEntityName = require('@bem/entity-name');
* const name = new BemEntityName({ block: 'button', elem: 'text' });
*
* name.type; // elem
*
* @example <caption>type of element modifier</caption>
* const BemEntityName = require('@bem/entity-name');
* const name = new BemEntityName({ block: 'menu', elem: 'item', mod: 'current' });
*
* name.type; // elemMod
*
* @returns {string} - type of entity. One of 'block', 'elem', 'blockMod', 'elemMod'.
*/
get type() {
if (this._type) { return this._type; }
const data = this._data;
const isMod = data.mod;
this._type = data.elem
? isMod ? TYPES.ELEM_MOD : TYPES.ELEM
: isMod ? TYPES.BLOCK_MOD : TYPES.BLOCK;
return this._type;
}
/**
* Determines whether modifier simple or not
*
* @example <caption>simple mod</caption>
* const BemEntityName = require('@bem/entity-name');
* const name = new BemEntityName({ block: 'button', mod: { name: 'theme' } });
*
* name.isSimpleMod(); // true
*
* @example <caption>mod with value</caption>
* const BemEntityName = require('@bem/entity-name');
* const name = new BemEntityName({ block: 'button', mod: { name: 'theme', val: 'normal' } });
*
* name.isSimpleMod(); // false
*
* @example <caption>block</caption>
* const BemEntityName = require('@bem/entity-name');
* const name = new BemEntityName({ block: 'button' });
*
* name.isSimpleMod(); // false
*
* @returns {boolean}
*/
isSimpleMod() {
return this.mod ? this.mod.val === true : false;
}
/**
* Returns string representing the entity name.
*
* Important: If you want to get string representation in accordance with the provisions naming convention
* you should use `bem-naming` package.
*
* @example
* const BemEntityName = require('@bem/entity-name');
* const name = new BemEntityName({ block: 'button', mod: 'focused' });
*
* name.toString(); // button_focused
*
* @returns {string}
*/
toString() { return this.id; }
/**
* Returns object representing the entity name. 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 `block`, `elem` and `mod` fields
* without private and deprecated fields (`modName` and `modVal`).
*
* @example
* const BemEntityName = require('@bem/entity-name');
* const name = new BemEntityName({ block: 'button', mod: 'focused' });
*
* name.valueOf();
*
* // ➜ { block: 'button', mod: { name: 'focused', value: true } }
*
* @returns {{block: string, elem: (string|undefined), mod: ({name: string, val: (string|true)}|undefined)}}
*/
valueOf() { return this._data; }
/**
* Returns object representing the entity name. 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 `block`, `elem` and `mod` fields
* without private and deprecated fields (`modName` and `modVal`).
*
* @example
* const BemEntityName = require('@bem/entity-name');
* const name = new BemEntityName({ block: 'button' });
*
* console.log(name); // BemEntityName { block: 'button' }
*
* @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._data, options);
return `BemEntityName ${stringRepresentation}`;
}
/**
* Return raw data for `JSON.stringify()`.
*
* @returns {{block: string, elem: (string|undefined),
* mod: ({name: string, val: (string|true|undefined)}|undefined)}}
*/
toJSON() {
return this._data;
}
/**
* Determines whether specified entity is the deepEqual entity.
*
* @param {BemEntityName} entityName - the entity to compare.
*
* @returns {boolean} - A Boolean indicating whether or not specified entity is the deepEqual entity.
* @example
* const BemEntityName = require('@bem/entity-name');
*
* const inputName = new BemEntityName({ block: 'input' });
* const buttonName = new BemEntityName({ block: 'button' });
*
* inputName.isEqual(buttonName); // false
* buttonName.isEqual(buttonName); // true
*/
isEqual(entityName) {
return entityName && (this.id === entityName.id);
}
/**
* Determines whether specified entity is instance of BemEntityName.
*
* @param {BemEntityName} entityName - the entity to check.
*
* @returns {boolean} A Boolean indicating whether or not specified entity is instance of BemEntityName.
* @example
* const BemEntityName = require('@bem/entity-name');
*
* const entityName = new BemEntityName({ block: 'input' });
*
* BemEntityName.isBemEntityName(entityName); // true
* BemEntityName.isBemEntityName({}); // false
*/
static isBemEntityName(entityName) {
return entityName && entityName.__isBemEntityName__;
}
/**
* Creates BemEntityName instance by any object representation.
*
* @param {object} obj — representation of entity name.
* @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 `obj.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 `obj.mod.name` wasn't specified.
* @param {string} [obj.modVal] — the modifier value of entity.
* Used if neither `obj.mod.val` nor `obj.val` were not specified.
*
* @returns {BemEntityName} An object representing entity name.
* @example
* const BemEntityName = require('@bem/entity-name');
*
* BemEntityName.create('my-button_theme_red');
* BemEntityName.create({ block: 'my-button', mod: 'theme', val: 'red' });
* BemEntityName.create({ block: 'my-button', modName: 'theme', modVal: 'red' });
* // → BemEntityName { block: 'my-button', mod: { name: 'theme', val: 'red' } }
*/
static create(obj) {
if (BemEntityName.isBemEntityName(obj)) {
return obj;
}
const data = { block: obj.block };
const mod = obj.mod;
obj.elem && (data.elem = obj.elem);
if (mod || obj.modName) {
const isString = typeof mod === 'string';
const modName = (isString ? mod : mod && mod.name) || obj.modName;
const modObj = !isString && mod || obj;
const hasModVal = modObj.hasOwnProperty('val') || obj.hasOwnProperty('modVal');
data.mod = {
name: modName,
val: hasModVal ? modObj.val || obj.modVal : true
};
}
return new BemEntityName(data);
}
};