diff --git a/README.md b/README.md index 3f3d819..5d06a46 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ API * [tech](#tech) * [layer](#layer) * [id](#id) +* [isBemCell(cell)](#isbemcellcell) ### constructor(obj) @@ -129,6 +130,26 @@ const cell = new BemCell({ cell.id; // ➜ "button__text@desktop.css" ``` +### isBemCell(cell) + +Determines whether specified cell is instance of BemCell. + +Parameter | Type | Description +----------|-----------------|----------------------- +`cell` | `BemCell` | The cell to check. + +```js +const BemCell = require('@bem/cell'); +const BemEntityName = require('@bem/entity-name'); + +const cell = new BemCell({ + entity: new BemEntityName({ block: 'button', elem: 'text' }) +}); + +BemCell.isBemCell(cell); // true +BemCell.isBemCell({}); // false +``` + License ------- diff --git a/index.js b/index.js index 2305364..35caeaf 100644 --- a/index.js +++ b/index.js @@ -104,4 +104,25 @@ module.exports = class BemCell { return this._id; } + + /** + * 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'); + * const BemEntityName = require('@bem/entity-name'); + * + * const cell = new BemCell({ + * entity: new BemEntityName({ block: 'button', elem: 'text' }) + * }); + * + * BemCell.isBemCell(cell); // true + * BemCell.isBemCell({}); // false + */ + static isBemCell(cell) { + return cell && this.name === cell.constructor.name; + } }; diff --git a/test/is-bem-cell.test.js b/test/is-bem-cell.test.js new file mode 100644 index 0000000..ddd747e --- /dev/null +++ b/test/is-bem-cell.test.js @@ -0,0 +1,21 @@ +const test = require('ava'); +const BemEntityName = require('@bem/entity-name'); + +const BemCell = require('../index'); + +test('should check valid entities', t => { + const cell = new BemCell({ + entity: new BemEntityName({ block: 'block' }) + }); + + t.true(BemCell.isBemCell(cell)); +}); + +test('should not pass invalid blocks', t => { + t.falsy(BemCell.isBemCell({})); + t.falsy(BemCell.isBemCell([])); +}); + +test('should not pass null', t => { + t.falsy(BemCell.isBemCell(null)); +});