Created
October 16, 2019 09:25
-
-
Save joelhinz/8962dd3a65856f42c2fef54e67d0a7d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function bemify(block, modifiers) { | |
if (!modifiers) { | |
return block; | |
} | |
if (typeof modifiers == 'object' && !Array.isArray(modifiers)) { | |
modifiers = Object.keys(modifiers).filter(modifier => modifiers[modifier]) | |
} | |
if (typeof modifiers == 'string') { | |
modifiers = modifiers.split(' ') | |
} | |
return block + ' ' + modifiers.map(modifier => `${block}--${modifier}`).join(' '); | |
} | |
console.log(bemify('button', null)) | |
// button | |
console.log(bemify('button', [])) | |
// button | |
console.log(bemify('button', 'red close')) | |
// button button--red button--close | |
console.log(bemify('button', ['red', 'close'])) | |
// button button--red button--close | |
console.log(bemify('button', { | |
red: true, | |
close: true, | |
nope: false | |
})) | |
// button button--red button--close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment