Skip to content
Onsite

classnames Utility

MediumP3Coding

Implement classNames(...args), a utility that builds a CSS class string, like the popular classnames package.

Each argument can be:

  • a string: included if it is not empty,
  • a number: included (as text) if it is not 0 and not NaN,
  • an object: each key is included if its value is truthy,
  • an array: processed recursively with the same rules (nested arrays too),
  • anything else falsy (false, null, undefined): ignored.

The result joins the included names with a single space, in order. There is no leading or trailing space and no de-duplication. If nothing is included, return "".

Example 1

Input
classNames('btn', { active: true, disabled: false }, ['big', null])
Output
'btn active big'

Constraints

  • Total number of values <= 10^4

Hints

Run checks the 3 visible tests, Submit all 10.