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

enhanced-switch

v1.1.9

Published

An enhanced switch, similar to Java

Downloads

22

Readme

EnhancedSwitch

npm npm downloads Tests CodeQL

An enhanced switch, similar to Java.

This library targets Node.js 16, 18 and 20, but can also work in the browser.

Installation

npm i enhanced-switch

Usage

TypeScript

import EnhancedSwitch from "enhanced-switch";

const value = 2;

new EnhancedSwitch(value)
    .case(1, () => console.log("one"))
    .case(2, () => console.log("two"))
    .default(() => console.log("default"));

const result = new EnhancedSwitch<number, string>(value)
    .case(1, () => "one")
    .case(2, () => "two")
    .default(() => "default").value;

JavaScript

import EnhancedSwitch from "enhanced-switch";

const value = 2;

new EnhancedSwitch(value)
    .case(1, () => console.log("one"))
    .case(2, () => console.log("two"))
    .default(() => console.log("default"));

const result = new EnhancedSwitch(value)
    .case(1, () => "one")
    .case(2, () => "two")
    .default(() => "default").value;

Documentation

Class EnhancedSwitch<T, U>

The EnhancedSwitch evaluates an expression, matching the expression's value against a series of case clauses, and executes the statements after the first case clause with a matching value. The default clause of an EnhancedSwitch will be jumped to if no case matches the expression's value.

| Template parameter | Description | |--------------------|------------------------| | T | Switch expression type | | U | Switch return type |

new EnhancedSwitch(expression: T)

Create a new EnhancedSwitch

| Parameter | Type | Description | |--------------|------|-------------------------------------------------------------------| | expression | T | An expression whose result is matched against each case clause. |

enhancedSwitch.allowFallthrough

Whether to allow fallthrough. If true, the switch will continue to execute case clauses after the first match until a break is encountered. Defaults to false.

Type: boolean. Read-only.

enhancedSwitch.break()

Prevent further execution of any subsequent case or default clauses.

Returns: this (EnhancedSwitch<T, U>)

enhancedSwitch.case(valueN: T | T[], code: U)

A case clause used to match against expression. If the expression matches the specified valueN (which can be any expression), this case clause is executed.

| Parameter | Type | Description | |-----------|------|----------------------------------------------------------------------------------------------------------------| | valueN | T | The case clause is executed if the expression matches this value, or at least one value if this is an array. | | code | U | The value to return if the case matches. |

Returns: this (EnhancedSwitch<T, U>)

enhancedSwitch.case(valueN: T | T[], code: (s: EnhancedSwitch<T, U>) => U | void)

A case clause used to match against expression. If the expression matches the specified valueN (which can be any expression), this case clause is executed.

| Parameter | Type | Description | |-----------|------------------------------------------|----------------------------------------------------------------------------------------------------------------| | valueN | T | The case clause is executed if the expression matches this value, or at least one value if this is an array. | | code | (s: EnhancedSwitch<T, U>) => U \| void | The function to run if the case matches. |

Returns: this (EnhancedSwitch<T, U>)

enhancedSwitch.default(code: U)

A default clause; if provided, this clause is executed if the value of expression does not match any of the case clauses. An EnhancedSwitch can only have one default clause.

| Parameter | Type | Description | |-----------|------|-----------------------------------------| | code | U | The value to return if no case matches. |

Returns: this (EnhancedSwitch<T, U>)

enhancedSwitch.default(code: (s: EnhancedSwitch<T, U>) => U | void)

A default clause; if provided, this clause is executed if the value of expression does not match any of the case clauses. An EnhancedSwitch can only have one default clause.

| Parameter | Type | Description | |-----------|------------------------------------------|------------------------------------------| | code | (s: EnhancedSwitch<T, U>) => U \| void | The function to run if no case matches. |

Returns: this (EnhancedSwitch<T, U>)

enhancedSwitch.default(code: U, returnvalueN: true)

A default clause; if provided, this clause is executed if the value of expression does not match any of the case clauses. An EnhancedSwitch can only have one default clause.

| Parameter | Type | Description | |----------------|--------|-------------------------------------------------------------------------| | code | U | The value to return if no case matches. | | returnvalueN | true | If true, the provided value is returned instead of the switch instance. |

Returns: U

enhancedSwitch.default(code: (s: EnhancedSwitch<T, U>) => U | void, returnvalueN: true)

A default clause; if provided, this clause is executed if the value of expression does not match any of the case clauses. An EnhancedSwitch can only have one default clause.

| Parameter | Type | Description | |----------------|------------------------------------------|-------------------------------------------------------------------------| | code | (s: EnhancedSwitch<T, U>) => U \| void | The function to run if no case matches. | | returnvalueN | true | If true, the provided value is returned instead of the switch instance. |

Returns: U

enhancedSwitch.expression

An expression whose result is matched against each case clause.

Type: T. Read-only.

enhancedSwitch.hasConcluded

Whether the switch statement has concluded (no further case or default will be executed). A switch can be concluded by calling enhancedSwitch.break() or constructing with allowFallthrough set to false.

Type: boolean. Read-only.

enhancedSwitch.value

The result of the switch statement.

| Throws | When | |-----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------| | TypeError | If the switch statement has no result, e.g. there is no default clause and no case clause matches the expression or returns a value. |

Type: U. Read-only.