mister-what / ex-patterns

Elixir-style pattern matching and control flow structures for native JavaScript data types and Immutable.js collections

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

build tests

Ex Patterns

This project brings Elixir-style pattern matching and control flow structures to JavaScript. Pattern matching is supported for native JavaScript data types as well as common Immutable.js collections. See the documentation for details and examples.

Setup

Install the package from npm:

npm i ex-patterns

Quick Start Guide

The match Function

A pattern matching function for flat and nested data structures, including arrays, objects and Immutable.js collections:

import { match, _, A } from 'ex-patterns';

const pattern = [_, 2];
const value   = [1, 2];
match(pattern, value)       // match

const pattern = [A, 2];
const value   = [1, 2];
match(pattern, value)       // match against placeholder A  >>  { A: 1 }

The when Function

A switch statement based on pattern matching, similar to Elixir's case control flow structure. It accepts any number of match clauses in the format (pattern, callback) that are matched against a value.

import { when, end, then, _, A, H, N } from 'ex-patterns';

const sayHi = (user) => when(user)
    ({ name:    N }, then(({ N }) => `Hi ${N}!`))
    ({ alias:   A }, then(({ A }) => `Hi ${A}!`))
    ({ handle:  H }, then(({ H }) => `Hi ${H}!`))
    (_, then(() => 'Hi!'))
(end);

sayHi({ name: 'Amelie' });
> 'Hi Amelie!'

The suppose Function

A control flow structure to leverage the power of pattern matching while coding for the happy path, similar to Elixir's with. Takes any number of clauses in the format (pattern, function) and checks if the return value of function matches pattern. Matches are piped through the suppose clauses until the then callback is reached. Can be combined with an optional otherwise clause.

import { suppose, then, otherwise, end, I, N, R } from 'ex-patterns';

const response = await ...

suppose
    ({ status: 200 },             () => response)
    ({ body: { name: N, id: I }}, () => response)
    (true,                        ({ N }) => isValidUserName(N))
    (true,                        ({ I }) => isUniqueUserId(I))
(then)
    (({ N }) => `Welcome ${N}`)
(end);

The cond Function

A compact switch statement that accepts any number of clauses in the format (truthy?, value), similar to Elixir's cond control flow structure:

import { cond, end, then } from 'ex-patterns';

const fizzBuzz = (number) => cond
    (number % 15 === 0, then('fizzbuzz'))
    (number % 3 === 0, then('fizz'))
    (number % 5 === 0, then('buzz'))
    (true, then(number))
(end);

fizzBuzz(5)
> 'buzz'

Want to learn more?

Head over to the documentation for a lot more details and examples!

About

Elixir-style pattern matching and control flow structures for native JavaScript data types and Immutable.js collections

License:MIT License


Languages

Language:TypeScript 99.7%Language:JavaScript 0.3%