Skip to content

conorhastings/match-default

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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. All arguments with also be passed along to the called function.

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published