From a6678d9719139411d769c323cc68fb53f3d63d49 Mon Sep 17 00:00:00 2001 From: Alexey Yaroshevich Date: Thu, 26 Jan 2017 21:32:53 +0300 Subject: [PATCH 1/2] BREAKING: simplify construct and add create method --- index.js | 21 +++++---------------- test/normalize.test.js | 39 --------------------------------------- 2 files changed, 5 insertions(+), 55 deletions(-) delete mode 100644 test/normalize.test.js diff --git a/index.js b/index.js index 69a0e1a..525d66a 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,10 @@ 'use strict'; +const assert = require('assert'); const util = require('util'); const stringifyEntity = require('bem-naming').stringify; +const parseEntity = require('bem-naming').parse; /** * Enum for types of BEM entities. @@ -30,26 +32,13 @@ module.exports = class BemEntityName { * Used if neither `mod.val` nor `val` were not specified. */ constructor(obj) { - if (!obj.block) { - throw new Error('This is not valid BEM entity: the field `block` is undefined.'); - } + assert(obj.block, 'This is not valid BEM entity: the field `block` is undefined.'); + assert(!obj.mod || obj.mod.name, 'This is not valid BEM entity: the field `mod.name` is undefined.'); const data = this._data = { block: obj.block }; obj.elem && (data.elem = obj.elem); - - const modObj = obj.mod; - const modName = (typeof modObj === 'string' ? modObj : modObj && modObj.name) || obj.modName; - const hasModVal = modObj && modObj.hasOwnProperty('val') || obj.hasOwnProperty('modVal'); - - if (modName) { - data.mod = { - name: modName, - val: hasModVal ? modObj && modObj.val || obj.modVal : true - }; - } else if (modObj || hasModVal) { - throw new Error('This is not valid BEM entity: the field `mod.name` is undefined.'); - } + obj.mod && (data.mod = { name: obj.mod.name, val: obj.mod.val || true }); this.__isBemEntityName__ = true; } diff --git a/test/normalize.test.js b/test/normalize.test.js deleted file mode 100644 index 1cce175..0000000 --- a/test/normalize.test.js +++ /dev/null @@ -1,39 +0,0 @@ -const test = require('ava'); - -const BemEntityName = require('../index'); - -test('should normalize boolean modifier', t => { - const entity = new BemEntityName({ block: 'block', mod: { name: 'mod' } }); - - t.true(entity.mod.val); -}); - -test('should normalize short entry for boolean modifier', t => { - const entity = new BemEntityName({ block: 'block', mod: 'mod' }); - - t.true(entity.mod.val); -}); - -test('should support `modName` and `modVal` fields', t => { - const entity = new BemEntityName({ block: 'block', modName: 'mod', modVal: 'val' }); - - t.deepEqual(entity.mod, { name: 'mod', val: 'val' }); -}); - -test('should support `modName` field only', t => { - const entity = new BemEntityName({ block: 'block', modName: 'mod' }); - - t.deepEqual(entity.mod, { name: 'mod', val: true }); -}); - -test('should use `mod.name` field instead of `modName`', t => { - const entity = new BemEntityName({ block: 'block', mod: { name: 'mod1' }, modName: 'mod2' }); - - t.is(entity.mod.name, 'mod1'); -}); - -test('should use `mod.val` field instead of `modVal`', t => { - const entity = new BemEntityName({ block: 'block', mod: { name: 'mod', val: 'val1' }, modVal: 'val2' }); - - t.is(entity.mod.val, 'val1'); -}); From 7d116b29878d7a16f8d75a457325f5b469c70fd3 Mon Sep 17 00:00:00 2001 From: Alexey Yaroshevich Date: Thu, 26 Jan 2017 22:56:22 +0300 Subject: [PATCH 2/2] docs: update readme --- README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/README.md b/README.md index 80abd71..0023546 100644 --- a/README.md +++ b/README.md @@ -85,12 +85,6 @@ new BemEntityName({ block: 'button', mod: { name: 'focused', val: true } }); - -// Shorthand for the boolean modifier of a block -new BemEntityName({ - block: 'button', - mod: 'focused' -}); ``` API