npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bem/sdk.naming.entity

v0.2.11

Published

Manage naming of BEM entities

Downloads

110

Readme

naming.entity

The tool for working with BEM entity representations:

NPM Status

Introduction

This package combines the capabilities of the following packages:

Try naming.entity

An example is available in the RunKit editor.

Quick start

Attention. To use @bem/sdk.naming.entity, you must install Node.js 8.0+.

To run the @bem/sdk.naming.entity package:

Installing the @bem/sdk.naming.entity package

To install the @bem/sdk.naming.entity package, run the following command:

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

Creating a naming.entity instance

To create a naming.entity instance, insert the following lines into your code:

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

By default, the created instance is based on the origin preset that represents the default naming convention for BEM entities. To use another preset, see Using the specified naming convention.

Using the created instance

Now you can use the created instance to parse and stringify BEM entity name representations.

Parse a string representation

bemNaming.parse('my-block__my-element');

This code will return the BemEntityName object with the block name my-block and the element name my-element.

Stringify an object representation

bemNaming.stringify({ block: 'my-block', mod: 'my-modifier' });

This code will return the string my-block_my-modifier.

API Reference

bemNaming()

This function creates a naming.entity instance with the parse() and stringify() functions.

/**
 * @typedef INamingConventionDelims
 * @property {string} elem — Separates an element name from block.
 * @property {string|Object} mod — Separates a modifier name and the value of a modifier.
 * @property {string} mod.name — Separates a modifier name from a block or an element.
 * @property {string|boolean} mod.val — Separates the value of a modifier from the modifier name.
 */

/**
 * @param {(Object|string)} [options] — User options or the name of the preset to return.
 *                                      If not specified, the default preset will be used.
 * @param {string} [options.preset] — Preset name that should be used as the default preset.
 * @param {Object} [options.delims] — Strings to separate names of bem entities.
 *                                    This object has the same structure as `INamingConventionDelims`,
 *                                    but all properties inside are optional.
 * @param {string} [options.wordPattern] — A regular expression that will be used to match an entity name.
 * @returns {Object} — Created instance with the `parse()` and `stringify()` functions.
 */
create(options);

Examples:

const defaultNaming = require('@bem/sdk.naming.entity');
const reactNaming = require('@bem/sdk.naming.entity')('react');
const customNaming = require('@bem/sdk.naming.entity'){ wordPattern: '[a-z]+' };

See more examples in the Parameter tuning section.

parse()

Parses the string with a BEM entity name into an object representation.

/**
 * @typedef BemEntityName
 * @property {string} block — Block name.
 * @property {string} [elem] — Element name.
 * @property {string|Object} [mod] — Modifier name or object with name and value.
 * @property {string} mod.name — Modifier name.
 * @property {string} [mod.val=true] — Modifier value.
 */

/**
 * @param {string} str — String representation of a BEM entity.
 * @returns {(BemEntityName|undefined)}
 */
parse(str);

Example:

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

bemNaming.parse('my-block__my-element_my-modifier_some-value');
// => BemEntityName {
//     block: 'my-block',
//     elem: 'my-element',
//     mod: { name: 'my-modifier', val: 'some-value' }
// }

For more information about the parse() function, see the @bem/sdk.naming.parse package documentation.

stringify()

Forms a string from the object that specifies a BEM entity name.

/**
 * @typedef BemEntityName
 * @property {string} block — Block name.
 * @property {string} [elem] — Element name.
 * @property {string|Object} [mod] — Modifier name or object with name and value.
 * @property {string} mod.name — Modifier name.
 * @property {string|boolean} [mod.val] — Modifier value.
 */

/**
 * @param {object|BemEntityName} entity — Object representation of a BEM entity.
 * @returns {string} — BEM entity name. This name can be used in class attributes.
 */
stringify(entity);

Example:

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

const bemEntityName = {
    block: 'my-block',
    elem: 'my-element',
    mod: { name: 'my-modifier', val: 'some-value' }
}

console.log(bemNaming.stringify(bemEntityName));
// => my-block__my-element_my-modifier_some-value

For more information about the stringify() function, see the @bem/sdk.naming.stringify package documentation.

Parameter tuning

Using a specified naming convention

The @bem/sdk.naming.presets package provides presets with various naming conventions.

Specify the name of a preset to use in the bemNaming() function. See the full list of supported presets in the package documentation.

Example:

const createBemNaming = require('@bem/sdk.naming.entity');
const myEntity = {
    block: 'my-block',
    elem: 'my-element',
    mod: {
        name: 'my-modifier',
        val: 'some-value'
    }
};

// Create the new instance from the `two-dashes` preset.
const twoDashes = createBemNaming('two-dashes');
twoDashes.stringify(myEntity);
// => my-block__my-element--my-modifier_some-value

// Create an instance from the `react` preset.
const react = createBemNaming('react');
react.stringify(myEntity);
// => my-block-my-element_my-modifier_some-value

RunKit live example.

Using a custom naming convention

To use a custom naming convention, create an object that will overwrite the default naming convention parameters. Pass this object in the bemNaming() function.

For example, overwrite the modifier value delimiter and use the equal sign (=) as the delimiter.

Example:

const createBemNaming = require('@bem/sdk.naming.entity');
const myNamingOptions = {
    delims: {
        mod: { val: '=' }
    }
};
const myNaming = createBemNaming(myNamingOptions);

// Parse a BEM entity name to test created instance.
myNaming.parse('my-block_my-modifier=some-value');
/**
 * => BemEntityName {
 *  block: 'my-block',
 *  mod: { name: 'my-modifier', val: 'some-value' } }
 */

// Stringify an object representation of the BEM entity name.
const myEntity = {
    block: 'my-block',
    elem: 'my-element',
    mod: {
        name: 'my-modifier',
        val: 'some-value'
    }
};
myNaming.stringify(myEntity);
// => my-block__my-element_my-modifier=some-value

RunKit live example.

Using another preset as default

The default preset is origin, but you can set another preset as default in the options.preset parameter.

For example, set the two-dashes preset as the default and create a naming.entity instance based on it.

Example:

const createBemNaming = require('@bem/sdk.naming.entity');
const myNamingOptions = {
    preset: 'two-dashes',
    delims: {
        mod: { val: '=' }
    }
};

const myNaming = createBemNaming(myNamingOptions);

// Parse a BEM entity name to test created preset.
myNaming.parse('my-block--my-modifier=some-value');
/**
 * => BemEntityName {
 * block: 'my-block',
 * mod: { name: 'my-modifier', val: 'some-value' } }
 */

// Stringify an object representation of the BEM entity name.
const myEntity = {
    block: 'my-block',
    elem: 'my-element',
    mod: {
        name: 'my-modifier',
        val: 'some-value'
    }
};
myNaming.stringify(myEntity);
// => my-block__my-element--my-modifier=some-value

RunKit live example.

Usage examples

Convert a string to the Two Dashes style

In this example, we will convert the string from the origin naming convention to Two Dashes.

Origin: my-block__my-element_my-modifier_some-value

Two Dashes: my-block__my-element--my-modifier_some-value

Example:

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

const bemEntityNameStr = 'my-block__my-element_my-modifier_some-value'

const bemEntityNameObj = originNaming.parse(bemEntityName);
// => BemEntityName {
//     block: 'my-block',
//     elem: 'my-element',
//     mod: { name: 'my-modifier', val: 'some-value' }
// }

twoDashesNaming.stringify(bemEntityNameObj);
// => my-block__my-element--my-modifier_some-value

RunKit live example.