Skip to content

Commit

Permalink
Adds number suppor to actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoferjoseph committed Nov 17, 2017
1 parent b85f4d6 commit 29a25fa
Show file tree
Hide file tree
Showing 3 changed files with 293 additions and 4 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module.exports = function hashswitch (actions, defaultCallback){
var hash = {}
hash.DEFAULT_CALLBACK = defaultCallback || function () {}
actions && Object.keys(actions).forEach(function (action) {
('string' === typeof action && 'function' === typeof actions[action]) ?
(('string' === typeof action || 'number' !== typeof action) && 'function' === typeof actions[action]) ?
(hash[action] = actions[action]) :
console.error(action + ' should be a string and ' + actions[action] + ' should be a function.')
console.error(action + ' should be a string or number and ' + actions[action] + ' should be a function.')
})
return function exectue (action) {
'string' !== typeof action && console.error(action + ' should be a string')
('string' !== typeof action || 'number' !== typeof action) && console.error(action + ' should be a string or number')
return (hash[action] || hash.DEFAULT_CALLBACK).apply(this, [].slice.call(arguments, 1))
}
}
285 changes: 285 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ test('should call action callback', function(t) {
})

test('should register multipe action callbacks', function(t) {
t.plan(4)
t.plan(5)
var hash = hs({
1: function one () {
return 1
},
'yolo': function() {
return 'YOLO'
},
Expand All @@ -56,6 +59,7 @@ test('should register multipe action callbacks', function(t) {
return 'FWEE'
}
)
t.equal(hash(1), 1)
t.equal(hash('yolo'), 'YOLO')
t.equal(hash('enzo'), 'SUP')
t.equal(hash('lazlo'), 'DUDE')
Expand Down

0 comments on commit 29a25fa

Please sign in to comment.