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

merge-class

v1.3.0

Published

Merge-class effortlessly creates and combines className strings without style conflicts.

Downloads

8

Readme

merge-class

Effortlessly creates and merges className strings without style conflicts using clsx and tailwind-merge.

npm license

Table of Contents

Installation

Using npm:

npm i merge-class

Using pnpm:

pnpm add merge-class

Using yarn:

yarn add merge-class

Usage

Import mc from merge-class:

import { mc } from 'merge-class';

const className = mc('bg-red-500', 'hover:bg-red-600', 'active:bg-red-700');
console.log(className); // output: 'bg-red-500 hover:bg-red-600 active:bg-red-700'

Tests

The mc (merge class) function is designed to combine class names into a single string, handling various cases such as conditional classes, undefined/null values, duplicates, arrays, nested arrays, and objects with boolean values. It also prioritizes Tailwind utility classes correctly.

Test Cases

Combine Class Names

  • Description: Should combine class names into a single string.

  • Test:

    expect(mc('font-bold', 'w-4')).toBe('font-bold w-4');

Handle Conditional Classes

  • Description: Should handle conditional classes correctly.

  • Test:

    const condition = true;
    expect(mc('font-bold', condition && 'w-4')).toBe('font-bold w-4');

Eliminate Undefined and Null Values

  • Description: Should eliminate undefined and null values.

  • Test:

    expect(mc('font-bold', undefined, 'w-4', null)).toBe('font-bold w-4');

Merge Duplicate Classes with Tailwind Utility Classes Priority

  • Description: Should merge duplicate classes and prioritize Tailwind utility classes.

  • Test:

    expect(mc('p-4', 'p-2')).toBe('p-2');

Handle Arrays of Classes

  • Description: Should handle arrays of classes.

  • Test:

    expect(mc(['font-bold', 'w-4'], ['px-2'])).toBe('font-bold w-4 px-2');

Handle Nested Arrays of Classes

  • Description: Should handle nested arrays of classes.

  • Test:

    expect(mc(['font-bold', ['w-4', 'px-2']])).toBe('font-bold w-4 px-2');

Process Objects with Boolean Values

  • Description: Should process objects with boolean values.

  • Test:

    expect(mc({ 'font-bold': true, 'w-4': false, 'px-2': true })).toBe('font-bold px-2');

Handle a Mix of Types

  • Description: Should handle a mix of types.

  • Test:

    const condition = false;
    expect(mc('font-bold', ['w-4', { 'px-2': true, 'h-5': condition }])).toBe('font-bold w-4 px-2');

Prioritize Correctly with Tailwind Utilities

  • Description: Should prioritize correctly with Tailwind utilities.

  • Test:

    expect(mc('text-red-500', 'text-blue-500')).toBe('text-blue-500');