pattern-case
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Node.js CI

Pattern Matching in Typescript

Fast and efficient pattern matching in Typescript.

test('early exit if the condition has met', () => {
  let executed = false;
  pattern<[number, number, number]>(1, 2, 3)
    .case(1, 2, 3)((a, b, c) => {return 1})
    .case(1, 2, 3)((a, b, c) => {executed = true; return 1})
    .match();
  expect(executed).toBe(false);
});

Fall-through

The function next could be used as a placeholder to continue with the next statement.

test('fall-through if the callback is not provided', () => {
  const result = pattern<[number, number, number]>(1, 2, 3)
    .case(1, 2, 3)(next)
    .case(1, 2, 3)((a, b, c) => {return 1})
    .match();
  expect(result).toBe(1);
});

Happy hacking!

Package Sidebar

Install

npm i pattern-case

Weekly Downloads

12

Version

2.0.0

License

MIT

Unpacked Size

24.7 kB

Total Files

10

Last publish

Collaborators

  • fatih-erikli