Created
May 1, 2016 09:48
-
-
Save st8998/64ae680511624b7876cee0231be29960 to your computer and use it in GitHub Desktop.
This file contains 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
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