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

react-match-case

v0.1.0

Published

React library for pattern matching and conditional rendering

Downloads

7

Readme

react-match-case

Components allowing you to use pattern matching in react, without any hassle.

This package uses package matchto (link: https://www.npmjs.com/package/matchto). For matchto utility functions, import { utils } from package.

Example (from unit tests)


const rendered = renderer.create(
    <Match item="Hello world" kind="all">
        <Case pattern={/^Hello/}>
            1{" "}
        </Case>
        <Case pattern="Nope">
            2{" "}
        </Case>
        <Case pattern="Hello world">
            3
        </Case>
    </Match>
);
expect(rendered.toJSON()?.toString()).toBe("1, ,3");

Example of delayed evaluation (useful for null checks)


const arrays = [
    [1, 2, 3],
    null,
];
const rendered = renderer.create(
    <>
        <Match item={arrays[0]}>
            <Case pattern={null}>
                null
            </Case>
            <Case pattern={[]}>
                {() => arrays[0]!.length}
            </Case>
        </Match>
        <Match item={arrays[1]}>
            <Case pattern={null}>
                null
            </Case>
            <Case pattern={[]}>
                {() => arrays[1]!.length}
            </Case>
        </Match>
    </>
);
expect(rendered.toJSON()?.toString()).toBe("3,null");

Example of one-line pattern matching using Pattern component


const rendered = renderer.create(
    <>
        <Pattern item="Hello world" to={/^Hello/}>
            1{" "}
        </Pattern>
        <Pattern item="Hello world" to="Nope">
            2{" "}
        </Pattern>
        <Pattern item="Hello world" to="Hello world">
            3
        </Pattern>
    </>
);
expect(rendered.toJSON()?.toString()).toBe("1, ,3");

Example of using prolog-like cut (if one case is true don't evaluate the rest)


const rendered = renderer.create(
    <Match item={{ a: 5, b: 10 }} kind="all">
        <Case pattern={{ a: 5, b: 10 }} cut>
            1{" "}
        </Case>
        <Case pattern={{ a: 5 }}>
            2{" "}
        </Case>
    </Match>
);
expect(rendered.toJSON()?.toString()).toBe("1, ");

Example of end-tail recursion with exact match (two objects exact equality)


const rendered = renderer.create(
    <Match item={[1, 2, 3, 4]}>
        <Case pattern={[]} exact></Case>
        <Case pattern={[utils.Any]} rematch={item => item.slice(1)}>
            {(items: number[]) => items[0]}
        </Case>
    </Match>
);
expect(rendered.toJSON()?.toString()).toBe("1,2,3,4");

Example of using negation


const rendered = renderer.create(
    <Match item="Hello world">
        <Case pattern={Number} not>
            1{" "}
        </Case>
        <Case pattern={String} not>
            2{" "}
        </Case>
    </Match>
);
expect(rendered.toJSON()?.toString()).toBe("1, ");

Example of guard conditions


const rendered = renderer.create(
    <Match item="Hello world">
        <Case pattern={String} guard={s => s.length > 5}>
            1{" "}
        </Case>
        <Case pattern={String} guard={s => s.length <= 5}>
            2{" "}
        </Case>
    </Match>
);
expect(rendered.toJSON()?.toString()).toBe("1, ");

Footer

If you have any ideas on how to improve this package, or have discovered any bugs, please fill out an issue or pull request.