match-default

v3.0.0
matches string to object key, if no match calls default case
match pattern default

match-default

Pass in a string, and object contain functions that could possibly match string + a default function denoted with the _ key. Made because I think this looks really nice with redux, semi inspired by the pattern matching implementation in Rust.

use

npm i --save match-default

import match from 'match-default';
const data = match({
    a: () => 'nice',
    b: () => 'cool',
    string: () => 'woo',
    _: () => ''
}, 'string');

Currying

The function can also be curried by passing in a single argument of an object. A function will be returned expecting a single argument of a string to match against the provided object.

import match from 'match-default';
const matcher = match({cool: () => 'wow', _: () => {}});
const myMatch = matcher('cool');
console.log(myMatch) //myMatch === 'wow'
npm i match-default

Metadata

  • MIT
  • Whatever
  • Conor Hastings
  • released 8/1/2016

Downloads

Maintainers