Skip to content

Instantly share code, notes, and snippets.

@guilhermehn
Created June 9, 2019 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermehn/a3fcf158fea1ede3e1fae474f22e73d9 to your computer and use it in GitHub Desktop.
Save guilhermehn/a3fcf158fea1ede3e1fae474f22e73d9 to your computer and use it in GitHub Desktop.
classnames package in four lines with Ramda
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