Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristofer Joseph authored and Kristofer Joseph committed Jun 27, 2016
0 parents commit ae1820b
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
Empty file added README.md
Empty file.
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var DEFAULT_CALLBACK = 'DEFAULT_CALLBACK'

module.exports = function(actions, defaultCallback){
var hash = {}
hash[DEFAULT_CALLBACK] = defaultCallback || function() {}
if (actions && actions.length) {
actions.forEach(function(a) {
if (
typeof a === 'object' &&
a.action &&
typeof a.action === 'string' &&
a.callback &&
typeof a.callback === 'function'
) {
hash[a.action] = a.callback
} else {
throw Error("Actions should be an Object with the properties 'action' ( a string constant ) and 'callback' ( a function )")
}
})
}
return function(action) {
if (
action &&
typeof action === 'string'
) {
return (hash[action] || hash[DEFAULT_CALLBACK])()
} else {
throw Error('requires an action string')
}
}
}

27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "hashswitch",
"version": "1.0.0",
"description": "Object hash based switch pattern",
"main": "index.js",
"scripts": {
"test": "tape test.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kristoferjoseph/hash-switch.git"
},
"keywords": [
"hash",
"switch",
"object"
],
"author": "@dam",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/kristoferjoseph/hash-switch/issues"
},
"homepage": "https://github.com/kristoferjoseph/hash-switch#readme",
"devDependencies": {
"tape": "^4.6.0"
}
}
67 changes: 67 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var test = require('tape')
var hs = require('./')

test('switch should exist', function(t) {
t.plan(1)
t.ok(hs)
})

test('should accept minimal arguments', function(t) {
t.plan(1)
t.ok(hs([]))
})

test('should call defaultCallback', function(t) {
t.plan(1)
var hash = hs(
[{
action: 'yolo',
callback: function() {
return 'YOLO'
}
}],
function(){return 'FWEE'}
)
t.equal(hash('fwee'), 'FWEE')
})

test('should call action callback', function(t) {
t.plan(1)
var hash = hs(
[{
action: 'yolo',
callback: function() {
return 'YOLO'
}
}],
function(){return 'FWEE'}
)
t.equal(hash('yolo'), 'YOLO')
})

test('should register multipe action callbacks', function(t) {
t.plan(4)
var hash = hs(
[{
action: 'yolo',
callback: function() {
return 'YOLO'
}
},{
action: 'enzo',
callback: function() {
return 'SUP'
}
},{
action: 'lazlo',
callback: function() {
return 'DUDE'
}
}],
function(){return 'FWEE'}
)
t.equal(hash('yolo'), 'YOLO')
t.equal(hash('enzo'), 'SUP')
t.equal(hash('lazlo'), 'DUDE')
t.equal(hash('foo'), 'FWEE')
})

0 comments on commit ae1820b

Please sign in to comment.