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

@ramstack/alpinegear-match

v1.0.0

Published

@ramstack/alpinegear-match provides the 'x-match' Alpine.js directive, which functions similarly to the 'switch' statement in many programming languages, allowing to conditionally render elements based on matching cases.

Downloads

161

Readme

@ramstack/alpinegear-match

@ramstack/alpinegear-match is a plugin for Alpine.js that provides the x-match directive.

This directive functions similarly to the switch statement in many programming languages, allowing you to conditionally render elements based on matching cases.

Installation

Using CDN

To include the CDN version of this plugin, add the following <script> tag before the core alpine.js file:

<!-- alpine.js plugin -->
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-when@1/alpinegear-match.min.js" defer></script>

<!-- alpine.js -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js" defer></script>

Using NPM

Alternatively, you can install the plugin via npm:

npm install --save @ramstack/alpinegear-match

Then initialize it in your bundle:

import Alpine from "alpinejs";
import match from "@ramstack/alpinegear-match";

Alpine.plugin(match);
Alpine.start();

Usage

The x-match directive is similar to using multiple consecutive x-if or x-when directives. However, using multiple x-if or x-when can make your markup harder to read and lead to code bloat.

The x-match directive provides a cleaner solution by allowing you to define multiple blocks with conditions. The corresponding block will be displayed if its condition evaluates to true.

Here's a simple example solving the classic FizzBuzz game:

<div x-data="{ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] }">
    <template x-for="n in numbers">
        <template x-match>
            <div x-case="n % 3 == 0 && n % 5 == 0">Fizz Buzz</div>
            <div x-case="n % 3 == 0">Fizz</div>
            <div x-case="n % 5 == 0">Buzz</div>
            <div x-default x-text="n"></div>
        </template>
    </template>
</div>

While it's possible to wrap the loop's content in an additional <div> (since x-for only allows a single root element), or use a plugin like @ramstack/alpinegear-fragment, to achieve similar results, the x-match directive provides a much cleaner and more readable approach. Additionally, it avoids introducing extra elements that are only needed to bypass these limitations.

For comparison, here's how the same FizzBuzz game might look using x-fragment:

<div x-data="{ numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] }">
    <template x-for="n in numbers">
        <div>
            <template x-if="n % 3 == 0 && n % 5 == 0">
                <div>Fizz Buzz</div>
            </template>
            <template x-if="n % 3 == 0">
                <div>Fizz</div>
            </template>
            <template x-if="n % 5 == 0">
                <div>Buzz</div>
            </template>
            <template x-if="n % 3 != 0 && n % 5 != 0">
                <div x-text="n"></div>
            </template>
        </div>
    </template>
</div>

[!IMPORTANT] Ensure that x-case conditions are ordered from most specific to least specific. Otherwise, a more general case might intercept the condition, causing subsequent cases not to execute.

[!NOTE] The x-default branch is optional and only renders if none of the x-case conditions evaluate to true.

[!TIP] The x-case directive can be applied to regular HTML tags or <template> tags. When used with <template>, you can define multiple root elements, and all will be rendered.

Source code

You can find the source code for this plugin on GitHub:

https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/match

Contributions

Bug reports and contributions are welcome.

License

This package is released as open source under the MIT License. See the LICENSE file for more details.