@bem/entity-name

中文文档
1.5.0发布于 2017/04/22MPL-2.0TypeScriptCommonJS近 30 天下载 2110

BEM entity name representation

原始使用文档
文档目录24 节
  1. Contents
  2. Install
  3. Usage
  4. API
  5. constructor({ block, elem, mod })
  6. block
  7. elem
  8. mod
  9. type
  10. scope
  11. id
  12. isSimpleMod()
  13. isEqual(entityName)
  14. belongsTo(entityName)
  15. valueOf()
  16. toJSON()
  17. toString()
  18. static create(object)
  19. static isBemEntityName(entityName)
  20. Serialization
  21. TypeScript support
  22. Debuggability
  23. Deprecation
  24. License

BemEntityName

BEM entity name representation.

NPM Status Travis Status Coverage Status Dependency Status

Contents

Install

$ npm install --save @bem/entity-name

Usage

const BemEntityName = require('@bem/entity-name');

const entityName = new BemEntityName({ block: 'button', elem: 'text' });

entityName.block; // button
entityName.elem;  // text
entityName.mod;   // undefined

entityName.id;   // button__elem
entityName.type; // elem

entityName.isEqual(new BemEntityName({ block: 'button' }));               // false
entityName.isEqual(new BemEntityName({ block: 'button', elem: 'text' })); // true

API

constructor({ block, elem, mod })

ParameterTypeDescription
blockstringThe block name of entity.
elemstringThe element name of entity.
modstring, objectThe modifier of entity.

If specified value is string then it will be equivalent to { name: string, val: true }. Optional.
mod.namestringThe modifier name of entity.
mod.valstring, trueThe modifier value of entity. Optional.

BEM entities can be defined with a help of JS object with the following fields:

  • block — a block name. The field is required because only a block exists as an independent BEM entity
  • elem — an element name.
  • mod — a modifier.

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.

const BemEntityName = require('@bem/entity-name');

// The modifier of block
new BemEntityName({
    block: 'button',
    mod: { name: 'view', val: 'action' }
});

// Not valid modifier
new BemEntityName({
    block: 'button',
    mod: { val: 'action' }
});
// ➜ EntityTypeError: the object `{ block: 'block', mod: { val: 'action' } }` is not valid BEM entity, the field `mod.name` is undefined

To describe a simple modifier the mod.val field must be omitted.

// Simple modifier of a block
new BemEntityName({ block: 'button', mod: 'focused' });

// Is equivalent to simple modifier, if `mod.val` is `true`
new BemEntityName({
    block: 'button',
    mod: { name: 'focused', val: true }
});

block

The name of block to which this entity belongs.

const BemEntityName = require('@bem/entity-name');
const name = new BemEntityName({ block: 'button' });

name.block; // button

elem

The element name of this entity.

If entity is not element or modifier of element then returns empty string.

const BemEntityName = require('@bem/entity-name');
const name = new BemEntityName({ block: 'button', elem: 'text' });

name.elem; // text

mod

The modifier of this entity.

Important: If entity is not a modifier then returns undefined.

const BemEntityName = require('@bem/entity-name');

const blockName = new BemEntityName({ block: 'button' });
const modName = new BemEntityName({ block: 'button', mod: 'disabled' });

modName.mod;   // { name: 'disabled', val: true }
blockName.mod; // undefined

type

The type for this entity.

Possible values: block, elem, blockMod, elemMod.

const BemEntityName = require('@bem/entity-name');

const elemName = new BemEntityName({ block: 'button', elem: 'text' });
const modName = new BemEntityName({ block: 'menu', elem: 'item', mod: 'current' });

elemName.type; // elem
modName.type;  // elemMod

scope

The scope of this entity.

Important: block-typed entities has no scope.

const BemEntityName = require('@bem/entity-name');

const buttonName = new BemEntityName({ block: 'button' });
const buttonTextName = new BemEntityName({ block: 'button', elem: 'text' });
const buttonTextBoldName = new BemEntityName({ block: 'button', elem: 'text', mod: 'bold' });

buttonName.scope;         // null
buttonTextName.scope;     // BemEntityName { block: 'button' }
buttonTextBoldName.scope; // BemEntityName { block: 'button', elem: 'elem' }

id

The id for this entity.

Important: should only be used to determine uniqueness of entity.

If you want to get string representation in accordance with the provisions naming convention you should use @bem/naming package.

const BemEntityName = require('@bem/entity-name');
const name = new BemEntityName({ block: 'button', mod: 'disabled' });

name.id; // button_disabled

isSimpleMod()

Determines whether modifier simple or not.

NOTE: For entity without modifier isSimpleMod() returns null.

const BemEntityName = require('@bem/entity-name');
const modName = new BemEntityName({ block: 'button', mod: { name: 'theme' } });
const modVal = new BemEntityName({ block: 'button', mod: { name: 'theme', val: 'normal' } });
const block = new BemEntityName({ block: 'button' });

modName.isSimpleMod(); // true
modVal.isSimpleMod(); // false
block.isSimpleMod(); // null

isEqual(entityName)

ParameterTypeDescription
entityNameBemEntityNameThe entity to compare.

Determines whether specified entity is the deepEqual entity.

const BemEntityName = require('@bem/entity-name');

const inputName = new BemEntityName({ block: 'input' });
const buttonName = new BemEntityName({ block: 'button' });

inputName.isEqual(buttonName);  // false
buttonName.isEqual(buttonName); // true

belongsTo(entityName)

ParameterTypeDescription
entityNameBemEntityNameThe entity to compare.

Determines whether specified entity belongs to this.

const BemEntityName = require('@bem/entity-name');

const buttonName = new BemEntityName({ block: 'button' });
const buttonTextName = new BemEntityName({ block: 'button', elem: 'text' });
const buttonTextBoldName = new BemEntityName({ block: 'button', elem: 'text', mod: 'bold' });

buttonTextName.belongsTo(buttonName);         // true
buttonName.belongsTo(buttonTextName);         // false
buttonTextBoldName.belongsTo(buttonTextName); // true
buttonTextBoldName.belongsTo(buttonName);     // false

valueOf()

Returns normalized object representing the entity name.

const BemEntityName = require('@bem/entity-name');
const name = new BemEntityName({ block: 'button', mod: 'focused' });

name.valueOf();

// ➜ { block: 'button', mod: { name: 'focused', value: true } }

toJSON()

Returns raw data for JSON.stringify() purposes.

const BemEntityName = require('@bem/entity-name');

const name = new BemEntityName({ block: 'input', mod: 'available' });

JSON.stringify(name); // {"block":"input","mod":{"name":"available","val":true}}

toString()

Returns string representing the entity name.

Important: if you want to get string representation in accordance with the provisions naming convention you should use @bem/naming package.

const BemEntityName = require('@bem/entity-name');
const name = new BemEntityName({ block: 'button', mod: 'focused' });

name.toString(); // button_focused

static create(object)

Creates BemEntityName instance by any object representation or a string.

Helper for sugar-free simplicity.

ParameterTypeDescription
objectobject, stringRepresentation of entity name.

Passed Object could have the common field names for entities:

Object fieldTypeDescription
blockstringThe block name of entity.
elemstringThe element name of entity. Optional.
modstring, objectThe modifier of entity.

If specified value is string then it will be equivalent to { name: string, val: true }. Optional.
valstringThe modifier value of entity. Used if mod is a string. Optional.
mod.namestringThe modifier name of entity. Optional.
mod.valstring, trueThe modifier value of entity. Optional.
modNamestringThe modifier name of entity. Used if mod.name was not specified. Optional.
modValstring, trueThe modifier value of entity. Used if neither mod.val nor val were not specified. Optional.
const BemEntityName = require('@bem/entity-name');

BemEntityName.create('my-button');
BemEntityName.create({ block: 'my-button' });
// ➜ BemEntityName { block: 'my-button' }

BemEntityName.create({ block: 'my-button', mod: 'theme', val: 'red' });
BemEntityName.create({ block: 'my-button', modName: 'theme', modVal: 'red' });
// ➜ BemEntityName { block: 'my-button', mod: { name: 'theme', val: 'red' } }

BemEntityName.create({ block: 'my-button', mod: 'focused' });
// ➜ BemEntityName { block: 'my-button', mod: { name: 'focused', val: true } }

static isBemEntityName(entityName)

Determines whether specified entity is an instance of BemEntityName.

ParameterTypeDescription
entityName*The entity to check.
const BemEntityName = require('@bem/entity-name');

const entityName = new BemEntityName({ block: 'input' });

BemEntityName.isBemEntityName(entityName); // true
BemEntityName.isBemEntityName({ block: 'button' }); // false

Serialization

The BemEntityName has toJSON method to support JSON.stringify() behaviour.

Use JSON.stringify to serialize an instance of BemEntityName.

const BemEntityName = require('@bem/entity-name');

const name = new BemEntityName({ block: 'input', mod: 'available' });

JSON.stringify(name); // {"block":"input","mod":{"name":"available","val":true}}

Use JSON.parse to deserialize JSON string and create an instance of BemEntityName.

const BemEntityName = require('@bem/entity-name');

const str = '{"block":"input","mod":{"name":"available","val":true}}';

new BemEntityName(JSON.parse(str)); // BemEntityName({ block: 'input', mod: 'available' });

TypeScript support

The package includes typings for TypeScript. You have to set up transpilation yourself. When you set module to commonjs in your tsconfig.json file, TypeScript will automatically find the type definitions for @bem/entity-name.

The interfaces are provided in global namespace BEMSDK.EntityName. It is necessary to use interfaces in JsDoc.

Debuggability

In Node.js, console.log() calls util.inspect() on each argument without a formatting placeholder.

BemEntityName has inspect() method to get custom string representation of the object.

const BemEntityName = require('@bem/entity-name');

const name = new BemEntityName({ block: 'input', mod: 'available' });

console.log(name);

// ➜ BemEntityName { block: 'input', mod: { name: 'available' } }

You can also convert BemEntityName object to string.

const BemEntityName = require('@bem/entity-name');

const name = new BemEntityName({ block: 'input', mod: 'available' });

console.log(`name: ${name}`);

// ➜ name: input_available

Deprecation

Deprecation is performed with depd.

To silencing deprecation warnings from being output use the NO_DEPRECATION environment variable.

NO_DEPRECATION=@bem/entity-name node app.js

More details in depd documentation

License

Code and documentation © 2016 YANDEX LLC. Code released under the Mozilla Public License 2.0.

版本与发布记录

@bem/entity-name 共发布 9 个版本,首次发布于 2016/11/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.0latest2017/04/229 年前
1.4.02017/03/269 年前
1.3.22017/03/269 年前
1.3.12017/02/179 年前
1.3.02017/02/159 年前
1.2.02017/02/019 年前

其他发布通道:beta → 2.0.0-0。

关于 @bem/entity-name 的常见问题

@bem/entity-name 是什么?

BEM entity name representation

如何安装 @bem/entity-name?

在项目目录运行 npm install @bem/entity-name,或使用 pnpm add / yarn add / bun add。

@bem/entity-name 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bem/entity-name 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bem/entity-name 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 4.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bem/entity-name 可以商用吗?

@bem/entity-name 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@bem/entity-name 的下载量有多少?

根据 npm 官方下载统计,@bem/entity-name 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,110 次,日均约 70 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bem/entity-name 还在维护吗?

最新版本 1.5.0 发布于 2017/04/22(9 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/11/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。