Code Monkey home page Code Monkey logo

enhancedswitch's Introduction

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

Table of Contents

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.

enhancedswitch's People

Contributors

zefir-git avatar dependabot[bot] avatar

Watchers

 avatar

enhancedswitch's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.