Skip to content

Commit

Permalink
i-bem: setMod should cast non-boolean value to string
Browse files Browse the repository at this point in the history
closes #890
  • Loading branch information
Vladimir Varankin committed Mar 3, 2015
1 parent 7947ae6 commit 47414ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
21 changes: 21 additions & 0 deletions common.blocks/i-bem/i-bem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ describe('i-bem', function() {
.setMod('mod1')
.getMod('mod1').should.be.true;
});

it('should cast number mod value to string', function() {
block
.setMod('mod1', 1)
.getMod('mod1').should.be.equal('1');
});
});

describe('delMod', function() {
Expand Down Expand Up @@ -185,6 +191,21 @@ describe('i-bem', function() {
.should.be.false;
});

it('should assume number mod value as a string', function() {
block
.setMod('mod1', 1)
.hasMod('mod1', 1)
.should.be.true;

block.hasMod('mod1', '1')
.should.be.true;

block
.setMod('mod1', '2')
.hasMod('mod1', 2)
.should.be.true;
});

it('in short form should return true for undefined mod', function() {
block.hasMod('mod4').should.be.false;
});
Expand Down
12 changes: 9 additions & 3 deletions common.blocks/i-bem/i-bem.vanilla.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
* Checks whether a block or nested element has a modifier
* @param {Object} [elem] Nested element
* @param {String} modName Modifier name
* @param {String} [modVal] Modifier value
* @param {String|Number|Boolean} [modVal] Modifier value
* @returns {Boolean}
*/
hasMod : function(elem, modName, modVal) {
Expand All @@ -283,6 +283,8 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
}
}

typeof modVal === 'number' && (modVal = String(modVal));

var res = this.getMod(elem, modName) === modVal;
return invert? !res : res;
},
Expand Down Expand Up @@ -344,7 +346,7 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
* Sets the modifier for a block/nested element
* @param {Object} [elem] Nested element
* @param {String} modName Modifier name
* @param {String} modVal Modifier value
* @param {String|Number|Boolean} [modVal=true] Modifier value
* @returns {BEM} this
*/
setMod : function(elem, modName, modVal) {
Expand All @@ -361,7 +363,11 @@ var BEM = inherit(events.Emitter, /** @lends BEM.prototype */ {
}

if(!elem || elem[0]) {
modVal === false && (modVal = '');
if(modVal === false) {
modVal = '';
} else if(typeof modVal === 'number') {
modVal = String(modVal);
}

var modId = (elem && elem[0]? identify(elem[0]) : '') + '_' + modName;

Expand Down

0 comments on commit 47414ea

Please sign in to comment.