EN
Forum

Methodology

Toolbox

Platform

naming.entity

Tool for working with BEM entity representations: allows you to parse string representation and stringify object representation.

Supports various naming conventions: origin, two-dashes, react and allows to create your convention.

NPM Status

Install

$ npm install --save @bem/sdk.naming.entity

Usage

const bemNaming = require('@bem/sdk.naming.entity');

bemNaming.parse('button__text'); // BemEntityName { block: 'button', elem: 'text' }
bemNaming.stringify({ block: 'button', mod: 'checked' }); // String 'button_checked'

Table of Contents

BEM Entity representation

With BEM entity representation you can define block, element, block modifier and element modifier.

The representation can include name of block, name of element, name of modifier and value of modifier.

BEM entity can be represented using Object or String.

Object representation

The BemEntityName class describes the representation of a BEM entity name.

String representation

To define BEM entities, we often use a special string format that allows us to define exactly which entity is represented.

According to the original BEM naming convention, it looks like this:

'block[_block-mod-name[_block-mod-val]][__elem-name[_elem-mod-name[_elem-mod-val]]]'

(Parameters within square brackets are optional)

Delimiters

The names are separated from each other by means of special delimiters.

The original naming uses the following delimiters:

Examples

BEM Entity TypeString RepresentationBlockblock-nameBlock modifierblock-scope_mod-name_mod-valElementblock-scope__elem-nameElement modifierblock-scope__elem-scope_mod-name_mod-val

The simple modifier doesn't have value. Therefore, in the string representation the value should be omitted.

BEM Entity TypeString RepresentationBlock modifierblock-scope_mod-nameElement modifierblock-scope__elem-scope_mod-name

Common misconceptions

The BEM methodology uses a flat structure inside blocks. This means that a BEM entity can't be represented as an element of another element, and the following string representation will be invalid:

'block__some-elem__sub-elem'

For more information, see the FAQ:

Why doesn't BEM recommend using elements within elements (blockelem1elem2)?

Also, a BEM entity can't be a block modifier and an element modifier simultaneously, so the following string representation will be invalid:

'block_block-mod-name_block-mod-val__elem-name_elem-mod-name_elem-mod-val'

Naming conventions

The main idea of the naming convention is to make names of BEM entities as informative and clear as possible.

Read more in the section naming convention of the BEM methodology.

The BEM methodology provides an idea for creating naming rules and implements that idea in its canonical naming convention: origin naming convention.

However, a number of alternative schemes based on the BEM principles also exist in the world of web development:

In addition, you can invent your naming convention. How to do this, see the Custom naming convention section.

Origin naming convention

According to this convention elements are delimited with two underscores (__), modifiers and values of modifiers are delimited by one underscore (_).

Read more in the section naming convention of the BEM methodology.

Example:

const originNaming = require('@bem/sdk.naming.entity')('origin');

originNaming.parse('block__elem');    // BemEntityName { block: 'block', elem: 'elem' }
originNaming.parse('block_mod_val');  // BemEntityName { block: 'block',
                                      //                 mod: { name: 'mod', val: 'val' } }

originNaming.stringify({
    block: 'block',
    elem: 'elem',
    mod: 'mod'
});

// ➜ block__elem_mod

Harry Roberts' naming convention

According to this convention elements are delimited with two underscores (__), modifiers are delimited by two hyphens (--), and values of modifiers are delimited by one underscore (_).

Read more in the Guidelines.

Example:

const twoDashesNaming = require('@bem/sdk.naming.entity')('two-dashes');

twoDashesNaming.parse('block__elem');    // { block: 'block', elem: 'elem' }
twoDashesNaming.parse('block--mod_val'); // { block: 'block',
                                         //   mod: { name: 'mod', val: 'val' } }

twoDashesNaming.stringify({
    block: 'block',
    elem: 'elem',
    mod: 'mod'
});

// ➜ block__elem--mod

React naming convention

According to this convention elements are delimited with one hyphen (-), modifiers are delimited by one underscore (_), and values of modifiers are delimited by one underscore (_).

You can explore this convention at bem-react-components.

Example:

const reactNaming = require('@bem/sdk.naming.entity')('react');

reactNaming.parse('Block-Elem');    // BemEntityName { block: 'Block', elem: 'Elem' }
reactNaming.parse('Block_Mod_Val'); // BemEntityName { block: 'Block',
                                    //                 mod: { name: 'Mod', val: 'Val' } }

reactNaming.stringify({
    block: 'Block',
    elem: 'Elem',
    mod: 'Mod'
});

// ➜ Block-Elem_Mod

Custom naming convention

To create an instance where you can manage your own naming convention use the bemNaming function.

Example:

const createBemNaming = require('@bem/sdk.naming.entity');

const myNaming = createBemNaming({
    delims: {
        elem: '-',
        mod: { name: '--', val: '_' }
    },
    wordPattern: '[a-zA-Z0-9]+'   // because element and modifier's separators include
});                               // hyphen in it, we need to exclude it from block,
                                  // element and modifier's name

myNaming.parse('block--mod_val'); // BemEntityName
                                  // { block: 'block',
                                  //   mod: { name: 'mod', val: 'val' } }

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

myNaming.stringify(new BemEntityName({
    block: 'blockName',
    elem: 'elemName',
    mod: 'simpleElemMod'
});

// ➜ blockName-elemName--simpleElemMod

API

bemNaming({ delims: {elem, mod}, wordPattern })

ParameterTypeDescriptionDefaultdelimsobjectDefines delimeters for elem and/or modsdelims.elemstringSeparates element's name from block.__delims.modstring, { name: string, val: string }Separates modifier from block or element._delims.mod.namestringSeparates a modifier name from a block or an element._delims.mod.valstringSeparates the value of a modifier from the modifier name._wordPatternstringDefines which characters can be used in names of blocks, elements, and modifiers.[a-z0-9]+(?:-[a-z0-9]+)*

parse(str)

ParameterTypeDescriptionstrstringBEM entity name representation.

Parses the string into an instance of BemEntityName.

Example:

const bemNaming = require('@bem/sdk.naming.entity');

bemNaming.parse('block__elem_mod_val');

// ➜ BemEntityName {
//     block: 'block',
//     elem: 'elem',
//     mod: { name: 'mod', val: 'val' }
// }

stringify(entityName)

ParameterTypeDescriptionentityNameBemEntityName, objectBEM entity name representation.

Forms a string from the instance of BemEntityName.

Example:

const bemNaming = require('@bem/sdk.naming.entity');
const BemEntityName = require('@bem/sdk.entity-name');

bemNaming.stringify(new BemEntityName({
    block: 'block',
    elem: 'elem',
    mod: { name: 'mod', val: 'val' }
});

// ➜ block__elem_mod_val

delims

Strings to separate names of bem entities.

Type: Object

delims.elem

String to separate an element from a block.

Type: String

Default: __

delims.mod.name

String to separate a modifier name from a block or element.

Type: String

Default: _

delims.mod.val

String to separate a modifier value from the name of the modifier.

Type: String

Default: _

License

Code and documentation copyright 2014 YANDEX LLC. Code released under the Mozilla Public License 2.0.

Rate the article
Report an error on GitHub or correct using prose.io.