Skip to content
This repository was archived by the owner on Feb 4, 2018. It is now read-only.

Commit ae9c83f

Browse files
author
blond
committed
feat(scope): add scope field
1 parent 580c45c commit ae9c83f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib/entity-name.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,36 @@ class BemEntityName {
156156
return this._type;
157157
}
158158

159+
/**
160+
* Returns scope of this entity.
161+
*
162+
* Important: block-typed entities has no scope.
163+
*
164+
* @example
165+
* const BemEntityName = require('@bem/entity-name');
166+
*
167+
* const buttonName = new BemEntityName({ block: 'button' });
168+
* const buttonTextName = new BemEntityName({ block: 'button', elem: 'text' });
169+
* const buttonTextBoldName = new BemEntityName({ block: 'button', elem: 'text', mod: 'bold' });
170+
*
171+
* buttonName.scope; // null
172+
* buttonTextName.scope; // BemEntityName { block: 'button' }
173+
* buttonTextBoldName.scope; // BemEntityName { block: 'button', elem: 'elem' }
174+
*
175+
* @returns {BemEntityName} - scope entity name.
176+
*/
177+
get scope() {
178+
if (this.type === TYPES.BLOCK) { return null; }
179+
if (this._scope) { return this._scope; }
180+
181+
this._scope = new BemEntityName({
182+
block: this.block,
183+
elem: this.type === TYPES.ELEM_MOD && this.elem
184+
});
185+
186+
return this._scope;
187+
}
188+
159189
/**
160190
* Returns id for this entity.
161191
*
@@ -238,6 +268,7 @@ class BemEntityName {
238268
*
239269
* buttonTextName.belongsTo(buttonName); // true
240270
* buttonName.belongsTo(buttonTextName); // false
271+
*
241272
* buttonTextBoldName.belongsTo(buttonTextName); // true
242273
* buttonTextBoldName.belongsTo(buttonName); // false
243274
*

0 commit comments

Comments
 (0)