Skip to content

Commit

Permalink
Add polymorphic callback with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beautifulcoder committed Aug 16, 2016
1 parent 56d8a6d commit f5cd198
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
33 changes: 33 additions & 0 deletions callbackPolymorphic.js
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);
}
5 changes: 5 additions & 0 deletions commaDelimiter.js
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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test": "node_modules/.bin/mocha --ui bdd test/**/*.js",
"callback-hell": "node callbackHell.js",
"dependency-inversion": "node callbackDependencyInversion.js",
"polymorphic-callback": "node callbackPoloymorphic.js"
"polymorphic-callback": "node callbackPolymorphic.js"
},
"devDependencies": {
"mocha": "3.0.1",
Expand Down
5 changes: 5 additions & 0 deletions pipeDelimiter.js
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;
18 changes: 18 additions & 0 deletions test/commaDelimiter.test.js
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');
});
});
18 changes: 18 additions & 0 deletions test/pipeDelimiter.test.js
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');
});
});

0 comments on commit f5cd198

Please sign in to comment.