Skip to content

Commit

Permalink
Update name to clx
Browse files Browse the repository at this point in the history
  • Loading branch information
willybrauner committed Aug 31, 2023
1 parent 20fd707 commit 7bbb000
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/dom/clx.ts
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(" ")
}
23 changes: 23 additions & 0 deletions tests/dom/clx.test.ts
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("")
})
})

0 comments on commit 7bbb000

Please sign in to comment.