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

We should not use bem-naming just for id getter #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/entity-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const util = require('util');

const stringifyEntity = require('@bem/naming').stringify;

const deprecate = require('./deprecate');
const EntityTypeError = require('./entity-type-error');

Expand Down Expand Up @@ -205,7 +203,12 @@ class BemEntityName {
get id() {
if (this._id) { return this._id; }

this._id = stringifyEntity(this._data);
const e = this._data;

this._id = e.block +
(!e.elem ? '' : '__' + e.elem) +
(!e.mod ? '' : '_' + e.mod.name +
(e.mod.val === true ? '' : '_' + e.mod.val));

return this._id;
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"node": ">= 4.0"
},
"dependencies": {
"@bem/naming": "2.0.0-5",
"depd": "1.1.0",
"es6-error": "4.0.2"
},
Expand Down
21 changes: 5 additions & 16 deletions test/id.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import test from 'ava';
import sinon from 'sinon';
import proxyquire from 'proxyquire';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


import BemEntityName from '..';

Expand All @@ -18,19 +16,10 @@ test('should build not equal id for not equal blocks', t => {
t.not(entityName1.id, entityName2.id);
});

test('should cache id value', t => {
const stub = sinon.stub().returns('id');
const StubBemEntityName = proxyquire('../lib/entity-name', {
'@bem/naming': {
stringify: stub
}
});

const entityName = new StubBemEntityName({ block: 'block' });

/*eslint no-unused-expressions: "off"*/
entityName.id;
entityName.id;
test('should cache id for repeating calls', t => {
const entityName1 = new BemEntityName({ block: 'block1' });
const id = entityName1.id;

t.is(stub.callCount, 1);
entityName1._data = {};
t.is(id, entityName1.id);
});
42 changes: 19 additions & 23 deletions test/to-string.test.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
import test from 'ava';
import sinon from 'sinon';
import proxyquire from 'proxyquire';

const spy = sinon.spy();
const BemEntityName = proxyquire('../lib/entity-name', {
'@bem/naming': {
stringify: spy
}
});
import BemEntityName from '..';

test('should use `naming.stringify()` for block', t => {
test('should stringify block entity name', t => {
const entityName = new BemEntityName({ block: 'block' });

entityName.toString();

t.true(spy.calledWith({ block: 'block' }));
t.is(entityName.toString(), 'block');
});

test('should use `naming.stringify()` for elem', t => {
test('should stringify elem entity name', t => {
const entityName = new BemEntityName({ block: 'block', elem: 'elem' });

entityName.toString();
t.is(entityName.toString(), 'block__elem');
});

test('should stringify block’s simple modifier entity name', t => {
const entityName = new BemEntityName({ block: 'block', mod: { name: 'mod', val: true } });

t.true(spy.calledWith({ block: 'block', elem: 'elem' }));
t.is(entityName.toString(), 'block_mod');
});

test('should use `naming.stringify()` for block modifier', t => {
test('should stringify block’s modifier entity name', t => {
const entityName = new BemEntityName({ block: 'block', mod: { name: 'mod', val: 'val' } });

entityName.toString();
t.is(entityName.toString(), 'block_mod_val');
});

test('should stringify element’s simple modifier entity name', t => {
const entityName = new BemEntityName({ block: 'block', elem: 'elem', mod: { name: 'mod', val: true } });

t.true(spy.calledWith({ block: 'block', mod: { name: 'mod', val: 'val' } }));
t.is(entityName.toString(), 'block__elem_mod');
});

test('should use naming.stringify() for element modifier', t => {
test('should stringify element’s modifier entity name', t => {
const entityName = new BemEntityName({ block: 'block', elem: 'elem', mod: { name: 'mod', val: 'val' } });

entityName.toString();

t.true(spy.calledWith({ block: 'block', elem: 'elem', mod: { name: 'mod', val: 'val' } }));
t.is(entityName.toString(), 'block__elem_mod_val');
});