Created
June 9, 2019 23:32
-
-
Save guilhermehn/a3fcf158fea1ede3e1fae474f22e73d9 to your computer and use it in GitHub Desktop.
classnames package in four lines with Ramda
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
import { when, is, map, filter, compose, nth, flatten, pipe, join } from 'ramda'; | |
const isValueTruthy = compose(Boolean, nth(1)); | |
const getTruthyKeys = compose(map(nth(0)), filter(isValueTruthy), Object.entries); | |
const classnames = (...args) => pipe(flatten, filter(Boolean), map(when(is(Object), getTruthyKeys)), flatten, join(' '))(args); | |
console.log(classnames('foo', 'bar', ['baz', ['deep', [[{ nested: true , falseNested: false }]]]], '', false, { foobar: true, no: false })); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment