Skip to content

Commit

Permalink
fixup! Add is-equal method
Browse files Browse the repository at this point in the history
  • Loading branch information
skad0 committed Feb 14, 2017
1 parent 8d12865 commit 748a5c1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
11 changes: 7 additions & 4 deletions README.md
Expand Up @@ -179,7 +179,7 @@ Returns an object for `JSON.stringify()` purpose.

### isEqual(cell)

Determines whether specified cell is deep equal cell.
Determines whether specified cell is deep equal to cell or not.

Parameter | Type | Description
----------|-----------------|-----------------------
Expand All @@ -188,15 +188,18 @@ Parameter | Type | Description
```js
const BemCell = require('@bem/cell');
const BemEntityName = require('@bem/entity-name');
const buttonCell = new BemCell({
const buttonCell1 = new BemCell({
entity: new BemEntityName({ block: 'button' }), tech: 'css', layer: 'desktop'
});
const buttonCell2 = new BemCell({
entity: new BemEntityName({ block: 'button' }), tech: 'css', layer: 'desktop'
});
const inputCell = new BemCelL({
entity: new BemEntityName({ block: 'input' }), tech: 'css', layer: 'common'
});

buttonCell.isEqual(buttonCell); // true
buttonCell.isEqual(inputCell); // false
buttonCell1.isEqual(buttonCell2); // true
buttonCell1.isEqual(inputCell); // false
```

### #isBemCell(cell)
Expand Down
15 changes: 6 additions & 9 deletions index.js
Expand Up @@ -210,7 +210,6 @@ module.exports = class BemCell {
* @param {integer} depth — tells inspect how many times to recurse while formatting the object.
* @param {object} options — An optional `options` object may be passed
* that alters certain aspects of the formatted string.
*
* @returns {string}
*/
inspect(depth, options) {
Expand All @@ -229,19 +228,19 @@ module.exports = class BemCell {
}

/**
* Determines whether specified cell is deep equal cell
* Determines whether specified cell is deep equal to cell or not
*
* @param {BemCell} cell - the cell to compare
*
* @returns {Boolean} - A boolean indicating whether or not scecified cell is deep equal
* @returns {Boolean} - A boolean value indicating whether or not specified cell is deep equal
* @example
* const BemCell = require('@bem/cell');
* const BemEntityName = require('@bem/entity-name');
* const buttonCell = new BemCell({ entity: new BemEntityName({ block: 'button' }), tech: 'css', layer: 'desktop' });
* const buttonCell1 = new BemCell({ entity: new BemEntityName({ block: 'button' }), tech: 'css', layer: 'desktop' });
* const buttonCell2 = new BemCell({ entity: new BemEntityName({ block: 'button' }), tech: 'css', layer: 'desktop' });
* const inputCell = new BemCell({ entity: new BemEntityName({ block: 'input' }), tech: 'css', layer: 'common' });
*
* buttonCell.isEqual(buttonCell); // true
* buttonCell.isEqual(inputCell); // false
* buttonCell1.isEqual(buttonCell2); // true
* buttonCell1.isEqual(inputCell); // false
*/
isEqual(cell) {
return (cell.tech === this.tech) && (cell.layer === this.layer) && cell.entity.isEqual(this.entity);
Expand All @@ -251,7 +250,6 @@ module.exports = class BemCell {
* Determines whether specified cell is instance of BemCell.
*
* @param {BemCell} cell - the cell to check.
*
* @returns {boolean} A Boolean indicating whether or not specified entity is instance of BemCell.
* @example
* const BemCell = require('@bem/cell');
Expand Down Expand Up @@ -282,7 +280,6 @@ module.exports = class BemCell {
* @param {string} [obj.modVal] — the modifier value of entity. Used if neither `mod.val` nor `val` were not specified.
* @param {string} [obj.tech] — technology of cell.
* @param {string} [obj.layer] — layer of cell.
*
* @returns {BemCell} An object representing cell.
* @example
* const BemCell = require('@bem/cell');
Expand Down
29 changes: 15 additions & 14 deletions test/is-equal.test.js
@@ -1,37 +1,38 @@
const test = require('ava');
const BemEntityName = require('@bem/entity-name');

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

test('should detect equal cell', t => {
const cell = new BemCell({ entity: new BemEntityName({ block: 'button' }), tech: 'css', layer: 'desktop' });
const cell1 = BemCell.create({ entity: { block: 'button' }, tech: 'css', layer: 'desktop' });
const cell2 = BemCell.create({ entity: { block: 'button' }, tech: 'css', layer: 'desktop' });

t.true(cell.isEqual(cell))
t.true(cell1.isEqual(cell2))
});

test('should not detect equal cell by entity', t => {
const cell1 = new BemCell({ entity: new BemEntityName({ block: 'button' }), tech: 'css', layer: 'desktop' });
const cell2 = new BemCell({ entity: new BemEntityName({ block: 'input' }), tech: 'css', layer: 'desktop' });
test('should detect that cells are not equal by entity', t => {
const cell1 = BemCell.create({ entity: { block: 'button' }, tech: 'css', layer: 'desktop' });
const cell2 = BemCell.create({ entity: { block: 'input' }, tech: 'css', layer: 'desktop' });

t.false(cell1.isEqual(cell2));
});

test('should detect equal cell without tech and layer', t => {
const cell = new BemCell({ entity: new BemEntityName({ block: 'button' }) });
const cell1 = BemCell.create({ entity: { block: 'button' } });
const cell2 = BemCell.create({ entity: { block: 'button' } });

t.true(cell.isEqual(cell));
t.true(cell1.isEqual(cell2));
});

test('should not detect equal cell by tech', t => {
const cell1 = new BemCell({ entity: new BemEntityName({ block: 'button' }), tech: 'css', layer: 'desktop' });
const cell2 = new BemCell({ entity: new BemEntityName({ block: 'button' }), tech: 'js', layer: 'desktop' });
test('should detect that cells are not equal by tech', t => {
const cell1 = BemCell.create({ entity: { block: 'button' }, tech: 'css', layer: 'desktop' });
const cell2 = BemCell.create({ entity: { block: 'button' }, tech: 'js', layer: 'desktop' });

t.false(cell1.isEqual(cell2));
});

test('should not detect equal cell by layer', t => {
const cell1 = new BemCell({ entity: new BemEntityName({ block: 'button' }), tech: 'css', layer: 'desktop' });
const cell2 = new BemCell({ entity: new BemEntityName({ block: 'button' }), tech: 'css', layer: 'touch' });
test('should detect that cells are not equal by layer', t => {
const cell1 = BemCell.create({ entity: { block: 'button' }, tech: 'css', layer: 'desktop' });
const cell2 = BemCell.create({ entity: { block: 'button' }, tech: 'css', layer: 'touch' });

t.false(cell1.isEqual(cell2));
});

0 comments on commit 748a5c1

Please sign in to comment.