Join Class Names Conditionally

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Write classNames(...args), which builds a class attribute from its arguments, like the classnames package. A string adds its classes: it may hold several, separated by spaces, and extra spaces are ignored. An object adds each key whose value is truthy, in key order. An array adds its items by the same rules, nested to any depth. Falsy arguments, such as false, null, undefined, 0 and "", add nothing. Return the classes joined by single spaces, each one only once, where it first appears; with no classes, return "".

Your code must pass

  • Joins strings and skips falsy values
  • Adds object keys whose values are truthy
  • Flattens nested arrays
  • Lists each class once, where it first appears