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

Commit

Permalink
Merge pull request #89 from bem-sdk/qfox.short-syntax
Browse files Browse the repository at this point in the history
BemEntityName.create(string)
  • Loading branch information
qfox authored Mar 25, 2017
2 parents 900ff30 + baaa7a6 commit 9396a95
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ BemEntityName.isBemEntityName({ block: 'button' }); // false

### #create(object)

Creates BemEntityName instance by any object representation.
Creates BemEntityName instance by any object representation or a string.

Helper for sugar-free simplicity.

Parameter | Type | Description
-------------|--------------------|--------------------------
`object` | `object` | Representation of entity name.
`object` | `object`, `string` | Representation of entity name.

Passed Object could have the common field names for entities:

Expand All @@ -293,12 +293,16 @@ Object field | Type | Description
`val` | `string` | The modifier value of entity. Used if `mod` is a string. Optional.
`mod.name` | `string` | The modifier name of entity. Optional.
`mod.val` | `string`, `true` | The modifier value of entity. Optional.
`modName` | `string` | The modifier name of entity. Used if `mod.name` wasn't specified. Optional.
`modName` | `string` | The modifier name of entity. Used if `mod.name` was not specified. Optional.
`modVal` | `string`, `true` | The modifier value of entity. Used if neither `mod.val` nor `val` were not specified. Optional.

```js
const BemEntityName = require('@bem/entity-name');
BemEntityName.create('my-button');
BemEntityName.create({ block: 'my-button' });
// ➜ BemEntityName { block: 'my-button' }
BemEntityName.create({ block: 'my-button', mod: 'theme', val: 'red' });
BemEntityName.create({ block: 'my-button', modName: 'theme', modVal: 'red' });
// ➜ BemEntityName { block: 'my-button', mod: { name: 'theme', val: 'red' } }
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ module.exports = class BemEntityName {
/**
* Creates BemEntityName instance by any object representation.
*
* @param {object} obj — representation of entity name.
* @param {object|string} obj — representation of entity name.
* @param {string} obj.block — the block name of entity.
* @param {string} [obj.elem] — the element name of entity.
* @param {object|string} [obj.mod] — the modifier of entity.
Expand All @@ -339,6 +339,10 @@ module.exports = class BemEntityName {
return obj;
}

if (typeof obj === 'string') {
obj = { block: obj };
}

const data = { block: obj.block };
const mod = obj.mod;

Expand Down
6 changes: 6 additions & 0 deletions test/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ test('should use `mod.name` and `mod.val` instead of `modVal` and `val`', t => {

t.is(entity.mod.val, 'v1');
});

test('should create block entity by a string', t => {
const entity = BemEntityName.create('my-block');

t.deepEqual(entity.valueOf(), { block: 'my-block' });
});

0 comments on commit 9396a95

Please sign in to comment.