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

Commit d131857

Browse files
authored
Merge pull request #110 from bem-sdk/issue-100
Scope field
2 parents 580c45c + 8f52467 commit d131857

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ API
6464
* [elem](#elem)
6565
* [mod](#mod)
6666
* [type](#type)
67+
* [scope](#scope)
6768
* [id](#id)
6869
* [isSimpleMod()](#issimplemod)
6970
* [isEqual(entityName)](#isequalentityname)
@@ -178,6 +179,24 @@ elemName.type; // elem
178179
modName.type; // elemMod
179180
```
180181

182+
### scope
183+
184+
The scope of this entity.
185+
186+
**Important:** block-typed entities has no scope.
187+
188+
```js
189+
const BemEntityName = require('@bem/entity-name');
190+
191+
const buttonName = new BemEntityName({ block: 'button' });
192+
const buttonTextName = new BemEntityName({ block: 'button', elem: 'text' });
193+
const buttonTextBoldName = new BemEntityName({ block: 'button', elem: 'text', mod: 'bold' });
194+
195+
buttonName.scope; // null
196+
buttonTextName.scope; // BemEntityName { block: 'button' }
197+
buttonTextBoldName.scope; // BemEntityName { block: 'button', elem: 'elem' }
198+
```
199+
181200
### id
182201

183202
The id for this entity.

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ declare class BemEntityName {
99
readonly modName: string | undefined;
1010
readonly modVal: string | true | undefined;
1111
readonly type: BemSDK.EntityName.TYPE;
12+
readonly scope: BemEntityName | null;
1213
readonly id: string;
1314

1415
isSimpleMod(): boolean | null;

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
*

test/scope.test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import test from 'ava';
2+
3+
import BemEntityName from '../lib/entity-name';
4+
5+
test('should return scope of block', t => {
6+
const entityName = new BemEntityName({ block: 'block' });
7+
8+
t.is(entityName.scope, null);
9+
});
10+
11+
test('should return scope of block modifier', t => {
12+
const entityName = new BemEntityName({ block: 'block', mod: 'mod' });
13+
14+
t.deepEqual(entityName.scope.valueOf(), { block: 'block' });
15+
});
16+
17+
test('should return same scope for simple and complex mod', t => {
18+
const simpleModName = new BemEntityName({ block: 'block', mod: 'mod' });
19+
const complexModName = new BemEntityName({ block: 'block', mod: { name: 'mod', val: 'val' } });
20+
21+
t.deepEqual(simpleModName.scope, complexModName.scope);
22+
});
23+
24+
test('should return scope of element', t => {
25+
const entityName = new BemEntityName({ block: 'block', elem: 'elem' });
26+
27+
t.deepEqual(entityName.scope.valueOf(), { block: 'block' });
28+
});
29+
30+
test('should return scope of element modifier', t => {
31+
const entityName = new BemEntityName({ block: 'block', elem: 'elem', mod: 'mod' });
32+
33+
t.deepEqual(entityName.scope.valueOf(), { block: 'block', elem: 'elem' });
34+
});
35+
36+
test('should cache scope value', t => {
37+
const entity = new BemEntityName({ block: 'block', elem: 'elem' });
38+
39+
entity.scope; // eslint-disable-line no-unused-expressions
40+
41+
t.deepEqual(entity._scope.valueOf(), { block: 'block' });
42+
});
43+
44+
test('should get scope from cache', t => {
45+
const entity = new BemEntityName({ block: 'block', elem: 'elem' });
46+
47+
entity._scope = 'fake';
48+
49+
t.is(entity.scope, 'fake');
50+
});

0 commit comments

Comments
 (0)