1.1.5 • Published 1 month ago

enhanced-switch v1.1.5

Weekly downloads
-
License
LGPL-3.0
Repository
github
Last release
1 month ago

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 parameterDescription
TSwitch expression type
USwitch return type

new EnhancedSwitch(expression: T)

Create a new EnhancedSwitch

ParameterTypeDescription
expressionTAn 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.

ParameterTypeDescription
valueNTThe case clause is executed if the expression matches this value, or at least one value if this is an array.
codeUThe 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.

ParameterTypeDescription
valueNTThe 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 \| voidThe 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.

ParameterTypeDescription
codeUThe 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.

ParameterTypeDescription
code(s: EnhancedSwitch<T, U>) => U \| voidThe 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.

ParameterTypeDescription
codeUThe value to return if no case matches.
returnvalueNtrueIf 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.

ParameterTypeDescription
code(s: EnhancedSwitch<T, U>) => U \| voidThe function to run if no case matches.
returnvalueNtrueIf 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.

ThrowsWhen
TypeErrorIf 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.

1.1.5

1 month ago

1.1.4

2 months ago

1.1.3

4 months ago

1.1.2

4 months ago

1.1.1

6 months ago

1.1.0

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago