Skip to content

Commit

Permalink
Merge pull request #8 from bem-sdk/issue-2
Browse files Browse the repository at this point in the history
Id field impl
  • Loading branch information
blond committed Dec 12, 2016
2 parents 9e4e115 + d367552 commit 9d4a4ae
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const cell = new BemCell({
cell.entity; // ➜ BemEntityName { block: 'button', elem: 'text' }
cell.tech; // css
cell.layer; // common
cell.id; // button__text@common.css
```

License
Expand Down
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,36 @@ module.exports = class BemCell {
* @returns {string} layer of cell.
*/
get layer() { return this._layer; }

/**
* 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;
}
};
54 changes: 54 additions & 0 deletions test/id.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const test = require('ava');
const BemEntityName = require('@bem/entity-name');

const BemCell = require('../index');

test('should provide `id` field', t => {
const cell = new BemCell({
entity: new BemEntityName({ block: 'block' }),
layer: 'desktop',
tech: 'css'
});

t.is(cell.id, 'block@desktop.css');
});

test('should provide `id` field for cell with entity `field` only', t => {
const cell = new BemCell({
entity: new BemEntityName({ block: 'block' })
});

t.is(cell.id, 'block');
});

test('should provide `id` field for cell with `tech` field', t => {
const cell = new BemCell({
entity: new BemEntityName({ block: 'block' }),
tech: 'css'
});

t.is(cell.id, 'block.css');
});

test('should provide `id` field for cell with `layer` field', t => {
const cell = new BemCell({
entity: new BemEntityName({ block: 'block' }),
layer: 'desktop',
});

t.is(cell.id, 'block@desktop');
});

test('should cache `id` field', t => {
const cell = new BemCell({
entity: new BemEntityName({ block: 'block' }),
layer: 'desktop',
tech: 'css'
});
const id = cell.id;

cell._tech = 'js';
cell._layer = 'common';

t.is(cell.id, id);
});

0 comments on commit 9d4a4ae

Please sign in to comment.