switch-match

2.0.4 • Public • Published

Switch-match

NPM version Travis Build Status XO code style

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

Readme

Keywords

Package Sidebar

Install

npm i switch-match

Weekly Downloads

5

Version

2.0.4

License

MIT

Last publish

Collaborators

  • canvaskisa