Skip to content

Commit

Permalink
Add is-equal method
Browse files Browse the repository at this point in the history
  • Loading branch information
skad0 committed Feb 10, 2017
1 parent 339b0ba commit 587c792
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Expand Up @@ -61,6 +61,7 @@ API
* [toString()](#tostring)
* [valueOf()](#valueof)
* [toJSON()](#tojson)
* [isEqual(cell)](#isequalcell)
* [isBemCell(cell)](#isbemcellcell)
* [create(object)](#createobject)

Expand Down Expand Up @@ -176,6 +177,28 @@ cell.valueOf();

Returns an object for `JSON.stringify()` purpose.

### isEqual(cell)

Determines whether specified cell is deep equal cell.

Parameter | Type | Description
----------|-----------------|-----------------------
`cell` | `BemCell` | The cell to compare.

```js
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 inputCell = new BemCelL({
entity: new BemEntityName({ block: 'input' }), tech: 'css', layer: 'common'
});

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

### #isBemCell(cell)

Determines whether specified cell is instance of BemCell.
Expand Down
19 changes: 19 additions & 0 deletions index.js
Expand Up @@ -228,6 +228,25 @@ module.exports = class BemCell {
return this.valueOf();
}

/**
* Determines whether specified cell is deep equal cell
*
* @param {BemCell} cell - the cell to compare
*
* @returns {Boolean} - A boolean indicating whether or not scecified 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 inputCell = new BemCelL({ entity: new BemEntityName({ block: 'input' }), tech: 'css', layer: 'common' });
*
* buttonCell.isEqual(buttonCell); // true
* buttonCell.isEqual(inputCell); // false
*/
isEqual(cell) {
return (cell.tech === this.tech) && (cell.layer === this.layer) && cell.entity.isEqual(this.entity);
}

/**
* Determines whether specified cell is instance of BemCell.
*
Expand Down
37 changes: 37 additions & 0 deletions test/is-equal.test.js
@@ -0,0 +1,37 @@
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' });

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

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' });

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

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

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

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' });

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' });

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

0 comments on commit 587c792

Please sign in to comment.