npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ex-patterns

v0.8.0

Published

Elixir-style pattern matching and control flow structures

Downloads

198

Readme

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';

> match([_, 2], [1, 2])
[true, { }]

> match([A, 2], [1, 2])
[true, { A: 1 }]

> match([$('first'), $('second')], [1, 2])
[true, { first: 1, second: 2 }]

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, _, A, H, N } from 'ex-patterns';

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

const user = { name: 'Amelie', hometown: 'Stockholm' };

> sayHi(user);
'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,                        ({ 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 } from 'ex-patterns';

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

> fizzBuzz(5)
'buzz'

Want to learn more?

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