Skip to content

Instantly share code, notes, and snippets.

@joelhinz
Created October 16, 2019 09:16
Show Gist options
  • Save joelhinz/6072673a3e6ed83037c52221590d42ec to your computer and use it in GitHub Desktop.
Save joelhinz/6072673a3e6ed83037c52221590d42ec to your computer and use it in GitHub Desktop.
export function bemify(block, modifiers) {
return block + ' ' + modifiersToArray(modifiers)
.map(modifier => `${block}--${modifier}`)
.join(' ');
}
function modifiersToArray(modifiers) {
if (!modifiers) {
return []
}
if (Array.isArray(modifiers)) {
return modifiers
}
if (typeof modifiers == 'string') {
return modifiers.split(' ')
}
return Object.keys(modifiers).filter(modifier => modifiers[modifier])
}
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