2.0.4 • Published 10 years ago

switch-match v2.0.4

Weekly downloads
5
License
MIT
Repository
github
Last release
10 years ago

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

2.0.4

10 years ago

2.0.3

10 years ago

2.0.2

10 years ago

2.0.1

10 years ago

2.0.0

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.0.0

10 years ago