npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@grinstead/classnames

v1.0.3

Published

A simple, performance-oriented utility for optionally joining css classnames

Downloads

3

Readme

Classnames

A small, memory-efficient npm package for dynamically concatenating CSS class names. This package provides various functions taking certain numbers of arguments, though its main function is classnames, all filtering out falsy values to ensure clean and valid class strings.

The plain classnames function will avoid creating arrays under the hood, and will even avoid creating new strings if only one of the classnames is truthy.

Installation

You can install this package using npm:

npm install @grinstead/classnames

Usage

classnames

Concatenates up to three class names, filtering out falsy values. Returns undefined if all inputs are falsy.

import { classnames } from "@grinstead/classnames";

const className = classnames("class1", "class2", "class3"); // 'class1 class2 class3'
const twoClassNames = classnames("class1", "", "class3"); // 'class1 class3'
const nullClassName = classnames("class1", null, "class3"); // 'class1 class3'
const falseClassName = classnames(false, "class2", "class3"); // 'class2 class3'
const singleClassName = classnames("class1"); // 'class1'
const emptyClassName = classnames(0, "", ""); // undefined
const anotherEmptyClassName = classnames(undefined, undefined, undefined); // undefined

classnames1

Generates a single class name, or returns undefined if the input is falsy.

import { classnames1 } from "@grinstead/classnames";

const className = classnames1("class1"); // 'class1'
const emptyClassName = classnames1(""); // undefined
const nullClassName = classnames1(null); // undefined
const falseClassName = classnames1(false); // undefined

classnames2

Concatenates two class names, filtering out falsy values. Returns undefined if both inputs are falsy.

import { classnames2 } from "@grinstead/classnames";

const className = classnames2("class1", "class2"); // 'class1 class2'
const singleClassName = classnames2("class1", ""); // 'class1'
const nullClassName = classnames2(null, "class2"); // 'class2'
const falseClassName = classnames2("class1", false); // 'class1'
const undefinedClassName = classnames2("", undefined); // undefined

classnames3

An alias for the classnames function, providing the same functionality but declaring the arguments in the typescript as non-optional.

import { classnames3 } from "@grinstead/classnames";

const className = classnames3("class1", "class2", "class3"); // 'class1 class2 class3'

classnames4

Concatenates four class names. Returns undefined if all inputs are falsy.

import { classnames4 } from "@grinstead/classnames";

const className = classnames4("class1", "class2", "class3", "class4"); // 'class1 class2 class3 class4'
const twoClassNames = classnames4("class1", "", "class3", ""); // 'class1 class3'
const singleClassName = classnames4("", "", "class3", ""); // 'class3'
const emptyClassName = classnames4("", "", "", ""); // undefined

classnamesMany

Concatenates any number of class names, filtering out falsy values. Returns undefined if all inputs are falsy.

import { classnamesMany } from "@grinstead/classnames";

const className = classnamesMany("class1", "class2", "class3"); // 'class1 class2 class3'
const twoClassNames = classnamesMany("class1", "", "class3"); // 'class1 class3'
const emptyClassName = classnamesMany("", "", ""); // undefined

classnamesJoin

Concatenates am array of class names, filtering out falsy values. Returns undefined if all inputs are falsy.

import { classnamesJoin } from "@grinstead/classnames";

const className = classnamesJoin(["class1", "class2", "class3"]); // 'class1 class2 class3'
const emptyClassName = classnamesJoin(["", "", ""]); // undefined

License

This project is licensed under the UNLICENSE, which means you can use it however you like - see the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any bugs or feature requests.

Author