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

Commit 33bed0f

Browse files
authored
Merge pull request #95 from bem-sdk/fix/type-error
Refactor of TypeError
2 parents 3a919b7 + ce206fb commit 33bed0f

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,33 @@ BEM entities can be defined with a help of JS object with the following fields:
5757

5858
The modifier consists of a pair of fields `mod.name` and `mod.val`. This means that the field `mod.val` without `mod.name` has no meaning.
5959

60-
**Example:**
61-
6260
```js
6361
const BemEntityName = require('@bem/entity-name');
6462

6563
// The modifier of block
6664
new BemEntityName({
6765
block: 'button',
68-
mod: { name 'view', val: 'action' }
66+
mod: { name: 'view', val: 'action' }
6967
});
7068

7169
// Not valid modifier
7270
new BemEntityName({
73-
block: 'block',
71+
block: 'button',
7472
mod: { val: 'action' }
7573
});
74+
// ➜ EntityTypeError: the object `{ block: 'block', mod: { val: 'action' } }` is not valid BEM entity, the field `mod.name` is undefined
7675
```
7776

78-
To describe the simple modifier, field `mod.val` must be specified as `true`.
79-
80-
**Example:**
77+
To describe a simple modifier the `mod.val` field must be omitted.
8178

8279
```js
83-
// Boolean modifier of a block
84-
new BemEntityName({
85-
block: 'button',
86-
mod: { name: 'focused', val: true }
87-
});
80+
// Simple modifier of a block
81+
new BemEntityName({ block: 'button', mod: 'focused' });
8882

89-
// Shorthand for the boolean modifier of a block
83+
// Is equivalent to simple modifier, if `mod.val` is `true`
9084
new BemEntityName({
9185
block: 'button',
92-
mod: 'focused'
86+
mod: { name: 'focused', val: true }
9387
});
9488
```
9589

index.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ const TYPES = {
1717
ELEM: 'elem',
1818
ELEM_MOD: 'elemMod'
1919
};
20-
21-
class NotValidEntityError extends ExtendableError {
20+
/**
21+
* The EntityTypeError object represents an error when a value is not valid BEM entity.
22+
*/
23+
class EntityTypeError extends ExtendableError {
2224
/**
2325
* @param {object} obj — not valid object
24-
* @param {str} [reason] — reason why object is not valid
26+
* @param {string} [reason] — human-readable reason why object is not valid
2527
*/
2628
constructor(obj, reason) {
2729
const str = util.inspect(obj, { depth: 1 });
28-
const message = `The object \`${str}\` is not valid BEM entity`;
30+
const message = `the object \`${str}\` is not valid BEM entity`;
2931

30-
super(reason ? `${message}: ${reason}` : message);
32+
super(reason ? `${message}, ${reason}` : message);
3133
}
3234
}
3335

@@ -45,7 +47,7 @@ module.exports = class BemEntityName {
4547
*/
4648
constructor(obj) {
4749
if (!obj.block) {
48-
throw new NotValidEntityError(obj, 'the field `block` is undefined');
50+
throw new EntityTypeError(obj, 'the field `block` is undefined');
4951
}
5052

5153
const data = this._data = { block: obj.block };
@@ -62,7 +64,7 @@ module.exports = class BemEntityName {
6264
val: hasModVal ? modObj && modObj.val || obj.modVal : true
6365
};
6466
} else if (modObj || hasModVal) {
65-
throw new NotValidEntityError(obj, 'the field `mod.name` is undefined');
67+
throw new EntityTypeError(obj, 'the field `mod.name` is undefined');
6668
}
6769

6870
this.__isBemEntityName__ = true;

test/construct.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ test('should create object and normalize boolean modifier', t => {
2929
test('should throw error for if entity object is not valid', t => {
3030
t.throws(
3131
() => new BemEntityName({ elem: 'elem' }),
32-
"The object `{ elem: 'elem' }` is not valid BEM entity: the field `block` is undefined"
32+
"the object `{ elem: 'elem' }` is not valid BEM entity, the field `block` is undefined"
3333
);
3434
});
3535

3636
test('should throw error for if mod object is empty', t => {
3737
t.throws(
3838
() => new BemEntityName({ block: 'block', mod: {} }),
39-
"The object `{ block: 'block', mod: {} }` is not valid BEM entity: the field `mod.name` is undefined"
39+
"the object `{ block: 'block', mod: {} }` is not valid BEM entity, the field `mod.name` is undefined"
4040
);
4141
});
4242

4343
test('should throw error for if mod name is undefined', t => {
4444
t.throws(
4545
() => new BemEntityName({ block: 'block', mod: { val: 'val' } }),
46-
"The object `{ block: 'block', mod: { val: 'val' } }` is not valid BEM entity: the field `mod.name` is undefined"
46+
"the object `{ block: 'block', mod: { val: 'val' } }` is not valid BEM entity, the field `mod.name` is undefined"
4747
);
4848
});

0 commit comments

Comments
 (0)