diff --git a/index.js b/index.js index fbf6feb..4cc88b3 100644 --- a/index.js +++ b/index.js @@ -1,11 +1 @@ -module.exports = { - assert : { - equal : function(one, two){ - return one === two - } - }, - run : function(){ - - }, - test : {} -}; \ No newline at end of file +module.exports = require('./lib'); \ No newline at end of file diff --git a/lib/data_structures/linkedList.js b/lib/data_structures/linkedList.js new file mode 100644 index 0000000..a099545 --- /dev/null +++ b/lib/data_structures/linkedList.js @@ -0,0 +1 @@ +module.exports = {}; \ No newline at end of file diff --git a/lib/helper.js b/lib/helper.js new file mode 100644 index 0000000..a38d8b0 --- /dev/null +++ b/lib/helper.js @@ -0,0 +1,20 @@ +var fs = require('fs'); + +module.exports = { + getHTML: function(url, callback) { + + setTimeout(function() { + callback('Hello World'); + }, 10); + }, + + getFile: function(filename, callback) { + + fs.readFile( __dirname + '/' + filename, function (err, data) { + if (err) { + throw err; + } + callback(data.toString()); + }); + } +}; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..ce0d5d6 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,7 @@ +var myLib = require('./mylib') + , linkedList = require('./data_structures/linkedList'); + +module.exports = { + myLib: myLib, + LinkedList: linkedList +}; \ No newline at end of file diff --git a/lib/mylib.js b/lib/mylib.js new file mode 100644 index 0000000..a142f61 --- /dev/null +++ b/lib/mylib.js @@ -0,0 +1,42 @@ +var helper = require('./helper'); + +module.exports = { + assert:{ + equal: function(a,b){ + return a === b; + } + + }, + test:{ + + }, + run: function(){ + + }, + + getBodyContent: function(url, callback) { + + var secondCB = function(html) { + + var bodyContent = html.substring(html.indexOf('')+6, html.indexOf('')); + + callback(bodyContent); + }; + + helper.getHTML(url, secondCB); + }, + + getFileContent: function(filename,callback){ + + helper.getFile(filename, function(data){ + callback(data); + }); + }, + + fib: function(num) { + + if(num <= 2) return 1; + + return this.fib(num-1) + this.fib(num-2); + } +}; \ No newline at end of file diff --git a/lib/testFile.txt b/lib/testFile.txt new file mode 100644 index 0000000..c57eff5 --- /dev/null +++ b/lib/testFile.txt @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/test.js b/test.js new file mode 100644 index 0000000..10d939e --- /dev/null +++ b/test.js @@ -0,0 +1,25 @@ +var syncWait = function(milliseconds) { + var now = new Date().getTime(); + + var stop = now + milliseconds; + + while(stop > new Date().getTime()) { + console.log('waiting...'); + } + + console.log('done!'); +}; + + +console.log('1'); +var myFunction = function(callback) { + console.log('2'); + setTimeout(callback, 1); + console.log('3'); + syncWait(1000); +}; + +myFunction(function() { + console.log('4'); +}); +console.log('5'); \ No newline at end of file diff --git a/test/object_test.js b/test/object_test.js new file mode 100644 index 0000000..81af19e --- /dev/null +++ b/test/object_test.js @@ -0,0 +1,82 @@ +var assert = require('chai').assert; +var myLib = require('../index.js').myLib; + +describe('MyLib', function() { + it('should be an object', function () { + assert.equal(typeof(myLib), 'object'); + }); + + it('should have a property called assert that is an object', function() { + assert.equal(typeof(myLib.assert), 'object'); + }); + + it('should have a property called test that is an object', function() { + assert.equal(typeof(myLib.test), 'object'); + }); + + it('should have a property called run that is a function', function() { + assert.equal(typeof(myLib.run), 'function'); + }); + + describe('assert', function() { + it('should have a property called equal that is a function', function() { + assert.equal(typeof(myLib.assert.equal), 'function'); + }); + + describe('equal', function() { + it('should take two parameters and return whether they are equal or not', function() { + assert.equal(true, myLib.assert.equal(5, 5)); + }); + + it('should take two parameters and return whether they are exactly equal or not', function() { + assert.equal(false, myLib.assert.equal(5, '5')); + }); + }); + }); + + describe('getBodyContent', function() { + + it('should be a function', function() { + assert.equal(typeof(myLib.getBodyContent), 'function'); + }); + + it('should get the HTML of a URL and only return the contents of the tag', function(done) { + + var firstCB = function(bodyContent) { + assert.equal('Hello World', bodyContent); + done(); + }; + + myLib.getBodyContent('https://www.google.com/', firstCB); + }); + }); + + describe('getFileContent', function() { + + it('should be a function', function() { + assert.equal(typeof(myLib.getFileContent), 'function'); + }); + + it('should get the contents of a file and return it', function(done) { + + myLib.getFileContent('testFile.txt', function(content) { + assert.equal('Hello World!', content); + done(); + }); + }); + }); + + describe('fib', function() { + + it('should return the fibonacci number for the provided number', function() { + assert.equal(1, myLib.fib(1)); + assert.equal(1, myLib.fib(2)); + assert.equal(2, myLib.fib(3)); + assert.equal(3, myLib.fib(4)); + assert.equal(5, myLib.fib(5)); + assert.equal(8, myLib.fib(6)); + assert.equal(610, myLib.fib(15)); + + }); + }); +}); \ No newline at end of file diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 476b0cb..0000000 --- a/test/test.js +++ /dev/null @@ -1,36 +0,0 @@ -var assert = require('chai').assert; -var myLib = require('../index.js'); - -describe('MyLib', function() { - it('should be an object', function () { - assert.equal(typeof(myLib), 'object'); - }); - - it('should have a property called assert that is an object', function() { - assert.equal(typeof(myLib.assert), 'object'); - }); - - it('should have a property called test that is an object', function() { - assert.equal(typeof(myLib.test), 'object'); - }); - - it('should have a property called run that is a function', function() { - assert.equal(typeof(myLib.run), 'function'); - }); - - describe('assert', function() { - it('should have a property called equal that is a function', function() { - assert.equal(typeof(myLib.assert.equal), 'function'); - }); - - describe('equal', function() { - it('should take two parameters and return whether they are equal or not', function() { - assert.equal(true, myLib.assert.equal(5, 5)); - }); - - it('should take two parameters and return whether they are exactly equal or not', function() { - assert.equal(false, myLib.assert.equal(5, '5')); - }); - }); - }); -}); \ No newline at end of file