🪢 A really tiny library that generates classname strings.

Overview

cnz (“classnamez”)

The smallest classnames-compatible package yet (I think).

This is an extremely simple javascript utility for conditionally joining classNames together. It’s based on classnames and its various clones and improvements.

This one is 151 bytes (146 gzipped), which is several times smaller than classnames. It’s much slower than classnames and clsx, but still runs a few million operations / second. It’s a tradeoff; clsx is a great library if you’re concerned about execution speed.

import cn from 'cnz';

cnz('one', 'two', 'three'); // 'one two three'
cnz('one', 'two', ['three', 'four', false && 'five']); // 'one two three four'
cnz('one', { two: true, three: false }, 'four'); // 'one two four'

Differences

The main differences between cnz and classnames is that cnz only supports strings, arrays, and objects. Numbers and booleans are ignored. This is deliberate, as it allows shorthands like results.count || 'empty'.

This is a subtle difference, so cnz should still work as a drop-in replacement for classnames in most cases.

Installation

npm install --save cnz

Or without a build step—import it right in your browser.

<script type="module">
  import cnz from "https://unpkg.com/cnz"
</script>

Benchmarks

npm --prefix benchmarks start

Similar packages

  • classnames: The original and most popular.
  • clsx: A smaller and faster classnames replacement.
  • classcat: Another alternative that this repo borrows from.

License

GNU GPLv3

You might also like...

Compare camelized/dasherized/underscored strings each other 🤜🏿 🤛🏿

aynen Compare camelized/dasherized/underscored strings each other Install npm install aynen yarn add aynen Usage import aynen from 'aynen'; aynen('fo

Mar 25, 2022

Simple string diffing. Given two strings, striff will return an object noting which characters were added or removed, and at which indices

Simple string diffing. Given two strings, striff will return an object noting which characters were added or removed, and at which indices

Jan 6, 2023

A TypeScript namespace declaration for KeyboardEvent.key strings, just in case your code is allergic to enums.

ts-key-namespace A TypeScript namespace declaration for KeyboardEvent.key strings, just in case you prefer namespaces to enums. Largely based on ts-ke

Apr 5, 2022

Transform URLs in strings to actual links.

Transform URLs in strings to actual links.

Transform URLs in strings to actual links. It will find valid links in the given string and create a tags for it. Internally, it uses this Regex to

Nov 4, 2022

Converts JSX to HTML strings at compile time.

unplugin-jsx-string Converts JSX to HTML strings at compile time. Installation npm i unplugin-jsx-string Vite // vite.config.ts import JsxString from

Sep 3, 2022

Construct ANSI colors strings from object descriptors.

ansi-construct Construct ANSI colors strings from object descriptors. Usage import { ansi } from 'ansi-construct' const item = ansi({ text: 'foo', co

Sep 8, 2022

Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Welcome to ts-object-keys đź‘‹ Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Jul 4, 2022

♻️ Simple way to convert strings to gitmojis

convert-gitmoji Simple way to convert strings to gitmojis ♻️ Install # Using npm npm install convert-gitmoji # Using yarn yarn add convert-gitmoji Us

Nov 4, 2022

Formats message strings with number, date, plural, and select placeholders to create localized messages

Formats message strings with number, date, plural, and select placeholders to create localized messages. Small. Between 700 bytes and 1.3 kilobytes (m

Oct 30, 2022
Owner
Riley Shaw
toymaker, troublemaker, dev @miseryco. formerly @mozilla and @Khan. learned @SFPC and @recursecenter. founder @ListnApp eons ago. strong passwords, weakly held.
Riley Shaw
Sort tailwind classes for each elements className list by a given order-config

eslint-plugin-tailwind-classname-order This eslint plugin automatically orders the tailwind classes included in the className tags from each element b

aacn 4 Nov 25, 2022
This package will help parse OData strings (only the Microsoft Dataverse subset). It can be used as a validator, or you can build some javascript library which consumes the output of this library.

@albanian-xrm/dataverse-odata This package will help parse OData strings (only the Microsoft Dataverse subset). It can be used as a validator, or you

AlbanianXrm 3 Oct 22, 2022
A simple browser extension, intended to get you "Back To Work" when you start slacking off to one of those really addictive sites.

Back to Work A simple browser extension, intended to get you Back To Work when you start slacking off to one of those really addictive sites. What doe

Dheeraj Lalwani 29 Nov 19, 2022
A guide for your daily "professional" interactions (not really)

How to professionally say A guide for your daily professional interactions. A simple static website for common phrases we might want to say to your co

Akash Rajpurohit 979 Dec 14, 2022
Million is a lightweight (<1kb) Virtual DOM. It's really fast and makes it easy to create user interfaces.

?? Watch Video ?? Read the docs ?? Join our Discord What is Million? Million is a lightweight (<1kb) Virtual DOM. It's really fast and makes it easy t

Derek Jones 5 Aug 24, 2022
🚀 A really straight-forward SurrealDB Rest API wrapper.

SurrealDB Node.js npm i surrealdb Quick Start surreal start --root user --pass user import SurrealDB from 'surrealdb' const Surreal = new SurrealDB('

Jareer Abdullah 5 Dec 10, 2022
A lightweight JavaScript library that renders text in a brilliant style by displaying strings of random characters before the actual text.

cryptoWriter.js A lightweight javascript library which creates brilliant text animation by rendering strings of random characters before the actual te

Keshav Bajaj 2 Sep 13, 2022
Small js library to animate some writing effect through a list of strings. It also supports settings for typo errors, to make it more human likely.

Write and Delete Small js library to animate some writing effect through a list of strings. It also supports settings for typo errors, to make it more

fabs 2 Nov 15, 2022
A JavaScript library built on top of the Faker.JS library. It generates massive amounts of fake data in the browser and node.js.

Blaver - generate massive amounts of fake data in the browser and node.js Blaver is a JavaScript library built on top of the Faker.JS library. It gene

Priyansh 113 Dec 30, 2022
Emoji - Use emoji names instead of Unicode strings. Copy-pasting emoji sucks.

Grammy Emoji Adds emoji parsing for grammY. Check out the official documentation for this plugin. While this draft is working, we still do not recomme

null 8 Sep 5, 2022