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

match-expression

v1.2.1

Published

Switch/match expression in method chaining style

Downloads

36

Readme

Match expression

npm Build Status

A flexible switch/match expression utility for Javascript.

import match from 'match-expression'

const x = match('bar')
  .case('foo')
    .then(() => 'FOO')
  .case('bar')
  .case('baz')
    .then(() => 'BARBAZ')
  .default(() => 'DEFAULT')

// x === 'BARBAZ'

Custom comparison function

Apply and get regular expression capture group matches in one go:

const inputUrl = "http://eat-frogs.io/dishes/parmigiana-di-rana"

function regexMatch(str, regex) { return regex.exec(str) }

const httpsUrl = match(inputUrl, regexMatch)
  .case(/^http:\/\/(.*)/)
    .then((url, _, [, noprotocol]) => `https://${noprotocol}`)
  .case(/^\//)
    .then(url => `https://${DOMAIN}${url}`)
  .default(url => url)

// httpsUrl === "https://eat-frogs.io/dishes/parmigiana-di-rana"

No default clause

Execute the function returned by then to resolve to a value without having to use a default clause.

match(person.type)
  .case('HUMAN').then(() => greet(person))
  .case('NOT_HUMAN').then(() => eat(person))()

Interactive examples

API

match( value [, comparisonFunction] )

value: any
The value to be matched against the subsequent cases.
comparisonFunction: (value, caseValue) => any (optional)
Defaults to strict equality (===).
It is passed the initial value as first argument and the comparing value as second. If its result is truthy, then the initial value is interpreted as matching the comparing value.
Its result with the matching value is used a the third argument to the matching then clause.
Once a value has matched, the comparison function is not called anymore.
Returns: { case }
An object with a .case method.

.case( comparisonValue [, ...] )

Available after match and case clauses.
comparisonValue: any
The value(s) to compare with the initial value provided to match. The actual matched value will be passed as second argument to then handler.
Returns: { then, case }
An object with .then and .case methods.

.then( callback )

Available after case clauses.
callback: (value, matchedValue, comparisonFunctionResult) => any
Executed only if a previous case clause matched, in which case its return value will be used as the return value of the match expression.
The first argument is the initial value, the second is the matching case value, the third one is the result of the call to the comparison function with the two previous arguments (defaults to true if no custom comparisonFunction was provided).
Returns: [Callable: () => result]{ case, default }
A callable object with .case and .default methods.
The function can be called to resolve the match directly, without a "default" clause.

.default( callback )

Available after then clauses.
callback: (value) => any
Executed only if no previous case clause matched, in which case its return value will be used as the return value of the match expression. The first argument is the initial value.
Returns: any
Returns resolved value from then clause callback corresponding to the matched case clause, or from its own callback if no case matched.

Notes

  • Checkout ./test.js for an exhaustive spec.
  • Before writing this package I wasn't particularly fond of the builder-style API, so I wrote another match expression module that had a more data-oriented API. The configuration was made through a big array passed to match:
    match: (value, [ ...cases, default ])
    where cases: [ [ ...caseValues, callback ] ] and default: [ callback ].
    Turns out that it's very unreadable for anything non-trivial. So in case the builder style API is an initial turn off for you, put yourself in the shoes of someone reading your code: the case, then and default visual keywords are very helpful in making the code more understandable.
  • Related: if-exp