Skip to content

Commit

Permalink
implemented a lite version of the bem-cn
Browse files Browse the repository at this point in the history
  • Loading branch information
mistakster committed Jun 18, 2016
1 parent 4a0a01c commit 1044fa8
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.idea
*.iml
node_modules
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";

var bemClassName = require('bem-cn').default;

module.exports = function (blockName) {
var b = bemClassName(blockName);

function element(elementName, modifiers, mixin) {
// normalize arguments
if (typeof elementName === 'string') {
// going to create an element
if (typeof modifiers === 'string') {
mixin = modifiers;
modifiers = null;
}
} else {
// going to create a block
mixin = modifiers;
modifiers = null;
}

var result = b(elementName, modifiers);

return (mixin ? result.mix(mixin) : result).toString();
}

element.builder = function () {
return b;
};

return element;
};
24 changes: 22 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,28 @@
"description": "Lite version of the friendly BEM-style class name generator, which is great for React",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha tests/*-spec.js"
},
"author": "Vladimir Kuznetsov <mistakster@gmail.com>",
"license": "MIT"
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/mistakster/bem-cn-lite.git"
},
"keywords": [
"BEM",
"React",
"class",
"classname",
"block",
"element",
"modifier"
],
"dependencies": {
"bem-cn": "^2.0.0"
},
"devDependencies": {
"mocha": "^2.5.3",
"should": "^9.0.2"
}
}
39 changes: 39 additions & 0 deletions tests/block-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

require('should');

var generator = require('../index');

var b = generator('block');

describe('module', function () {
it('should generate block', function () {
var block = b();

block.should.equal('block');
});

it('should generate block with modifier', function () {
var block = b({modifier: true});

block.should.equal('block block_modifier');
});

it('should generate block with modifier and value', function () {
var block = b({modifier: 'value'});

block.should.equal('block block_modifier_value');
});

it('should generate block with mixin', function () {
var block = b(null, 'mixin');

block.should.equal('block mixin');
});

it('should generate block with modifier and mixin', function () {
var block = b({modifier: 'value'}, 'mixin');

block.should.equal('block block_modifier_value mixin');
});
});
39 changes: 39 additions & 0 deletions tests/element-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";

require('should');

var generator = require('../index');

var b = generator('block');

describe('module', function () {
it('should generate element', function () {
var element = b('element');

element.should.equal('block__element');
});

it('should generate element with modifier', function () {
var element = b('element', {modifier: true});

element.should.equal('block__element block__element_modifier');
});

it('should generate element with modifier and value', function () {
var element = b('element', {modifier: 'value'});

element.should.equal('block__element block__element_modifier_value');
});

it('should generate element with mixin', function () {
var element = b('element', 'mixin');

element.should.equal('block__element mixin');
});

it('should generate element with modifier and mixin', function () {
var element = b('element', {modifier: 'value'}, 'mixin');

element.should.equal('block__element block__element_modifier_value mixin');
});
});

0 comments on commit 1044fa8

Please sign in to comment.