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
Provenance
- Commit
1a49142- Tree
0946852- Author
Luke Edwards <luke.edwards05@gmail.com>- Committer
Luke Edwards <luke.edwards05@gmail.com>- Parent
5cac14c
7 files changed+194 added−25 removed
modified:bench/index.js+2 added
1234567891011121314151617181944 unmodified linesconst { Suite } = require('benchmark');const classnames = require('classnames');const classcat = require('classcat');const clsx = require('../dist/clsx');const old = require('clsx');const lite = require('../dist/lite');
function bench(name, ...args) { console.log(`\n# ${name}`); new Suite() .add('classcat ≠ ', () => classcat.apply(classcat, [args])) .add('classnames ', () => classnames.apply(classnames, args)) .add('clsx (prev) ', () => old.apply(old, args)) .add('clsx ', () => clsx.apply(clsx, args)) .add('clsx (lite) ', () => lite.apply(lite, args)) .on('cycle', e => console.log(' ' + e.target)) .run();}
44 unmodified linesmodified:bench/readme.md+1 added
11 unmodified lines12131415161718192041 unmodified lines11 unmodified lines# Strings classcat ≠ x 9,613,381 ops/sec ±0.16% (94 runs sampled) classnames x 6,540,072 ops/sec ±0.11% (101 runs sampled) clsx x 12,924,662 ops/sec ±0.15% (102 runs sampled) clsx/lite x 13,122,004 ops/sec ±0.40% (99 runs sampled)
# Objects classcat ≠ x 8,936,903 ops/sec ±0.12% (100 runs sampled) classnames x 6,143,319 ops/sec ±0.14% (100 runs sampled)41 unmodified linesmodified:bin/index.js+49 added-16 removed
2 unmodified lines345678789103 unmodified lines1415161720211819202122232425262427282930313226273334353637382930313233343940414243444546474849505152535455565758596061623637386364656667686970712 unmodified linesconst zlib = require('zlib');const { minify } = require('terser');const pkg = require('../package.json');
if (!fs.existsSync('dist')) fs.mkdirSync('dist');
/** * @param {string} file * @param {string} source */3 unmodified lines module: isModule, compress: true, });
fs.writeFileSync(file, result.code); console.log('~> "%s" (%d b)', file, zlib.gzipSync(result.code).byteLength); if (result.code) { fs.writeFileSync(file, result.code); let size = zlib.gzipSync(result.code).byteLength; console.log('~> "%s" (%d b)', file, size); } else { console.error('!! "%s" ::', file, result.error); }}
let input = fs.readFileSync('src/index.js', 'utf8');/** * @typedef Export * @property {Condition} import * @property {Condition} default */
// copy for ESMwrite(pkg.module, input);/** * @typedef Condition * @property {string} types * @property {string} default */
// transform ESM -> CJS exportswrite(pkg.main, input.replace('export function', 'function').replace( 'export default clsx;', 'module.exports = clsx;\n' + 'module.exports.clsx = clsx;'));/** * @param {string} file * @param {"." | "./lite"} entry */function bundle(file, entry) { fs.existsSync('dist') || fs.mkdirSync('dist');
/** * @type {Export} */ let output = pkg.exports[entry]; let input = fs.readFileSync(file, 'utf8');
// copy for ESM file write(output.import.default, input);
// transform ESM -> CJS exports write(output.default.default, input.replace('export function', 'function').replace( 'export default clsx;', 'module.exports = clsx;\n' + 'module.exports.clsx = clsx;' ));
if (entry === '.') {// transform ESM -> UMD exportsinput = input.replace('export function', 'function').replace('export default clsx;', 'return clsx.clsx=clsx, clsx;');write(pkg.unpkg, '!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):global.clsx=factory()}(this,function(){' + input + '});'); // transform ESM -> UMD exports input = input.replace('export function', 'function').replace('export default clsx;', 'return clsx.clsx=clsx, clsx;'); write(pkg.unpkg, '!function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):global.clsx=factory()}(this,function(){' + input + '});'); }}
bundle('src/index.js', '.');console.log('---');bundle('src/lite.js', './lite');added:src/lite.js+13 added
12345678910111213export function clsx() { var i=0, tmp, str='', len=arguments.length; for (; i < len; i++) { if (tmp = arguments[i]) { if (typeof tmp === 'string') { str += (str && ' ') + tmp; } } } return str;}
export default clsx;added:test/lite.js+63 added
1+// @ts-check2+import { test } from 'uvu';3+import * as assert from 'uvu/assert';4+import * as mod from '../src/lite';5+6+const fn = mod.default;7+8+test('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');12+13+ assert.type(mod.default(), 'string', '~> returns string output');14+ assert.type(mod.clsx(), 'string', '~> returns string output');15+});16+17+test('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+});23+24+test('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+});30+31+test('emptys', () => {32+ assert.is(fn(''), '');33+ assert.is(fn(undefined), '');34+ assert.is(fn(null), '');35+ assert.is(fn(0), '');36+});37+38+// lite ignores all non-strings39+test('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), '');46+47+ // objects48+ assert.is(fn({}), '');49+ assert.is(fn(null), '');50+ assert.is(fn({ a:1 }), '');51+ assert.is(fn({ a:1 }, { b:2 }), '');52+53+ // arrays54+ assert.is(fn([]), '');55+ assert.is(fn(['foo']), '');56+ assert.is(fn(['foo', 'bar']), '');57+58+ // functions59+ assert.is(fn(fn), '');60+ assert.is(fn(fn, fn), '');61+});62+63+test.run();modified:package.json+20 added-8 removed
4 unmodified lines5 "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 linesmodified:readme.md+46 added-1 removed
64 unmodified lines65 clsx(true, false, '', null, undefined, 0, NaN);66 //=> ''67 ```68 69+## Modes70+71+There are multiple "versions" of `clsx` available, which allows you to bring only the functionality you need!72+73+#### `clsx`74+> **Size (gzip):** 239 bytes<br>75+> **Availability:** CommonJS, ES Module, UMD76+77+The default `clsx` module; see [API](#API) for info.78+79+```js80+import { clsx } from 'clsx';81+// or82+import clsx from 'clsx';83+```84+85+#### `clsx/lite`86+> **Size (gzip):** 140 bytes<br>87+> **Availability:** CommonJS, ES Module<br>88+> **CAUTION:** Accepts **ONLY** string arguments!89+90+Ideal for applications that ***only*** use the string-builder pattern.91+92+Any non-string arguments are ignored!93+94+```js95+import { clsx } from 'clsx/lite';96+// or97+import clsx from 'clsx/lite';98+99+// string100+clsx('hello', true && 'foo', false && 'bar');101+// => "hello foo"102+103+// NOTE: Any non-string input(s) ignored104+clsx({ foo: true });105+//=> ""106+```107+108 ## Benchmarks109 110 For snapshots of cross-browser results, check out the [`bench`](bench) directory~!111 7 unmodified lines119 120 ## Tailwind Support121 122 Here some additional (optional) steps to enable classes autocompletion using `clsx` with Tailwind CSS.84-<details>123 124+<details>125 <summary>126 Visual Studio Code127 </summary>128 9 unmodified lines138 }139 ```140 </details>141 142+You 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:143+144+```js145+clsx('text-base', props.active && 'text-primary', props.className);146+```147+148 ## Related149 150 - [obj-str](https://github.com/lukeed/obj-str) - A smaller (96B) and similiar utility that only works with Objects.151 3 unmodified lines