
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Combines multiple class names into a single space-separated string, filtering out falsy values (false, 0, "", null, undefined, NaN, 0n).
Like classnames and clsx, but accepts only strings or falsy values as arguments, and faster.
Same function as clsx/lite but slightly faster.
npm install clarr
import clarr from "clarr"; // or `import { clarr } from "clarr"`
clarr("class1", true && "class2", false && "class3", null && "class4");
// returns "class1 class2"
// in react: `<div className={clarr("class1", true && "class2")}></div>`
Type for each argument: class name string or falsy value (string | false | 0 | -0 | 0n | "" | null | undefined | NaN)
Return value: a space-separated string of valid class names
ES Module, Common JS, simple function in global scope (minified), UMD (minified), and TypeScript definition are available.
Use it directly in HTML:
<script src="https://unpkg.com/clarr/dist/clarr.global.js"></script>
<script>
clarr(<...>);
</script>
The minified, non-gzipped global scope version is 133 bytes (it is actually slightly larger when gzipped).
import clarr from "clarr"; // or `import { clarr } from "clarr"`
const three: number = 3;
const nonExistentElement = undefined;
clarr("btn", three === 6 && "three-is-six", three === 3 && "three-is-three hello", "active", nonExistentElement && "nonExistentElement-exists", "", null, "large", NaN && "nan-class", 0);
// Returns: "btn three-is-three hello active large"
Test files contain more examples if you can read them.
I compared several possible implementations/writings (including whether to cache .length as a variable, var/let/const, declare variables inside/outside the loop, for/while, the way to check whether it's the first class, the way to concatenate strings, etc.), to ensure the best performance.
Each argument is of type string or any falsy value. Among all falsy values, NaN is excluded in the type definition because it is not a valid type, unlike other falsy values such as 0 or 0n. However, this is not an issue, as the function is intended to be used in expressions like clarr(NaN && 'nan-class'), in such cases, TypeScript infers the type of NaN && 'nan-class' as 0 | "nan-class", ensuring compatibility.
Class is "as is" and will not be trimmed (e.g. " my-class "), one or several space/whitespace " " will also be kept, duplicates will not be removed.
As argument, those that would fail TypeScript check (objects like {a:1}, non-0 numbers, etc.) can still run in JavaScript, the result would be the first truthy value (which may not be string), or space-separated string of all truthy values' string forms. The reason not to check it in runtime is that we have TypeScript to check it, no need to check it again in runtime.
FAQs
Fastest className construction library
We found that clarr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.