diff --git a/Stephen_Schroeder/.gitignore b/Stephen_Schroeder/.gitignore new file mode 100644 index 0000000..fd4f2b0 --- /dev/null +++ b/Stephen_Schroeder/.gitignore @@ -0,0 +1,2 @@ +node_modules +.DS_Store diff --git a/Stephen_Schroeder/main.js b/Stephen_Schroeder/main.js new file mode 100644 index 0000000..c4a94ce --- /dev/null +++ b/Stephen_Schroeder/main.js @@ -0,0 +1,35 @@ +'use strict'; +const fs = require('fs'); +const ee = new(require('events').EventEmitter); + +var fileList = ['text_files/one.txt', 'text_files/two.txt', 'text_files/three.txt']; + +var mainReader = module.exports = function(cb) { + let finalArray = []; + fs.readFile('text_files/one.txt', (err, data) => { + console.log(data.toString('hex', 0, 8)); + finalArray.push(0); + ee.emit('one'); + }); + + ee.on('one', () => { + fs.readFile('text_files/two.txt', (err, data) => { + console.log(data.toString('hex', 0, 8)); + finalArray.push(1); + ee.emit('two'); + }); + }); + + ee.on('two', () => { + fs.readFile('text_files/three.txt', (err, data) => { + console.log(data.toString('hex', 0, 8)); + finalArray.push(2); + cb.files + }) + }) +} + +mainReader((finalArray) => { + return finalArray; +}) +debugger; diff --git a/Stephen_Schroeder/package.json b/Stephen_Schroeder/package.json new file mode 100644 index 0000000..7001110 --- /dev/null +++ b/Stephen_Schroeder/package.json @@ -0,0 +1,18 @@ +{ + "name": "fs_and_async", + "version": "0.1.0", + "description": "testing using buffers and event emitters", + "main": "main.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Stephen Schroeder", + "license": "ISC", + "devDependencies": { + "async": "^1.5.2", + "chai": "^3.5.0", + "gulp": "^3.9.1", + "gulp-install": "^0.6.0", + "mocha": "^2.4.5" + } +} diff --git a/Stephen_Schroeder/test/test.js b/Stephen_Schroeder/test/test.js new file mode 100644 index 0000000..6321198 --- /dev/null +++ b/Stephen_Schroeder/test/test.js @@ -0,0 +1,38 @@ +'use strict'; + +const expect = require('chai').expect; +const mainReader = require('../main.js'); +const fs = require('fs'); + +describe('readFile processes', () => { + let files; + before((done) => { + files = []; + fs.readFile(__dirname + '/../text_files/one.txt', (err, data) => { + console.log('one'); + files.push(data.slice(0, 8)); + fs.readFile(__dirname + '/../text_files/two.txt', (err, data) => { + console.log('two'); + files.push(data.slice(0, 8)); + fs.readFile(__dirname + '/../text_files/three.txt', (err, data) => { + console.log('three'); + files.push(data.slice(0, 8)); + done(); + }); + }); + }); + }); + + it('should read files correctly', () => { + mainReader((files) => { + expect(Buffer.isBuffer(files[0])).to.equal(true); + done(); + }); + }); + + it('should keep files in correct order', () => { + mainReader((data) => { + expect(mainReader.finalArray).to.eql('abc'); + }); + }); +}); diff --git a/Stephen_Schroeder/text_files/one.txt b/Stephen_Schroeder/text_files/one.txt new file mode 100644 index 0000000..1071e29 --- /dev/null +++ b/Stephen_Schroeder/text_files/one.txt @@ -0,0 +1 @@ +Larry like cake and pies. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.lorem diff --git a/Stephen_Schroeder/text_files/three.txt b/Stephen_Schroeder/text_files/three.txt new file mode 100644 index 0000000..d866106 --- /dev/null +++ b/Stephen_Schroeder/text_files/three.txt @@ -0,0 +1 @@ +Sally likes weed and beer. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. diff --git a/Stephen_Schroeder/text_files/two.txt b/Stephen_Schroeder/text_files/two.txt new file mode 100644 index 0000000..112a30f --- /dev/null +++ b/Stephen_Schroeder/text_files/two.txt @@ -0,0 +1 @@ +George likes to kill people. Nobody likes George. Don't be like George.Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.