├── .github └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE.md ├── README.md ├── bench ├── index.js ├── package.json └── runBenchmark.js ├── index.d.ts ├── index.js ├── package.json └── tests └── index.test.js /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: push 3 | jobs: 4 | test: 5 | env: 6 | NODE_ENV: development 7 | runs-on: ubuntu-latest 8 | strategy: 9 | matrix: 10 | node-version: [14.x, 16.x, 18.x] 11 | steps: 12 | - name: Checkout 13 | uses: actions/checkout@v1 14 | - name: Use Node.js ${{ matrix.node-version }} 15 | uses: actions/setup-node@v1 16 | with: 17 | node-version: ${{ matrix.node-version }} 18 | - name: Test 19 | run: | 20 | npm install -g codecov 21 | npm install 22 | npm test 23 | codecov 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.map 2 | *.br 3 | *.gz 4 | 5 | *.cjs 6 | *.min.js 7 | 8 | package-lock.json 9 | node_modules 10 | coverage 11 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright © Jorge Bucaran <> 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Classcat 2 | 3 | > Build a [`class`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/class) attribute string quickly. 4 | 5 | - Framework agnostic, reusable, plain vanilla JavaScript. 6 | - Up to [2.5x faster](#benchmarks) than alternatives. 7 | - [217 B](http://bundlephobia.com/result?p=classcat) (minified+gzipped). 👌 8 | 9 | This module makes it easy to build a space-delimited `class` attribute string from an object or array of CSS class names. Just pair each class with a boolean value to add or remove them conditionally. 10 | 11 | ```js 12 | import cc from "classcat" 13 | 14 | export const ToggleButton = ({ isOn, toggle }) => ( 15 |
toggle(!isOn)}> 16 |
23 | {isOn ? "ON" : "OFF"} 24 |
25 | ) 26 | ``` 27 | 28 | [Try with React](https://codepen.io/jorgebucaran/pen/NYgLwG?editors=0010), [lit-html](https://codepen.io/jorgebucaran/pen/LjPJEp?editors=1000), [Mithril](https://codepen.io/jorgebucaran/pen/JjjOjwB?editors=1100), [Superfine](https://codepen.io/jorgebucaran/pen/wrMvjz?editors=1000) 29 | 30 | ## Installation 31 | 32 | ```console 33 | npm install classcat 34 | ``` 35 | 36 | Or without a build step—import it right in your browser. 37 | 38 | ```html 39 | 42 | ``` 43 | 44 | ## API 45 | 46 | ### `cc(names)` 47 | 48 | ```ps 49 | cc(names: string | number | object | array): string 50 | ``` 51 | 52 | ```js 53 | import cc from "classcat" 54 | 55 | cc("elf") //=> "elf" 56 | 57 | cc(["elf", "orc", "gnome"]) //=> "elf orc gnome" 58 | 59 | cc({ 60 | elf: false, 61 | orc: null, 62 | gnome: undefined, 63 | }) //=> "" 64 | 65 | cc({ 66 | elf: true, 67 | orc: false, 68 | gnome: true, 69 | }) //=> "elf gnome" 70 | 71 | cc([ 72 | { 73 | elf: true, 74 | orc: false, 75 | }, 76 | "gnome", 77 | ]) //=> "elf gnome" 78 | ``` 79 | 80 | ## Benchmarks 81 | 82 | ```console 83 | npm --prefix bench start 84 | ``` 85 | 86 | ## License 87 | 88 | [MIT](LICENSE.md) 89 | -------------------------------------------------------------------------------- /bench/index.js: -------------------------------------------------------------------------------- 1 | import { runBenchmark } from "./runBenchmark.js" 2 | import classcat from "../index.js" 3 | import classnames from "classnames" 4 | import clsx from "clsx" 5 | 6 | runBenchmark( 7 | [ 8 | { 9 | name: "# Strings", 10 | args: ["one", "two", "three"], 11 | }, 12 | { 13 | name: "# Objects", 14 | args: [ 15 | { 16 | one: true, 17 | two: true, 18 | three: false, 19 | }, 20 | ], 21 | }, 22 | { 23 | name: "# Strings/Objects", 24 | args: [ 25 | "one", 26 | "two", 27 | { 28 | four: true, 29 | three: false, 30 | }, 31 | ], 32 | }, 33 | { 34 | name: "# Arrays", 35 | args: [ 36 | ["one", "two"], 37 | ["three"], 38 | ["four", ["five"]], 39 | [ 40 | { 41 | six: true, 42 | }, 43 | { 44 | seven: false, 45 | }, 46 | ], 47 | ], 48 | }, 49 | { 50 | name: "# Arrays/Objects", 51 | args: [ 52 | "one", 53 | { 54 | two: true, 55 | three: false, 56 | }, 57 | { 58 | four: "foo", 59 | five: true, 60 | }, 61 | 6, 62 | {}, 63 | ], 64 | }, 65 | { 66 | name: "# Arguments vs Array", 67 | args: [ 68 | ["one", "two"], 69 | ["three"], 70 | ["four", ["five"]], 71 | [ 72 | { 73 | six: true, 74 | }, 75 | { 76 | seven: false, 77 | }, 78 | ], 79 | ], 80 | unique: true, 81 | }, 82 | ].reduce( 83 | (t, { name, args, unique }) => ({ 84 | ...t, 85 | [name]: unique 86 | ? (c, id) => (id === "classcat" ? c(args) : c(...args)) 87 | : (c) => c(args), 88 | }), 89 | {} 90 | ), 91 | { classcat, classnames, clsx } 92 | ) 93 | -------------------------------------------------------------------------------- /bench/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "type": "module", 4 | "scripts": { 5 | "start": "npm install && node index.js" 6 | }, 7 | "devDependencies": { 8 | "benchmark": "^2.1.4", 9 | "classnames": "*", 10 | "clsx": "*" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /bench/runBenchmark.js: -------------------------------------------------------------------------------- 1 | import bench from "benchmark" 2 | 3 | export const runBenchmark = (tests, modules) => 4 | Object.keys(tests).map((name, i) => { 5 | console.log(`${i > 0 ? "\n" : ""}${name}`) 6 | Object.keys(modules) 7 | .reduce( 8 | (bench, id) => bench.add(id, tests[name].bind({}, modules[id], id)), 9 | new bench.Suite().on("cycle", ({ target: { name, hz } }) => 10 | console.log(`${name} × ${Math.floor(hz).toLocaleString()} ops/sec`) 11 | ) 12 | ) 13 | .run() 14 | }) 15 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export as namespace Classcat 2 | 3 | export interface ClassObject { 4 | [key: string]: boolean | any 5 | } 6 | 7 | export interface ClassArray extends Array {} 8 | 9 | export type Class = string | number | null | undefined | ClassObject | ClassArray 10 | 11 | /** 12 | * @param names A string, array or object of CSS class names (as keys). 13 | * @returns The class attribute string. 14 | */ 15 | export default function cc(names: Class): string 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function cc(names) { 2 | if (typeof names === "string" || typeof names === "number") return "" + names 3 | 4 | let out = "" 5 | 6 | if (Array.isArray(names)) { 7 | for (let i = 0, tmp; i < names.length; i++) { 8 | if ((tmp = cc(names[i])) !== "") { 9 | out += (out && " ") + tmp 10 | } 11 | } 12 | } else { 13 | for (let k in names) { 14 | if (names[k]) out += (out && " ") + k 15 | } 16 | } 17 | 18 | return out 19 | } 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "classcat", 3 | "version": "5.0.5", 4 | "type": "module", 5 | "main": "index.cjs", 6 | "module": "index.js", 7 | "types": "index.d.ts", 8 | "description": "Build a class attribute string quickly.", 9 | "repository": "jorgebucaran/classcat", 10 | "license": "MIT", 11 | "exports": { 12 | "./package.json": "./package.json", 13 | ".": { 14 | "require": "./index.cjs", 15 | "import": "./index.js" 16 | } 17 | }, 18 | "files": [ 19 | "*.*(c)[tj]s*" 20 | ], 21 | "author": "Jorge Bucaran", 22 | "keywords": [ 23 | "classnames", 24 | "classlist", 25 | "class" 26 | ], 27 | "scripts": { 28 | "test": "c8 twist tests/*.js", 29 | "build": "node -e \"fs.writeFileSync('index.cjs',fs.readFileSync('index.js','utf8').replace(/export default/,'module.exports ='),'utf8')\"", 30 | "deploy": "npm test && git commit --all --message $tag && git tag --sign $tag --message $tag && git push && git push --tags", 31 | "release": "tag=$npm_package_version npm run deploy && npm publish --access public", 32 | "prepare": "npm run build" 33 | }, 34 | "devDependencies": { 35 | "c8": "*", 36 | "twist": "*" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /tests/index.test.js: -------------------------------------------------------------------------------- 1 | import cc from "../index.js" 2 | import { t, equal } from "twist" 3 | 4 | export default [ 5 | t("classcat", [ 6 | t("nothing, null, undefined", [ 7 | equal(cc(), ""), 8 | equal(cc(null), ""), 9 | equal(cc(undefined), ""), 10 | equal(cc([, , , null, undefined]), ""), 11 | ]), 12 | t("empty objects", [ 13 | equal(cc({}), ""), 14 | equal(cc([]), ""), 15 | equal(cc([{}]), ""), 16 | equal(cc([{}, {}, {}]), ""), 17 | ]), 18 | t("booleans", [ 19 | equal(cc(true), ""), 20 | equal(cc(false), ""), 21 | equal(cc([true, false]), ""), 22 | ]), 23 | t("numbers", [ 24 | equal(cc(0), "0"), 25 | equal(cc([0, 1]), "0 1"), 26 | equal(cc({ 0: true, 1: true }), "0 1"), 27 | ]), 28 | t("empty strings", [ 29 | equal(cc(""), ""), 30 | equal( 31 | cc({ 32 | elf: "", 33 | orc: "", 34 | gnome: "", 35 | }), 36 | "" 37 | ), 38 | equal(cc(["", "", ""]), ""), 39 | ]), 40 | t("arrays of strings", [ 41 | equal(cc(["elf", "orc", false, "gnome"]), "elf orc gnome"), 42 | ]), 43 | t("array of arrays", [ 44 | equal(cc(["elf", ["orc", [false, "gnome"]]]), "elf orc gnome"), 45 | ]), 46 | t("object of key:string pairs", [ 47 | equal( 48 | cc({ 49 | elf: true, 50 | orc: true, 51 | dodo: false, 52 | gnome: true, 53 | }), 54 | "elf orc gnome" 55 | ), 56 | ]), 57 | t("array of objects and arrays", [ 58 | equal( 59 | cc([ 60 | "elf", 61 | "half-orc", 62 | { 63 | "half-elf": true, 64 | }, 65 | ["gnome", "goblin", "dwarf"], 66 | ]), 67 | "elf half-orc half-elf gnome goblin dwarf" 68 | ), 69 | ]), 70 | ]), 71 | ] 72 | --------------------------------------------------------------------------------