-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kristofer Joseph
authored and
Kristofer Joseph
committed
Jun 27, 2016
0 parents
commit ae1820b
Showing
5 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) |