JSPM

pattern-matching-ts

1.0.3
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 25
    • Score
      100M100P100Q56252F
    • License MIT

    Pattern Matching in typescript

    Package Exports

    • pattern-matching-ts

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (pattern-matching-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    Pattern matching in Typescript.

    Pattern Matching is a declarative much more powerful and less verbose alternative to imperatives "if/else" conditions.
    A definition can be found inside Scala Documentation

    “Pattern matching tests whether a given value (or sequence of values) has the shape defined by a pattern, and, if it does, binds the variables in the pattern to the corresponding components of the value (or sequence of values).”

    In Functional Programming languages, there're built-in keywords for Pattern Matching. Typescript though is one language that works very well with Functional Programming but lacks this feature.
    This package aims to bring Pattern Matching feature to Typescript through Discriminated Union Types / Algebraic Data Types.

    npm version


    Index

    Installation

    yarn

    yarn add pattern-matching-ts

    npm

    npm install --save pattern-matching-ts

    Usage

    Option Monad Example

    import * as M from 'pattern-matching-ts'
    
    interface None {
      readonly _tag: 'None'
    }
    
    interface Some<A> {
      readonly _tag: 'Some'
      readonly value: A
    }
    
    type Option<A> = None | Some<A>
    
    const optionMatching = M.match<Option<string>, string>({
        Some: (x) => `Something: ${x.value}`,
        None: () => 'Nothing'
    })
    
    assert.deepStrictEqual(optionMatching(O.some('data')), 'Something: data')
    

    Default Example

    import * as M from 'pattern-matching-ts'
    
    interface ChangeColor<T = number> {
      readonly _tag: 'ChangeColor'
      readonly value: {
        readonly r: T
        readonly g: T
        readonly b: T
      }
    }
    interface Move<T = number> {
      readonly _tag: 'Move'
      readonly value: {
        readonly x: T
        readonly y: T 
      }
    }
    
    interface Write {
      readonly _tag: 'Write'
      readonly value: {
        readonly text: string
      }
    }
    
    type Cases = ChangeColor<number> | Move | Write 
    
    const matchMessage = M.match<Cases, string>({
        ChangeColor: ({ value: { r, g, b } }) => `Red: ${r} | Green: ${g} | Blue: ${b}`,
        Move: ({ value: { x, y } }) => `Move in the x direction: ${x} and in the y direction: ${y}`,
        Write: ({ value: { text } }) => `Text message: ${text}`,
        _: () => 'Default message'
    })
    
    const ChangeColor = ({ r, g, b }: ChangeColor<number>['value']): ChangeColor<number> => ({
       _tag: 'ChangeColor', value: { r, g, b }
    })
    
    assert.deepStrictEqual(matchMessage(ChangeColor({ r: 12, g: 20, b: 30 })),'Red: 12 | Green: 20 | Blue: 30')
    
    assert.deepStrictEqual(matchMessage(null), 'Default message')

    Here's a blog post that introduces the API. 👉 Pattern Matching in Typescript

    MIT