Better object matching
npm install switch-match> Better object matching
console
$ npm i -S switch-match
`Usage
`js
match(val: string, obj: object, def: any): any
`
---`js
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:
`js
export default (state = {}, action) => match(action.type, {
[FETCH_POSTS]: () => ({...state, posts: action.posts})
}, state);
``