classnames.js (1132B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 "use strict"; 5 6 /** 7 * Take any number of parameters and returns a space-concatenated string. 8 * If a parameter is a non-empty string, it's automatically added to the result. 9 * If a parameter is an object, for each entry, if the value is truthy, then the key 10 * is added to the result. 11 * 12 * For example: `classnames("hi", null, undefined, false, { foo: true, bar: false })` will 13 * return `"hi foo"` 14 * 15 * @param {...string|object} argss 16 * @returns String 17 */ 18 module.exports = function (...args) { 19 let className = ""; 20 21 for (const arg of args) { 22 if (!arg) { 23 continue; 24 } 25 26 if (typeof arg == "string") { 27 className += " " + arg; 28 } else if (Object(arg) === arg) { 29 // We don't test that we have an Object literal, so we can be as fast as we can 30 for (const key in arg) { 31 if (arg[key]) { 32 className += " " + key; 33 } 34 } 35 } 36 } 37 38 return className.trim(); 39 };