Skip to content
Myr

feat: add `clsx/lite` module

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Luke Edwards · committed by Luke Edwards · parent 5cac14c · Browse files · 7 files+194 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+194 added−25 removed

  • tests:+63 added−0 removed
  • other:+131 added−25 removed

7 files+194 added−25 removed

modified:bench/index.js+2 added
modified:bench/readme.md+1 added
modified:bin/index.js+49 added-16 removed
added:src/lite.js+13 added
added:test/lite.js+63 added
1// @ts-check2import { test } from 'uvu';3import * as assert from 'uvu/assert';4import * as mod from '../src/lite';56const fn = mod.default;78test('exports', () => {9 assert.type(mod.default, 'function', 'exports default function');10 assert.type(mod.clsx, 'function', 'exports named function');11 assert.is(mod.default, mod.clsx, 'exports are equal');1213 assert.type(mod.default(), 'string', '~> returns string output');14 assert.type(mod.clsx(), 'string', '~> returns string output');15});1617test('strings', () => {18 assert.is(fn(''), '');19 assert.is(fn('foo'), 'foo');20 assert.is(fn(true && 'foo'), 'foo');21 assert.is(fn(false && 'foo'), '');22});2324test('strings (variadic)', () => {25 assert.is(fn(''), '');26 assert.is(fn('foo', 'bar'), 'foo bar');27 assert.is(fn(true && 'foo', false && 'bar', 'baz'), 'foo baz');28 assert.is(fn(false && 'foo', 'bar', 'baz', ''), 'bar baz');29});3031test('emptys', () => {32 assert.is(fn(''), '');33 assert.is(fn(undefined), '');34 assert.is(fn(null), '');35 assert.is(fn(0), '');36});3738// lite ignores all non-strings39test('non-strings', () => {40 // number41 assert.is(fn(1), '');42 assert.is(fn(1, 2), '');43 assert.is(fn(Infinity), '');44 assert.is(fn(NaN), '');45 assert.is(fn(0), '');4647 // objects48 assert.is(fn({}), '');49 assert.is(fn(null), '');50 assert.is(fn({ a:1 }), '');51 assert.is(fn({ a:1 }, { b:2 }), '');5253 // arrays54 assert.is(fn([]), '');55 assert.is(fn(['foo']), '');56 assert.is(fn(['foo', 'bar']), '');5758 // functions59 assert.is(fn(fn), '');60 assert.is(fn(fn, fn), '');61});6263test.run();
modified:package.json+20 added-8 removed
4 unmodified lines
5 "description": "A tiny (239B) utility for constructing className strings conditionally.",6 "module": "dist/clsx.mjs",7 "unpkg": "dist/clsx.min.js",8 "main": "dist/clsx.js",9 "types": "clsx.d.ts",10 "license": "MIT",11 "exports": {12 ".": {10 "import": {11 "types": "./clsx.d.mts",12 "default": "./dist/clsx.mjs"13 "import": {14 "types": "./clsx.d.mts",15 "default": "./dist/clsx.mjs"16 },17 "default": {18 "types": "./clsx.d.ts",19 "default": "./dist/clsx.js"20 }21 },22 "./lite": {23 "import": {24 "types": "./clsx.d.mts",25 "default": "./dist/lite.mjs"26 },14 "default": {15 "types": "./clsx.d.ts",16 "default": "./dist/clsx.js"27 "default": {28 "types": "./clsx.d.ts",29 "default": "./dist/lite.js"30 }31 }32 },19 "types": "clsx.d.ts",20 "license": "MIT",33 "author": {34 "name": "Luke Edwards",35 "email": "luke.edwards05@gmail.com",36 "url": "https://lukeed.com"
24 unmodified lines
modified:readme.md+46 added-1 removed
64 unmodified lines
65clsx(true, false, '', null, undefined, 0, NaN);66//=> ''67```6869## Modes7071There are multiple "versions" of `clsx` available, which allows you to bring only the functionality you need!7273#### `clsx`74> **Size (gzip):** 239 bytes<br>75> **Availability:** CommonJS, ES Module, UMD7677The default `clsx` module; see [API](#API) for info.7879```js80import { clsx } from 'clsx';81// or82import clsx from 'clsx';83```8485#### `clsx/lite`86> **Size (gzip):** 140 bytes<br>87> **Availability:** CommonJS, ES Module<br>88> **CAUTION:** Accepts **ONLY** string arguments!8990Ideal for applications that ***only*** use the string-builder pattern.9192Any non-string arguments are ignored!9394```js95import { clsx } from 'clsx/lite';96// or97import clsx from 'clsx/lite';9899// string100clsx('hello', true && 'foo', false && 'bar');101// => "hello foo"102103// NOTE: Any non-string input(s) ignored104clsx({ foo: true });105//=> ""106```107108## Benchmarks109110For snapshots of cross-browser results, check out the [`bench`](bench) directory~!111
7 unmodified lines
119120## Tailwind Support121122Here some additional (optional) steps to enable classes autocompletion using `clsx` with Tailwind CSS.84<details>123124<details>125<summary>126 Visual Studio Code127</summary>128
9 unmodified lines
138 }139 ```140</details>141142You may find the [`clsx/lite`](#clsxlite) module useful within Tailwind contexts. This is especially true if/when your application **only** composes classes in this pattern:143144```js145clsx('text-base', props.active && 'text-primary', props.className);146```147148## Related149150- [obj-str](https://github.com/lukeed/obj-str) - A smaller (96B) and similiar utility that only works with Objects.151
3 unmodified lines