Skip to content

Instantly share code, notes, and snippets.

@joelhinz
Created October 16, 2019 09:25
Show Gist options
  • Save joelhinz/8962dd3a65856f42c2fef54e67d0a7d9 to your computer and use it in GitHub Desktop.
Save joelhinz/8962dd3a65856f42c2fef54e67d0a7d9 to your computer and use it in GitHub Desktop.
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