-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
20fd707
commit 7bbb000
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Returns a string with all the classes passed as arguments | ||
* Useful to compose classes in jsx | ||
* | ||
* @param classes: List of classes | ||
* @returns string | ||
* @example | ||
* | ||
* clx("a", "b") // "a b" | ||
* clx(false && "a", "b") // "b" | ||
* | ||
*/ | ||
export function clx(...classes: (boolean | string)[]): string { | ||
return classes?.filter(Boolean).join(" ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { describe, it, expect } from "vitest" | ||
import { clx } from "../../src/dom/clx" | ||
|
||
describe("classes", () => { | ||
it("should compose clx list", () => { | ||
// simple | ||
expect(clx("a", "b")).toBe("a b") | ||
|
||
// with boolean | ||
expect(clx(false && "a", "b")).toBe("b") | ||
expect(clx(true && "foo", true && "bar")).toBe("foo bar") | ||
expect(clx(false && "foo", true && "bar")).toBe("bar") | ||
|
||
// ternary operator | ||
expect(clx(false ? "foo" : "voo", true && "bar")).toBe("voo bar") | ||
|
||
// return empty string if no clx | ||
expect(clx(false)).toBe("") | ||
expect(clx(false && "foo")).toBe("") | ||
expect(clx(undefined)).toBe("") | ||
expect(clx(null)).toBe("") | ||
}) | ||
}) |