Giter Club home page Giter Club logo

switche's Introduction

Switche

Have you ever wished TypeScript had an expression-syntax equivalent of switch? I have. Unfortunately, JavaScript/TypeScript switch doesn't return a value. You can fake it with a function, something like this:

const result = (() => {
    switch (value) {
        case 1: return 'One';
        case 2: return 'Two';
        default: return 'Neither'
    }
})();

That's a lot of code, though, and you need to add a return type annotation if you want some kind of enforcement of the return type (in the above example, you can change 'two' to 2 and TypeScript won't complain).

Switche gives you a nicer syntax to achieve the same result, with a little more type safety.

Value Syntax

Provide the value to test in a call to switche, then provide multiple .case calls, and finally a .default call. The result of the call chain will be the second parameter of the first matching case, or else the parameter passed to .default

const result = switche(value)
    .case(1, 'One')
    .case(2, 'Two')
    .default('Neither');

Predicate syntax

As well as providing values to match against, you can provide predicate functions:

const result = switche(value)
    .case(v => v < 0, 'Negative')
    .case(1, 'One')
    .case(2, 'Two')
    .default('Neither');

Throw if not matched

Instead of providing a default, you can tell Switche to throw an error if there is no match. .orThrow takes two optional parameters: an error message and an error type. If you provide an error type, it must inherit from Error and accept a string in the constructor.

const result = switche(value)
    .case(1, 'One')
    .orThrow('The provided value was not 1');
class CustomError extends Error {    
    constructor(details) {
        super(`Custom error being thrown with message '${details}'`);
    }
}
const result = switche(value)
    .case(1, 'One')
    .orThrow('The provided value was not 1', CustomError);

If your custom error constructor doesn't take a single string parameter or you otherwise want to control the creation of the error, you can pass a function as the first parameter which will be called to create the error to throw. In this case, the second parameter is ignored.

class ComplexCustomError extends Error {
    constructor(errorCode: number) {
        super(`Error thrown with code ${errorCode}`);
    }
}
try {
    const result = switche(i)
    .case(2, 'Two')
    .orThrow(() => new ComplexCustomError(5));
} catch (ex) {
    ex;//?
}

Type Safety

const result = switche(value)
    .case(1, 'One')
    .case(2, 'Two')
    .default('Neither'); // Expression type: string

Unlike in the switch example above, if you replace one of the strings with something of a different type, you'll get a type error. All to case or default must be of the same type as the first parameter to the first case call.

const result = switche(value)
    .case(1, 'One')
    .case(2, 2) // Type error: Argument of type 'number' is not assignable to parameter of type 'string'.
    .default('Neither');

You can control the result type using generic parameters. For example, if you want to allow either strings or numbers, you can provide string|number as a generic parameter to the first case call.

const result = switche(value)
    .case<string|number>(1, 'One')
    .case(2, 2)
    .default('Neither'); // Expression type: string|number

You can place the generic parameter immediately after the switche call as well, but then you have to provide the input type as the first parameter (the second parameter, which specifies the output type, is optional):

const result = switche<number,string|number>(value)
    .case(1, 'One')
    .case(2, 2)
    .default('Neither'); // Expression type: string|number

If you provide generic parameters in both places, the second one will be ignored. In this example, string (provided as the generic parameter to switche) will be used, and number|string provided to the first case call will be ignored.

const result = switche<number,string>(value)
    .case<number|string>(1, 'One')
    .case(2, 2) // Type error: Argument of type 'number' is not assignable to parameter of type 'string'.
    .default('Neither');

switche's People

Contributors

rophuine avatar

Watchers

 avatar  avatar

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.