Skip to content

Commit

Permalink
fix(stringify):
Browse files Browse the repository at this point in the history
The stringify method should return `undefined` if impossible to
stringify BEM notation.

It will be easier to check for an empty string than use `try..catch`.

**Before changes:**

```js
try {
    var str = bemNaming.stringify({ elem: 'elem' });
} catch(e) { /* ... */ }
```

**After changes:**

```js
var str = bemNaming.stringify({ elem: 'elem' });

if (str) {
    /* ... */
}
```
  • Loading branch information
blond committed Mar 20, 2016
1 parent 87187a4 commit ce02c5f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions index.js
Expand Up @@ -60,15 +60,15 @@ BEMNaming.prototype.validate = function (str) {
/**
* Returns a string indicating the type of the BEM entity.
*
* @param {Object|String} obj BEM-naming object or string representation of BEM entity.
* @param {Object|String|undefined} obj BEM-naming object or string representation of BEM entity.
* @returns {String}
*/
BEMNaming.prototype.typeOf = function (obj) {
if (typeof obj === 'string') {
obj = this.parse(obj);
}

if (!obj || !obj.block) { return; }
if (!obj || !obj.block) { return undefined; }

var modName = obj.modName,
isMod = modName && (obj.modVal || !obj.hasOwnProperty('modVal'));
Expand Down Expand Up @@ -131,7 +131,7 @@ BEMNaming.prototype.isElemMod = function (obj) {
BEMNaming.prototype.parse = function (str) {
var executed = this._regex.exec(str);

if (!executed) { return; }
if (!executed) { return undefined; }

var notation = {
block: executed[1] || executed[4]
Expand Down Expand Up @@ -159,7 +159,7 @@ BEMNaming.prototype.parse = function (str) {
*/
BEMNaming.prototype.stringify = function (obj) {
if (!obj || !obj.block) {
throw new Error('The field `block` is undefined. It is impossible to stringify BEM notation.');
return undefined;
}

var res = obj.block;
Expand Down
6 changes: 3 additions & 3 deletions test/stringify.test.js
Expand Up @@ -4,7 +4,7 @@ const test = require('ava');
const naming = require('../index');

test('should not stringify not valid notation', t => {
t.throws(() => {
naming.stringify({});
}, 'The field `block` is undefined. It is impossible to stringify BEM notation.');
const str = naming.stringify({});

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

0 comments on commit ce02c5f

Please sign in to comment.