Skip to content

Commit

Permalink
Merge 813c78b into e7853c1
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Abramov committed Aug 10, 2014
2 parents e7853c1 + 813c78b commit dfc46c0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -69,7 +69,7 @@ API

### `validate(str)`

Checks whether string `str` to be parsed in BEM-naming object.
Checks a string to be valid BEM notation.

Example:

Expand Down Expand Up @@ -223,9 +223,9 @@ To use your own naming convention to define strings that represent BEM-entities

Constructor `BEMNaming` gets the object from the following options:

* **String** `modSeparator`sepatates names and values of modifiers from blocks and elements. Default as `_`.
* **String** `modSeparator`separates names and values of modifiers from blocks and elements. Default as `_`.
* **String** `elemSeparator` — separates element's name from block. Default as `__`.
* **String** `literal` — defines which symbols can be used as block, element and modifier's names. Default as `[a-zA-Z0-9-]`.
* **String** `literal` — defines which symbols can be used for block, element and modifier's names. Default as `[a-zA-Z0-9-]`.

Example:

Expand Down
58 changes: 58 additions & 0 deletions lib/bem-naming.js
Expand Up @@ -4,6 +4,19 @@ var LITERAL = '[a-zA-Z0-9-]',
MOD_SEPARATOR = '_',
ELEM_SEPARATOR = '__';

/**
* BEMNaming allows getting information about BEM-entity using string as well as forming string
* representation based on BEM-naming.
*
* @param {Object} [options]
* @param {Object} [options.modSeparator='_'] Separates names and values of modifiers from blocks and elements.
* @param {Object} [options.elemSeparator='__'] Separates element's name from block.
* @param {Object} [options.literal='[a-zA-Z0-9-]'] Defines which symbols can be used for block,
* element and modifier's names.
*
* @name BEMNaming
* @constructor
*/
var BEMNaming = function (options) {
options || (options = {});

Expand All @@ -14,34 +27,70 @@ var BEMNaming = function (options) {
this._buildRegex();
};

/**
* Checks a string to be valid BEM notation.
*
* @param {String} str String representation of BEM-entity.
* @returns {Boolean}
*/
BEMNaming.prototype.validate = function (str) {
return this._regex.test(str);
};

/**
* Checks whether BEM-naming object or string is a block.
*
* @param {Object|String} obj BEM-naming object or string representation of BEM-entity.
* @returns {Boolean}
*/
BEMNaming.prototype.isBlock = function (obj) {
obj = this._parse(obj);

return !!obj.block && !obj.modName && !obj.elem;
};

/**
* Checks whether BEM-naming object or string is modifier of a block.
*
* @param {Object|String} obj BEM-naming object or string representation of BEM-entity.
* @returns {Boolean}
*/
BEMNaming.prototype.isBlockMod = function (obj) {
obj = this._parse(obj);

return !!(obj.block && obj.modName && (!obj.hasOwnProperty('modVal') || obj.modVal)) && !obj.elem;
};

/**
* Checks whether BEM-naming object or string is element of a block.
*
* @param {Object|String} obj BEM-naming object or string representation of BEM-entity.
* @returns {Boolean}
*/
BEMNaming.prototype.isElem = function (obj) {
obj = this._parse(obj);

return !!(obj.block && obj.elem) && !obj.modName;
};

/**
* Checks whether BEM-naming object or string is element of a block.
*
* @param {Object|String} obj BEM-naming object or string representation of BEM-entity.
* @returns {Boolean}
*/
BEMNaming.prototype.isElemMod = function (obj) {
obj = this._parse(obj);

return !!(obj.block && obj.elem && obj.modName && (!obj.hasOwnProperty('modVal') || obj.modVal));
};

/**
* Parses string into BEM-naming.
*
* @param {String} str String representation of BEM-entity.
* @returns {Object|undefined}
*/
BEMNaming.prototype.parse = function (str) {
var executed = this._regex.exec(str);

Expand All @@ -66,6 +115,12 @@ BEMNaming.prototype.parse = function (str) {
return notation;
};

/**
* Forms a string according to BEM-naming object.
*
* @param obj BEM-naming object
* @returns {String}
*/
BEMNaming.prototype.stringify = function (obj) {
if (!obj || !obj.block) {
throw new Error('The field `block` is undefined. It is impossible to stringify BEM notation.');
Expand Down Expand Up @@ -122,18 +177,21 @@ var defineAsGlobal = true,
isElemMod: function () { return originalNaming.isElemMod.apply(originalNaming, arguments); }
};

// Node.js
if (typeof exports === 'object') {
module.exports = bemNaming;
defineAsGlobal = false;
}

// YModules
if (typeof modules === 'object') {
modules.define('bem-naming', function (provide) {
provide(bemNaming);
});
defineAsGlobal = false;
}

// AMD
if (typeof define === 'function') {
define(function (require, exports, module) {
module.exports = bemNaming;
Expand Down

0 comments on commit dfc46c0

Please sign in to comment.