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

switch-matcher

v1.0.1

Published

Simple lightweight match pattern

Downloads

8

Readme

Switch Matcher Status in Progress

Simple and lightweight library for handling switch statements and Pattern Matching in JavaScript and TypeScript.

Installation

To install the package, you can use npm or your favorite package manager.

npm install switch-matcher

Features

  • Simple: Uses a syntax similar to JavaScript switch statements and chainable methods.
  • Lightweight: Has few dependencies and is very lightweight.
  • Versatile: Can be used in any JavaScript or TypeScript project.

Usage

Switch-Matcher provides two main interfaces to work with: the Switcher and Matcher classes (the latter through the match function). Here comes a more detailed description of the interfaces.

Switcher

SMSwitcher is a class that facilitates the definition and evaluation of conditions in a structured manner. It allows creating a pipeline through multiple cases (case), a default handler (default), and an alternative handler (else). Additionally, it supports both synchronous and asynchronous evaluations.

First, import the SMSwitcher class.

import { SMSwitcher } from "switch-matcher";

Creating a switcher

const switcher = new SMSwitcher<string, string>();

Defining cases

switcher
  .case("value1", () => "Handled value1")
  .case("value2", "Handled value2")
  .case((value) => value.startsWith("test"), "Handled test values")
  .default(() => "Default handler")
  .elseValue("Else handler");

Evaluating a value

const result = switcher.syncEval("value1"); // Result: 'Handled value1'
const promiseResult = switcher.eval("unknown"); // Result: 'Else handler'

Matcher

SMMatcher is a class similar to SMSwitcher, but designed to evaluate a specific value against multiple conditions more straightforwardly. It offers a direct way to define cases (case), a default handler (default), and an alternative value (else). Unlike Switcher, Matcher evaluates the cases at the moment and does not support asynchronous evaluations.

First, import the match function.

import { match } from "switch-matcher";

Creating a matcher

const matcher = match<string, string>("value1");

Defining cases

matcher
  .case("value1", () => "Handled value1")
  .case("value2", "Handled value2")
  .case((value) => value.startsWith("test"), "Handled test values")
  .default(() => "Default handler")
  .else("Else handler");

Getting the resulting value

const result = matcher.value; // Result: 'Handled value1' or 'Else handler' if no matches

Contributions

If you want to contribute to the project, you can follow these steps:

  1. Fork the project.
  2. Create a new branch.
  3. Make your changes.
  4. Commit your changes.
  5. Push your changes to your fork.
  6. Open a pull request.

And that's it! You can easily contribute to the project and help improve it.

Future Features

Here we will list the features we plan to add to the library in the future.

Credits

This package was developed by LeoLizc. We thank the following people and projects for their contributions:

License

This package is under the MIT license. You can view the license file here.