switch-match
v2.0.4
Published
Better object matching
Downloads
54
Readme
Switch-match
Better object matching
Installation
$ npm i -S switch-match
Usage
match(val: string, obj: object, def: any): any
var match = require('switch-match');
match('x', {
x: 2,
y: 1
}, 100); // => 2
match('none', {
x: 2,
y: 1
}, 0); // => 0
match('none-without-default', {
x: 2,
y: 1
}); // => undefined
match('fn', {
fn: function(val) {
return val;
},
y: 1
}); // => 'fn', function's call result (val)
Personally, i wrote this module for usage with redux
and es6
, it fits pretty nice, but you can use it wherever you want to:
export default (state = {}, action) => match(action.type, {
[FETCH_POSTS]: () => ({...state, posts: action.posts})
}, state);
Related
License
MIT © Aleksandr Yakunichev