Skip to content

Instantly share code, notes, and snippets.

@st8998
Created May 1, 2016 09:48
Show Gist options
  • Save st8998/64ae680511624b7876cee0231be29960 to your computer and use it in GitHub Desktop.
Save st8998/64ae680511624b7876cee0231be29960 to your computer and use it in GitHub Desktop.
import { map, is, filter, keys, compose, identity, join } from 'ramda'
const processObj = compose(classnames, keys, filter(identity))
const processArr = compose(join(' '), filter(identity), map(classnames))
function classnames (arg) {
switch (true) {
case is(String, arg): return arg
case is(Array, arg): return processArr(arg)
case is(Object, arg): return processObj(arg)
default: return null
}
}
const cn = (...args) => classnames(args)
describe.only('classnames', function () {
it('merges simple strings', function () {
expect(cn('first', 'second')).to.eq('first second')
})
it('merges array simple strings', function () {
expect(cn(['first', 'second'])).to.eq('first second')
})
it('merges object of strings/predicates', function () {
expect(cn({ 'first': true, 'second': false })).to.eq('first')
})
it('merges damn complex case', function () {
expect(cn('first', { 'second': true }, [{ 'third': false, 'fourth': true }])).to.eq('first second fourth')
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment