Skip to content

Commit

Permalink
Merge pull request #904 from /issues/890@v3
Browse files Browse the repository at this point in the history
i-bem: setMod/hasMod cast non-boolean mod values to string
  • Loading branch information
Vladimir Varankin committed May 6, 2015
2 parents f4a6f95 + 47a3778 commit 8a223d7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
30 changes: 28 additions & 2 deletions common.blocks/i-bem/i-bem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ describe('i-bem', function() {
.getMod('mod1')
.should.be.true;
});

it('should cast non-boolean mod value to string', function() {
block
.setMod('mod1', 0)
.getMod('mod1').should.be.equal('0');
});
});

describe('delMod', function() {
Expand Down Expand Up @@ -187,14 +193,14 @@ describe('i-bem', function() {
block.hasMod('mod1').should.be.true;
});

it('in short form should return true for empty mod\'s value', function() {
it('in short form should return false for empty mod\'s value', function() {
block
.setMod('mod1', '')
.hasMod('mod1')
.should.be.false;
});

it('in short form should return true for undefined mod', function() {
it('in short form should return false for undefined mod', function() {
block.hasMod('mod4').should.be.false;
});

Expand All @@ -205,6 +211,26 @@ describe('i-bem', function() {

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

it('should not treat passed but undefined mod value as a short form', function() {
var modVal;
block.hasMod('mod1', modVal).should.be.false;
});

it('should treat defined non-boolean mod value as a string', function() {
block
.setMod('mod1', 0)
.hasMod('mod1', 0)
.should.be.true;

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

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

describe('toggleMod', function() {
Expand Down
16 changes: 11 additions & 5 deletions common.blocks/i-bem/i-bem.vanilla.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,13 @@ var BemEntity = inherit(events.Emitter, /** @lends BemEntity.prototype */ {
/**
* Checks whether a BEM entity has a modifier
* @param {String} modName Modifier name
* @param {String} [modVal] Modifier value
* @param {String|Boolean} [modVal] Modifier value. If not of type String or Boolean, it is casted to String
* @returns {Boolean}
*/
hasMod : function(modName, modVal) {
var typeModVal = typeof modVal;
typeModVal === 'undefined' || typeModVal === 'boolean' || (modVal = modVal.toString());

var res = this.getMod(modName) === (modVal || '');
return arguments.length === 1? !res : res;
},
Expand All @@ -280,14 +283,17 @@ var BemEntity = inherit(events.Emitter, /** @lends BemEntity.prototype */ {
/**
* Sets the modifier for a BEM entity
* @param {String} modName Modifier name
* @param {String} [modVal=true] Modifier value
* @param {String|Boolean} [modVal=true] Modifier value. If not of type String or Boolean, it is casted to String
* @returns {BemEntity} this
*/
setMod : function(modName, modVal) {
if(typeof modVal === 'undefined') {
var typeModVal = typeof modVal;
if(typeModVal === 'undefined') {
modVal = true;
} else if(modVal === false) {
modVal = '';
} else if(typeModVal === 'boolean') {
modVal === false && (modVal = '');
} else {
modVal = modVal.toString();
}

if(this._processingMods[modName]) return this;
Expand Down

0 comments on commit 8a223d7

Please sign in to comment.