klsx 0.2.1 → 0.3.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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  A simple replacement for [clsx](https://www.npmjs.com/package/clsx) **_you may not actually need_** (see the [benchmark](https://github.com/vuolter/klsx/blob/main/BENCHMARK.md))
4
4
 
5
- Used to construct `classNames` conditionally with strings, arrays and key-value objects.
5
+ Used to construct `class` conditionally with strings, arrays and key-value objects.
6
6
 
7
7
  Developed as a case study, [Bun](https://bun.com) friendly. 🌿
8
8
 
@@ -34,41 +34,111 @@ npm install klsx
34
34
  ```typescript
35
35
  import { klsx } from 'klsx'
36
36
 
37
- klsx('foo', 'bar') // -> "foo bar"
38
- klsx('foo', { bar: true, bux: false }, 'baz') // -> "foo bar baz"
39
- klsx('foo', [0, 'bar', [{ bux: null, baz: 'I am not empty' }, undefined], 'bax']) // -> "foo bar baz bax"
37
+ klsx('foo', 'bar', 'baz')
38
+ // => "foo bar baz"
39
+
40
+ klsx({foo: 'string', bar: true, bux: null }, 'baz')
41
+ // => "foo bar baz"
42
+
43
+ klsx(['foo', ['bar', true && 'baz']])
44
+ // => "foo bar baz"
45
+ ```
46
+
47
+ or importing the short alias `kx`:
48
+
49
+ ### React
50
+
51
+ ```jsx
52
+ import React, { useState } from 'react';
53
+ import { kx } from 'klsx'
54
+
55
+ const MyComponent = () => {
56
+ const [isActive, setIsActive] = useState(false);
57
+ return (
58
+ <div>
59
+ <div className={kx('base-class', isActive ? 'active' : 'inactive')}>
60
+ {/* ... */}
61
+ </div>
62
+
63
+ <div className={kx('base-class', {'active': isActive, 'inactive': !isActive})}>
64
+ {/* ... */}
65
+ </div>
66
+
67
+ <div className={kx('base-class', [isActive && 'active', !isActive && 'inactive'])}>
68
+ {/* ... */}
69
+ </div>
70
+ </div>
71
+ );
72
+ };
73
+ ```
74
+
75
+ ### Migrate from `clsx`
76
+
77
+ Just replace every `c` with a `k`, as in the example below.
78
+
79
+ From:
80
+
81
+ ```typescript
82
+ // clsx
83
+
84
+ import { clsx, type ClassValue } from "clsx"
85
+ import { twMerge } from "tailwind-merge"
86
+
87
+ export function cn(...inputs: ClassValue[]) {
88
+ return twMerge(clsx(inputs))
89
+ }
40
90
  ```
41
91
 
42
- or with the short alias `cn`:
92
+ To:
43
93
 
44
94
  ```typescript
45
- import { cn } from 'klsx'
95
+ // KLSX
46
96
 
47
- cn('foo', 'bar') // -> "foo bar"
48
- cn('foo', { bar: true, bux: false }, 'baz') // -> "foo bar baz"
49
- cn('foo', [0, 'bar', [{ bux: null, baz: 'I am not empty' }, undefined], 'bax']) // -> "foo bar baz bax"
97
+ import { klsx, type KlassValue } from "klsx"
98
+ import { twMerge } from "tailwind-merge"
99
+
100
+ export function cn(...inputs: ClassValue[]) {
101
+ return twMerge(klsx(inputs))
102
+ }
50
103
  ```
51
104
 
52
105
  ## Integrations
53
106
 
54
107
  ### Tailwind CSS
55
108
 
56
- <details open>
57
- <summary>
58
- Visual Studio Code
59
- </summary>
109
+ #### Visual Studio Code
60
110
 
61
- 1. Install the [Tailwind CSS IntelliSense](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss) extension.
111
+ 1. Install the [Tailwind CSS IntelliSense](https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss) extension:
112
+
113
+ ```bash
114
+ code --install-extension bradlc.vscode-tailwindcss
115
+ ```
62
116
 
63
117
  2. Update your [`settings.json`](https://code.visualstudio.com/docs/getstarted/settings) configuration with:
64
118
 
65
119
  ```json
66
120
  {
67
- "tailwindCSS.classFunctions": ["klsx", "cn"]
121
+ "tailwindCSS.classFunctions": ["klsx", "kx"]
68
122
  }
69
123
  ```
70
124
 
71
- </details>
125
+ #### Prettier
126
+
127
+ 1. Install the [prettier-plugin-tailwindcss](https://www.npmjs.com/package/prettier-plugin-tailwindcss) plugin:
128
+
129
+ ```bash
130
+ bun add -D prettier prettier-plugin-tailwindcss
131
+ ```
132
+
133
+ 2. Update your `prettier.config.js` configuration to handle `klsx`:
134
+
135
+ ```javascript
136
+ /** @type {import('prettier').Config & import('prettier-plugin-tailwindcss').PluginOptions} */
137
+ export default {
138
+ plugins: ["prettier-plugin-tailwindcss"],
139
+ tailwindFunctions: ['klsx', 'kx']
140
+ }
141
+ ```
72
142
 
73
143
  ### ESLint
74
144
 
@@ -80,7 +150,7 @@ bun add -D eslint eslint-plugin-clsx
80
150
 
81
151
  2. Update your `eslint.config.js` configuration to handle `klsx`:
82
152
 
83
- ```typescript
153
+ ```javascript
84
154
  import { defineConfig } from 'eslint/config'
85
155
  import clsx from 'eslint-plugin-clsx'
86
156
 
@@ -88,7 +158,7 @@ export default defineConfig({
88
158
  plugins: { clsx },
89
159
  settings: {
90
160
  clsxOptions: {
91
- klsx: ['default', 'klsx', 'cn'],
161
+ klsx: ['default', 'klsx', 'kx'],
92
162
  },
93
163
  },
94
164
  })
@@ -121,7 +191,3 @@ Run benchmark suite:
121
191
  ```bash
122
192
  bun bench
123
193
  ```
124
-
125
- ## Case Study
126
-
127
- _Coming soon..._
package/dist/index.d.ts CHANGED
@@ -1,4 +1,9 @@
1
- //#region src/cn.d.ts
2
- declare function cn(...args: KlsxValue[]): string;
1
+ //#region src/types.d.ts
2
+ type KlassObject = Record<string, boolean | unknown>;
3
+ type KlassArray = KlassValue[];
4
+ type KlassValue = bigint | boolean | KlassArray | KlassObject | null | number | string | undefined;
3
5
  //#endregion
4
- export { cn, cn as default, cn as klsx };
6
+ //#region src/kx.d.ts
7
+ declare function kx(...args: KlassValue[]): string;
8
+ //#endregion
9
+ export { type KlassArray, type KlassObject, type KlassValue, kx as default, kx as klsx, kx };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- function e(t){let n=``;for(let r of t)if(r){if(typeof r==`string`)n=n?n+` `+r:r;else if(typeof r==`object`)if(Array.isArray(r)){let t=e(r);t&&(n=n?n+` `+t:t)}else for(let e in r)r[e]&&(n=n?n+` `+e:e)}return n}function t(...t){return e(t)}export{t as cn,t as default,t as klsx};
1
+ function e(t){let n=``;for(let r of t)if(r){if(typeof r==`string`)n=n?n+` `+r:r;else if(typeof r==`object`)if(Array.isArray(r)){let t=e(r);t&&(n=n?n+` `+t:t)}else for(let e in r)r[e]&&(n=n?n+` `+e:e)}return n}function t(...t){return e(t)}export{t as default,t as klsx,t as kx};
package/package.json CHANGED
@@ -1,22 +1,54 @@
1
1
  {
2
2
  "name": "klsx",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
+ "type": "module",
5
+ "description": "Same as clsx but \"maybe\" better",
4
6
  "author": {
5
7
  "name": "Walter Purcaro",
6
8
  "url": "https://github.com/vuolter"
7
9
  },
10
+ "license": "MIT",
8
11
  "repository": {
9
12
  "type": "git",
10
13
  "url": "git+https://github.com/vuolter/klsx.git"
11
14
  },
15
+ "url": "https://github.com/vuolter/klsx",
16
+ "keywords": [
17
+ "cn",
18
+ "cx",
19
+ "kx",
20
+ "klsx",
21
+ "clsx",
22
+ "class",
23
+ "classix",
24
+ "classname",
25
+ "classnames",
26
+ "bun"
27
+ ],
28
+ "files": [
29
+ "dist"
30
+ ],
12
31
  "main": "./dist/index.js",
13
32
  "module": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "exports": {
35
+ ".": "./dist/index.js",
36
+ "./package.json": "./package.json"
37
+ },
38
+ "scripts": {
39
+ "build": "bun compile && tsdown",
40
+ "format": "prettier . --cache --experimental-cli --write",
41
+ "lint": "eslint --cache --cache-location ./node_modules/.cache/eslint --cache-strategy content --concurrency auto --fix .",
42
+ "compile": "tsgo",
43
+ "bench": "bun ./tests/bench",
44
+ "postinstall": "npm install --force --ignore-scripts --package-lock-only --silent"
45
+ },
14
46
  "devDependencies": {
15
47
  "@eslint/js": "^9.39.1",
16
- "@prettier/plugin-oxc": "^0.0.5",
48
+ "@prettier/plugin-oxc": "^0.1.2",
17
49
  "@socketsecurity/bun-security-scanner": "^1.1.1",
18
50
  "@types/bun": "^1.3.3",
19
- "@typescript/native-preview": "^7.0.0-dev.20251123.1",
51
+ "@typescript/native-preview": "^7.0.0-dev.20251201.1",
20
52
  "classix": "^2.2.6",
21
53
  "classnames": "^2.5.1",
22
54
  "clsx": "^2.1.1",
@@ -31,44 +63,13 @@
31
63
  "eslint-plugin-tsdoc": "^0.5.0",
32
64
  "eslint-plugin-unicorn": "^62.0.0",
33
65
  "globals": "^16.5.0",
34
- "prettier": "^3.6.2",
35
- "tinybench": "^5.1.0",
36
- "tsdown": "^0.16.6",
37
- "typescript-eslint": "^8.47.0"
66
+ "prettier": "^3.7.3",
67
+ "tinybench": "^6.0.0",
68
+ "tsdown": "^0.17.0-beta.5",
69
+ "typescript-eslint": "^8.48.0"
38
70
  },
39
- "exports": {
40
- ".": "./dist/index.js",
41
- "./package.json": "./package.json"
42
- },
43
- "description": "Same as clsx but \"maybe\" better",
44
71
  "engines": {
45
72
  "node": ">=18"
46
73
  },
47
- "files": [
48
- "dist"
49
- ],
50
- "keywords": [
51
- "cn",
52
- "cx",
53
- "klsx",
54
- "clsx",
55
- "class",
56
- "classix",
57
- "classname",
58
- "classnames",
59
- "bun"
60
- ],
61
- "license": "MIT",
62
- "scripts": {
63
- "build": "bun typecheck && tsdown",
64
- "format": "prettier . --cache --experimental-cli --write",
65
- "lint": "eslint --cache --cache-location ./node_modules/.cache/eslint --cache-strategy content --concurrency auto --fix .",
66
- "typecheck": "tsgo",
67
- "bench": "bun ./tests/bench",
68
- "postinstall": "npm install --force --ignore-scripts --package-lock-only --silent"
69
- },
70
- "sideEffects": false,
71
- "type": "module",
72
- "types": "./dist/index.d.ts",
73
- "url": "https://github.com/vuolter/klsx"
74
+ "sideEffects": false
74
75
  }