Skip to content

TypeScript match(switch-case) library inspired by Rust.

License

Notifications You must be signed in to change notification settings

ysuzuki19/match.ts

Repository files navigation

match.ts

TypeScript match(switch-case) library inspired by Rust.

How to Use

Simple

import match from 'match.ts';

const status = 'ArrowUp';
match(status, {
  ArrowUp: () => {
    handleUp();
  }, // called!!
  ArrowDown: () => {
    handleDown();
  },
  _: () => console.log(status),
});

With rewrite

import match from 'match.ts';

const status = 'KeyJ';
match(
  status,
  {
    UP: () => {
      handleUp();
    },
    DOWN: () => handleDown(), // called!!
    _: () => console.log(status),
  },
  {
    ArrowDown: 'DOWN',
    KeyJ: 'DOWN',
    ArrowUp: 'UP',
    KeyK: 'UP',
  }
);