-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add polymorphic callback with unit tests
- Loading branch information
1 parent
56d8a6d
commit f5cd198
Showing
6 changed files
with
80 additions
and
1 deletion.
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,33 @@ | ||
var commaDelimiter = require('./commaDelimiter'); | ||
var pipeDelimiter = require('./pipeDelimiter'); | ||
|
||
buildFerociousCats({ list: '', delimiter: commaDelimiter }, 'Panther', getJaguar); | ||
buildFerociousCats({ list: '', delimiter: pipeDelimiter }, 'Panther', getJaguar); | ||
|
||
function getJaguar(cat) { | ||
buildFerociousCats(cat, 'Jaguar', getLynx); | ||
} | ||
|
||
function getLynx(cat) { | ||
buildFerociousCats(cat, 'Lynx', getSnowLeopard); | ||
} | ||
|
||
function getSnowLeopard(cat) { | ||
buildFerociousCats(cat, 'Snow Leopard', getLion); | ||
} | ||
|
||
function getLion(cat) { | ||
buildFerociousCats(cat, 'Lion', printList); | ||
} | ||
|
||
function printList(cat) { | ||
console.log(cat.list); | ||
} | ||
|
||
function buildFerociousCats(cat, returnValue, next) { | ||
setTimeout(function asyncCall(data) { | ||
var catList = cat.delimiter(cat.list, data); | ||
|
||
next({ list: catList, delimiter: cat.delimiter }); | ||
}, 1, returnValue); | ||
} |
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,5 @@ | ||
function commaDelimiter(list, cat) { | ||
return list === '' ? cat : list + ',' + cat; | ||
} | ||
|
||
module.exports = commaDelimiter; |
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
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,5 @@ | ||
function pipeDelimiter(list, cat) { | ||
return list === '' ? cat : list + '|' + cat; | ||
} | ||
|
||
module.exports = pipeDelimiter; |
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,18 @@ | ||
var commaDelimiter = require('../commaDelimiter'); | ||
var describe = require('mocha/lib/mocha.js').describe; | ||
var it = require('mocha/lib/mocha.js').it; | ||
var assert = require('chai').assert; | ||
|
||
describe('A pipe delimiter', function () { | ||
it('does not add pipe with an empty list', function () { | ||
var list = commaDelimiter('', 'Cat'); | ||
|
||
assert.equal(list, 'Cat'); | ||
}); | ||
|
||
it('adds a pipe in the list', function () { | ||
var list = commaDelimiter('Cat', 'Cat'); | ||
|
||
assert.equal(list, 'Cat,Cat'); | ||
}); | ||
}); |
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,18 @@ | ||
var pipeDelimiter = require('../pipeDelimiter'); | ||
var describe = require('mocha/lib/mocha.js').describe; | ||
var it = require('mocha/lib/mocha.js').it; | ||
var assert = require('chai').assert; | ||
|
||
describe('A pipe delimiter', function () { | ||
it('does not add pipe with an empty list', function () { | ||
var list = pipeDelimiter('', 'Cat'); | ||
|
||
assert.equal(list, 'Cat'); | ||
}); | ||
|
||
it('adds a pipe in the list', function () { | ||
var list = pipeDelimiter('Cat', 'Cat'); | ||
|
||
assert.equal(list, 'Cat|Cat'); | ||
}); | ||
}); |