@lightsound/cn 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 lightsound
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,183 @@
1
+ # @lightsound/cn
2
+
3
+ A tiny, **blazing fast** utility for constructing `className` strings conditionally.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@lightsound/cn.svg)](https://www.npmjs.com/package/@lightsound/cn)
6
+ [![gzip size](https://img.shields.io/badge/gzip-139B-brightgreen.svg)](https://bundlephobia.com/package/@lightsound/cn)
7
+ [![license](https://img.shields.io/npm/l/@lightsound/cn.svg)](./LICENSE)
8
+
9
+ ## Features
10
+
11
+ - **Blazing Fast**: Up to 48% faster than `clsx/lite`
12
+ - **Tiny**: ~139B gzipped (smaller than clsx/lite!)
13
+ - **TypeScript**: Full type support out of the box
14
+ - **Simple API**: Strings only - no objects, no arrays, maximum performance
15
+ - **Zero Dependencies**: No external dependencies
16
+
17
+ ## Benchmarks
18
+
19
+ Benchmarked on Apple M2 with Bun 1.3.4:
20
+
21
+ | Test Case | @lightsound/cn | clsx/lite | Improvement |
22
+ | ---------- | -------------- | --------- | -------------- |
23
+ | 2 strings | 21.17 ns | 33.23 ns | **36% faster** |
24
+ | 3 strings | 25.74 ns | 46.05 ns | **44% faster** |
25
+ | 5 strings | 37.84 ns | 70.59 ns | **46% faster** |
26
+ | 10 strings | 63.60 ns | 121.75 ns | **48% faster** |
27
+
28
+ ## Installation
29
+
30
+ ```bash
31
+ # npm
32
+ npm install @lightsound/cn
33
+
34
+ # pnpm
35
+ pnpm add @lightsound/cn
36
+
37
+ # yarn
38
+ yarn add @lightsound/cn
39
+
40
+ # bun
41
+ bun add @lightsound/cn
42
+ ```
43
+
44
+ ## Usage
45
+
46
+ ```typescript
47
+ import { cn } from "@lightsound/cn";
48
+ // or
49
+ import cn from "@lightsound/cn";
50
+
51
+ // Basic usage
52
+ cn("foo", "bar");
53
+ // => 'foo bar'
54
+
55
+ // Conditional classes
56
+ cn("btn", isActive && "btn-active", isDisabled && "btn-disabled");
57
+ // => 'btn btn-active' (if isActive is true, isDisabled is false)
58
+
59
+ // With ternary expressions
60
+ cn("btn", variant === "primary" ? "btn-primary" : "btn-secondary");
61
+ // => 'btn btn-primary'
62
+
63
+ // Falsy values are ignored
64
+ cn("foo", false, null, undefined, 0, "", "bar");
65
+ // => 'foo bar'
66
+ ```
67
+
68
+ ## Real-world Example (Tailwind CSS)
69
+
70
+ ```typescript
71
+ import { cn } from "@lightsound/cn";
72
+
73
+ interface ButtonProps {
74
+ variant?: "primary" | "secondary";
75
+ size?: "sm" | "md" | "lg";
76
+ disabled?: boolean;
77
+ className?: string;
78
+ }
79
+
80
+ function Button({
81
+ variant = "primary",
82
+ size = "md",
83
+ disabled,
84
+ className,
85
+ }: ButtonProps) {
86
+ return (
87
+ <button
88
+ className={cn(
89
+ "inline-flex items-center justify-center rounded-md font-medium",
90
+ "transition-colors focus-visible:outline-none focus-visible:ring-2",
91
+ variant === "primary" && "bg-blue-500 text-white hover:bg-blue-600",
92
+ variant === "secondary" &&
93
+ "bg-gray-200 text-gray-900 hover:bg-gray-300",
94
+ size === "sm" && "h-8 px-3 text-sm",
95
+ size === "md" && "h-10 px-4 text-base",
96
+ size === "lg" && "h-12 px-6 text-lg",
97
+ disabled && "pointer-events-none opacity-50",
98
+ className
99
+ )}
100
+ disabled={disabled}
101
+ >
102
+ Click me
103
+ </button>
104
+ );
105
+ }
106
+ ```
107
+
108
+ ## API
109
+
110
+ ### `cn(...classes)`
111
+
112
+ Combines class names into a single string. Only accepts strings - non-string values (falsy values) are ignored.
113
+
114
+ #### Parameters
115
+
116
+ - `...classes`: `(string | false | null | undefined | 0)[]` - Any number of class name strings or falsy values
117
+
118
+ #### Returns
119
+
120
+ - `string` - The combined class names separated by spaces
121
+
122
+ ## Why @lightsound/cn?
123
+
124
+ | Feature | @lightsound/cn | clsx | clsx/lite |
125
+ | --------------- | -------------- | ----- | --------- |
126
+ | Strings only | ✅ | ❌ | ✅ |
127
+ | Objects support | ❌ | ✅ | ❌ |
128
+ | Arrays support | ❌ | ✅ | ❌ |
129
+ | Size (gzip) | ~139B | ~239B | ~149B |
130
+ | Performance | ⚡⚡⚡ | ⚡⚡ | ⚡⚡ |
131
+
132
+ If you only use string-based class composition (the most common pattern with Tailwind CSS), `@lightsound/cn` provides the best performance.
133
+
134
+ ## Compatibility with clsx/lite
135
+
136
+ `@lightsound/cn` is designed as a faster, drop-in replacement for `clsx/lite` in typical usage:
137
+
138
+ ```typescript
139
+ // Before
140
+ import clsx from "clsx/lite";
141
+ clsx("btn", isActive && "active", "btn-primary");
142
+
143
+ // After
144
+ import { cn } from "@lightsound/cn";
145
+ cn("btn", isActive && "active", "btn-primary");
146
+ ```
147
+
148
+ ### TypeScript Users
149
+
150
+ **You're fully covered!** The type definition only accepts `string | false | 0 | null | undefined`, so passing objects or arrays will result in a compile-time error:
151
+
152
+ ```typescript
153
+ cn("btn", { active: true }); // ❌ TypeScript error
154
+ cn("btn", ["a", "b"]); // ❌ TypeScript error
155
+ cn("btn", isActive && "active"); // ✅ Works perfectly
156
+ ```
157
+
158
+ ### JavaScript Users
159
+
160
+ Note that runtime behavior differs from `clsx/lite` when passing unsupported types:
161
+
162
+ | Input | clsx/lite | @lightsound/cn |
163
+ | ------------------ | --------- | ------------------- |
164
+ | `{ active: true }` | `""` | `"[object Object]"` |
165
+ | `["a", "b"]` | `""` | `"a,b"` |
166
+
167
+ If you're using JavaScript, ensure your codebase only passes strings and falsy values to `cn()`.
168
+
169
+ ## Tailwind CSS IntelliSense
170
+
171
+ To enable Tailwind CSS IntelliSense for `cn()`, add this to your VS Code settings:
172
+
173
+ ```json
174
+ {
175
+ "tailwindCSS.experimental.classRegex": [
176
+ ["cn\\(([^)]*)\\)", "(?:'|\"|`)([^']*)(?:'|\"|`)"]
177
+ ]
178
+ }
179
+ ```
180
+
181
+ ## License
182
+
183
+ MIT © [lightsound](https://github.com/lightsound)
@@ -0,0 +1,8 @@
1
+ type ClassValue = string | false | 0 | null | undefined;
2
+ export declare function cn(): string;
3
+ export declare function cn(a: ClassValue): string;
4
+ export declare function cn(a: ClassValue, b: ClassValue): string;
5
+ export declare function cn(a: ClassValue, b: ClassValue, c: ClassValue): string;
6
+ export declare function cn(a: ClassValue, b: ClassValue, c: ClassValue, d: ClassValue): string;
7
+ export declare function cn(...args: ClassValue[]): string;
8
+ export { cn as default };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ function e(){let s="",a,l=arguments.length;for(let n=0;n<l;n++)if(a=arguments[n])s=s?s+" "+a:a;return s}export{e as default,e as cn};
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@lightsound/cn",
3
+ "version": "0.1.0",
4
+ "description": "A tiny, blazing fast utility for constructing className strings conditionally",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "sideEffects": false,
19
+ "scripts": {
20
+ "build": "bun run build:js && bun run build:types",
21
+ "build:js": "bun build src/index.ts --outdir dist --minify --target browser",
22
+ "build:types": "tsgo -p tsconfig.build.json",
23
+ "test": "bun test",
24
+ "test:watch": "bun test --watch",
25
+ "bench": "bun run bench/index.ts",
26
+ "prepublishOnly": "bun run build && bun run test"
27
+ },
28
+ "keywords": [
29
+ "classnames",
30
+ "classes",
31
+ "classname",
32
+ "class",
33
+ "cn",
34
+ "clsx",
35
+ "tailwind",
36
+ "css"
37
+ ],
38
+ "author": "lightsound",
39
+ "license": "MIT",
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/lightsound/cn.git"
43
+ },
44
+ "bugs": {
45
+ "url": "https://github.com/lightsound/cn/issues"
46
+ },
47
+ "homepage": "https://github.com/lightsound/cn#readme",
48
+ "devDependencies": {
49
+ "@types/bun": "^1.3.4",
50
+ "@typescript/native-preview": "^7.0.0-dev.20251213.1",
51
+ "clsx": "^2.1.1",
52
+ "mitata": "^1.0.34"
53
+ }
54
+ }