2.2.1 • Published 6 years ago

hash-switch v2.2.1

Weekly downloads
8
License
Apache-2.0
Repository
github
Last release
6 years ago

hash-switch

This lib is made to give you the same functionality as a switch statement but with a more natural API.

Compare:

switch(expression) {
  case 'one':
    return 'ONE';
  break;
  case 'two':
    return 'TWO';
  break;
  default:
    return 'DEFAULT';
  break;
}

To:

hs({
  'one':function(){ return 'ONE' },
  'two':function(){ return 'TWO' }
},
function() { return 'DEFAULT' })

For me the second example is a much more familiar syntax and meshes with my other JavaScript more naturally.

Install

npm i hash-switch --save

Usage

var hs = require('hash-switch')({
    'one': function() {return 'ONE'},
    'two': function() {return 'TWO'},
    /* Extra arguments will be passed to the callback function */
    'three': function(four) {return 'THREE' + ' ' + four}
  },
  function(){return 'DEFAULT'}
)

hs('one') //ONE
hs('two') //TWO
/* You can pass an extra argument to the callback function like so */
hs('three', 'FOUR') //THREE FOUR
hs('nope') //DEFAULT
2.2.1

6 years ago

2.2.0

6 years ago

2.1.4

7 years ago

2.1.3

8 years ago

2.1.2

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.0.0

8 years ago