Skip to content

Commit

Permalink
Merge pull request #153 from bem-sdk/issue-152
Browse files Browse the repository at this point in the history
feat(stringify): support BemEntityName
  • Loading branch information
blond committed Dec 17, 2016
2 parents b330819 + 3a10f93 commit 8216699
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 53 deletions.
30 changes: 30 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,36 @@ console.log(entityName); // BemEntityName { block: 'button', mod: { name: 'disab
[#126]: https://github.com/bem-sdk/bem-naming/issues/126
[#95]: https://github.com/bem-sdk/bem-naming/issues/95

#### The `stringify` method supports [BemEntityName](https://github.com/bem-sdk/bem-entity-name) instance ([#152]).

**Important:** in `BemEntityName` the `modName` and `modVal` fields are deprecated. Use the `mod` field instead ([#95]).

**API v1.x.x**

```js
const stringify = require('bem-naming').stringify;

stringify({ block: 'button', modName: 'disabled', modVal: true });

// ➜ button_disabled
```

**API v2.x.x**

```js
const stringify = require('@bem/naming').stringify;
const BemEntityName = require('@bem/entity-name');

const entityName = new BemEntityName({ block: 'button', mod: 'disabled' });

stringify(entityName);

// ➜ button_disabled
```

[#152]: https://github.com/bem-sdk/bem-naming/issues/152
[#95]: https://github.com/bem-sdk/bem-naming/issues/95

### Performance

* Accelerated initialization for `origin` naming (@tadatuta [#134]).
Expand Down
4 changes: 2 additions & 2 deletions benchmark/stringify.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var naming = require('../index'),
notations = {
block: { block: 'block' },
blockMod: { block: 'block', modName: 'mod-name', modVal: 'mod-val' },
blockMod: { block: 'block', mod: { name: 'mod-name', val: 'mod-val' } },
elem: { block: 'block', elem: 'elem' },
elemMod: { block: 'block', elem: 'elem', modName: 'mod-name', modVal: 'mod-val' }
elemMod: { block: 'block', elem: 'elem', mod: { name: 'mod-name', val: 'mod-val' } }
};

suite('stringify', function () {
Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ function createNaming(options) {
res += delims.elem + obj.elem;
}

if (obj.modName) {
const modVal = obj.modVal;
const modObj = obj.mod;
const modName = (typeof modObj === 'string' ? modObj : modObj && modObj.name) || obj.modName;

if (modVal || modVal === 0 || !obj.hasOwnProperty('modVal')) {
res += delims.mod.name + obj.modName;
if (modName) {
const hasModVal = modObj && modObj.hasOwnProperty('val') || obj.hasOwnProperty('modVal');
const modVal = modObj && modObj.val || obj.modVal;

if (modVal || modVal === 0 || !hasModVal) {
res += delims.mod.name + modName;
}

if (modVal && modVal !== true) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"index.js"
],
"dependencies": {
"@bem/entity-name": "1.0.0"
"@bem/entity-name": "1.1.0"
},
"devDependencies": {
"ava": "^0.17.0",
Expand Down
40 changes: 17 additions & 23 deletions test/presets/original/stringify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,43 @@ test('should stringify block', t => {
t.is(str, 'block');
});

test('should stringify mod of block', t => {
test('should stringify modifier of block', t => {
const str = stringify({
block: 'block',
modName: 'mod',
modVal: 'val'
mod: { name: 'mod', val: 'val' }
});

t.is(str, 'block_mod_val');
});

test('should stringify boolean mod of block', t => {
test('should stringify simple modifier of block', t => {
const str = stringify({
block: 'block',
modName: 'mod'
mod: 'mod'
});

t.is(str, 'block_mod');
});

test('should stringify boolean mod of block by strict notation', t => {
test('should stringify boolean modifier of block', t => {
const str = stringify({
block: 'block',
modName: 'mod',
modVal: true
mod: { name: 'mod', val: true },
});

t.is(str, 'block_mod');
});

test('should stringify block if `modVal` filed is `undefined`', t => {
test('should stringify block if modifier value is `undefined`', t => {
const str = stringify({
block: 'block',
modName: 'mod',
modVal: undefined
mod: { name: 'mod', val: undefined }
});

t.is(str, 'block');
});

test('should stringify elem', t => {
test('should stringify element', t => {
const str = stringify({
block: 'block',
elem: 'elem'
Expand All @@ -58,44 +55,41 @@ test('should stringify elem', t => {
t.is(str, 'block__elem');
});

test('should stringify mod of elem', t => {
test('should stringify modifier of element', t => {
const str = stringify({
block: 'block',
elem: 'elem',
modName: 'mod',
modVal: 'val'
mod: { name: 'mod', val: 'val' }
});

t.is(str, 'block__elem_mod_val');
});

test('should stringify boolean mod of elem', t => {
test('should stringify simple modifier of element', t => {
const str = stringify({
block: 'block',
elem: 'elem',
modName: 'mod'
mod: 'mod'
});

t.is(str, 'block__elem_mod');
});

test('should stringify boolean mod of elem by strict notation', t => {
test('should stringify boolean modifier of element', t => {
const str = stringify({
block: 'block',
elem: 'elem',
modName: 'mod',
modVal: true
mod: { name: 'mod', val: true }
});

t.is(str, 'block__elem_mod');
});

test('should stringify elem if `modVal` filed is `undefined`', t => {
test('should stringify element if modifier value is `undefined`', t => {
const str = stringify({
block: 'block',
elem: 'elem',
modName: 'mod',
modVal: undefined
mod: { name: 'mod', val: undefined }
});

t.is(str, 'block__elem');
Expand Down
40 changes: 17 additions & 23 deletions test/presets/two-dashes/stringify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,43 @@ test('should stringify block', t => {
t.is(str, 'block');
});

test('should stringify mod of block', t => {
test('should stringify modifier of block', t => {
const str = stringify({
block: 'block',
modName: 'mod',
modVal: 'val'
mod: { name: 'mod', val: 'val' }
});

t.is(str, 'block--mod_val');
});

test('should stringify boolean mod of block', t => {
test('should stringify simple modifier of block', t => {
const str = stringify({
block: 'block',
modName: 'mod'
mod: 'mod'
});

t.is(str, 'block--mod');
});

test('should stringify boolean mod of block by strict notation', t => {
test('should stringify boolean modifier of block', t => {
const str = stringify({
block: 'block',
modName: 'mod',
modVal: true
mod: { name: 'mod', val: true }
});

t.is(str, 'block--mod');
});

test('should stringify block if `modVal` filed is `undefined`', t => {
test('should stringify block if modifier value is `undefined`', t => {
const str = stringify({
block: 'block',
modName: 'mod',
modVal: undefined
mod: { name: 'mod', val: undefined }
});

t.is(str, 'block');
});

test('should stringify elem', t => {
test('should stringify element', t => {
const str = stringify({
block: 'block',
elem: 'elem'
Expand All @@ -58,44 +55,41 @@ test('should stringify elem', t => {
t.is(str, 'block__elem');
});

test('should stringify mod of elem', t => {
test('should stringify simple modifier of element', t => {
const str = stringify({
block: 'block',
elem: 'elem',
modName: 'mod',
modVal: 'val'
mod: { name: 'mod', val: 'val' }
});

t.is(str, 'block__elem--mod_val');
});

test('should stringify boolean mod of elem', t => {
test('should stringify boolean modifier of element', t => {
const str = stringify({
block: 'block',
elem: 'elem',
modName: 'mod'
mod: 'mod'
});

t.is(str, 'block__elem--mod');
});

test('should stringify boolean mod of elem by strict notation', t => {
test('should stringify boolean modifier of element', t => {
const str = stringify({
block: 'block',
elem: 'elem',
modName: 'mod',
modVal: true
mod: { name: 'mod', val: true }
});

t.is(str, 'block__elem--mod');
});

test('should stringify elem if `modVal` filed is `undefined`', t => {
test('should stringify element if modifier value is `undefined`', t => {
const str = stringify({
block: 'block',
elem: 'elem',
modName: 'mod',
modVal: undefined
mod: { name: 'mod', val: undefined }
});

t.is(str, 'block__elem');
Expand Down
30 changes: 30 additions & 0 deletions test/stringify.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
'use strict';

const test = require('ava');
const BemEntityName = require('@bem/entity-name');

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

test('should not stringify not valid notation', t => {
const str = naming.stringify({});

t.is(str, undefined);
});

test('should support block instance of BemEntityName', t => {
const obj = { block: 'block' };
const entityName = new BemEntityName(obj);

t.is(naming.stringify(obj), naming.stringify(entityName));
});

test('should support modifier instance of BemEntityName', t => {
const obj = { block: 'block', mod: 'mod' };
const entityName = new BemEntityName(obj);

t.is(naming.stringify(obj), naming.stringify(entityName));
});

test('should support element instance of BemEntityName', t => {
const obj = { block: 'block', elem: 'elem' };
const entityName = new BemEntityName(obj);

t.is(naming.stringify(obj), naming.stringify(entityName));
});

test('should support element modifier instance of BemEntityName', t => {
const obj = { block: 'block', mod: 'mod' };
const entityName = new BemEntityName(obj);

t.is(naming.stringify(obj), naming.stringify(entityName));
});

0 comments on commit 8216699

Please sign in to comment.