Skip to content

Commit

Permalink
cls (#15)
Browse files Browse the repository at this point in the history
* Update name to clx

* rename to cls

* Add import
  • Loading branch information
Willy Brauner authored Sep 11, 2023
1 parent 20fd707 commit 9615f2a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/dom/cls.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
*
* cls("a", "b") // "a b"
* cls(false && "a", "b") // "b"
*
*/
export function cls(...classes: (boolean | string)[]): string {
return classes?.filter(Boolean).join(" ")
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export { shuffleArray } from "./array/shuffleArray"
// dom
export { getCssVariable } from "./dom/getCssVariable"
export { cls } from "./dom/cls"
// envs
export * from "./envs/envs"
// maths
Expand Down
23 changes: 23 additions & 0 deletions tests/dom/cls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, it, expect } from "vitest"
import { cls } from "../../src/dom/cls"

describe("cls", () => {
it("should compose cls list", () => {
// simple
expect(cls("a", "b")).toBe("a b")

// with boolean
expect(cls(false && "a", "b")).toBe("b")
expect(cls(true && "foo", true && "bar")).toBe("foo bar")
expect(cls(false && "foo", true && "bar")).toBe("bar")

// ternary operator
expect(cls(false ? "foo" : "voo", true && "bar")).toBe("voo bar")

// return empty string if no cls
expect(cls(false)).toBe("")
expect(cls(false && "foo")).toBe("")
expect(cls(undefined)).toBe("")
expect(cls(null)).toBe("")
})
})

0 comments on commit 9615f2a

Please sign in to comment.