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

Commit 7d0f798

Browse files
blondskad0
authored andcommitted
feat: add belongsTo method
1 parent 94ebc5e commit 7d0f798

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,30 @@ class BemEntityName {
310310
return entityName && (this.id === entityName.id);
311311
}
312312

313+
/**
314+
* Determines whether specified entity belongs to this.
315+
*
316+
* @example
317+
* const BemEntityName = require('@bem/entity-name');
318+
*
319+
* const buttonName = new BemEntityName({ block: 'button' });
320+
* const buttonTextName = new BemEntityName({ block: 'button', elem: 'text' });
321+
*
322+
* buttonTextName.belongsTo(buttonName); // true
323+
* buttonName.belongsTo(buttonTextName); // false
324+
*
325+
* @param {BemEntityName} entityName - the entity to compare.
326+
*
327+
* @returns {boolean}
328+
*/
329+
belongsTo(entityName) {
330+
return entityName && entityName.id !== this.id && this.id.startsWith(entityName.id) &&
331+
(entityName.type !== 'block' || this.type !== 'elemMod') &&
332+
(!entityName.elem || this.elem === entityName.elem) &&
333+
(!entityName.modName || this.modName === entityName.modName) &&
334+
(!entityName.modVal || entityName.modVal === true || this.modVal === entityName.modVal);
335+
}
336+
313337
/**
314338
* Determines whether specified entity is instance of BemEntityName.
315339
*

0 commit comments

Comments
 (0)