From 6d89f47aa1af0b1c20f81e53b264c870e32d4e87 Mon Sep 17 00:00:00 2001 From: Remil Marzan Date: Wed, 15 Feb 2017 13:57:50 -0800 Subject: [PATCH 1/3] setup eslintrc and gitignore files --- .eslintrc | 22 ++++++++++ .gitignore | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 0 3 files changed, 139 insertions(+) create mode 100644 .eslintrc create mode 100644 .gitignore create mode 100644 index.js diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..afa95a5 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,22 @@ +{ + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": [ "error", "single" ], + "semi": ["error", "always"], + "linebreak-style": [ "error", "unix" ], + "comma-dangle": ["error", "always-multiline"] + }, + "env": { + "es6": true, + "node": true, + "mocha": true, + "jasmine": true + }, + "ecmaFeatures": { + "modules": true, + "experimentalObjectRestSpread": true, + "impliedStrict": true + }, + "extends": "eslint:recommended" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0992345 --- /dev/null +++ b/.gitignore @@ -0,0 +1,117 @@ +# Created by https://www.gitignore.io/api/node,vim,osx,macos,linux + +*node_modules + +### Node ### +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules +jspm_packages + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + + + +### Vim ### +# swap +[._]*.s[a-v][a-z] +[._]*.sw[a-p] +[._]s[a-v][a-z] +[._]sw[a-p] +# session +Session.vim +# temporary +.netrwhist +*~ +# auto-generated tag files +tags + + +### OSX ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon +# Thumbnails +._* +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + + +### macOS ### +# Icon must end with two \r +# Thumbnails +# Files that might appear in the root of a volume +# Directories potentially created on remote AFP share + + +### Linux ### + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +# End of https://www.gitignore.io/api/node,vim,osx,macos,linux diff --git a/index.js b/index.js new file mode 100644 index 0000000..e69de29 From db5c2e92266772d3e7388040f0b40e9c594e875b Mon Sep 17 00:00:00 2001 From: Remil Marzan Date: Wed, 15 Feb 2017 17:14:32 -0800 Subject: [PATCH 2/3] completed fileLogger function --- .eslintrc | 2 +- data/one.txt | 1 + data/three.txt | 1 + data/two.txt | 1 + index.js | 7 +++++++ lib/filelogger.js | 30 ++++++++++++++++++++++++++++++ package.json | 26 ++++++++++++++++++++++++++ 7 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 data/one.txt create mode 100644 data/three.txt create mode 100644 data/two.txt create mode 100644 lib/filelogger.js create mode 100644 package.json diff --git a/.eslintrc b/.eslintrc index afa95a5..51a1bcb 100644 --- a/.eslintrc +++ b/.eslintrc @@ -4,7 +4,7 @@ "indent": [ "error", 2 ], "quotes": [ "error", "single" ], "semi": ["error", "always"], - "linebreak-style": [ "error", "unix" ], + "linebreak-style": [ "error", "windows" ], "comma-dangle": ["error", "always-multiline"] }, "env": { diff --git a/data/one.txt b/data/one.txt new file mode 100644 index 0000000..42a7c15 --- /dev/null +++ b/data/one.txt @@ -0,0 +1 @@ +Ain't nothin but a g-thang diff --git a/data/three.txt b/data/three.txt new file mode 100644 index 0000000..79d0e11 --- /dev/null +++ b/data/three.txt @@ -0,0 +1 @@ +All hail the redskins!!! diff --git a/data/two.txt b/data/two.txt new file mode 100644 index 0000000..758ca61 --- /dev/null +++ b/data/two.txt @@ -0,0 +1 @@ +The doppity doppest doptacular dopeness eva diff --git a/index.js b/index.js index e69de29..eeb5cf2 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,7 @@ +'use strict'; + +const fileLogger = require('./lib/filelogger.js'); + +fileLogger([`${__dirname}/data/one.txt`, `${__dirname}/data/two.txt`, `${__dirname}/data/three.txt`], function(result) { + console.log(result); +}); diff --git a/lib/filelogger.js b/lib/filelogger.js new file mode 100644 index 0000000..d8f5947 --- /dev/null +++ b/lib/filelogger.js @@ -0,0 +1,30 @@ +'use strict'; + +const fs = require('fs'); + +const fileLogger = module.exports = function(files, callback) { + let resultArray = files.map(function(file){ + return { + path: file, + }; + }); + + let filterBufferHex = function(file) { + return file.toString('hex', 0, 8); + }; + + for(let i=0; i < files.length; i++) { + let readFile = files[i]; + fs.readFile(readFile, function(err, data) { + if (err) throw err; + resultArray[files.indexOf(readFile)].result = data; + if (resultArray.filter(function(file){ + return Object.keys(file).includes('result'); + }).length === files.length) { + resultArray.forEach(function(file) { + callback(`File '${file.path.substr(file.path.lastIndexOf('/') + 1)}': ${filterBufferHex(file.result)}`); + }); + } + }); + } +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..ebb83a4 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "lab-remil", + "version": "1.0.0", + "description": "![CF](https://camo.githubusercontent.com/70edab54bba80edb7493cad3135e9606781cbb6b/687474703a2f2f692e696d6775722e636f6d2f377635415363382e706e67) Lab 03: Parallel File Processing\r ===", + "main": "index.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/remilonwheels/03-parallel_file_processing.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/remilonwheels/03-parallel_file_processing/issues" + }, + "homepage": "https://github.com/remilonwheels/03-parallel_file_processing#readme", + "dependencies": { + "bluebird": "^3.4.7" + } +} From 3246e25b44f3664bcdbc60e6436732a57b3a0ad4 Mon Sep 17 00:00:00 2001 From: Remil Marzan Date: Wed, 15 Feb 2017 19:42:52 -0800 Subject: [PATCH 3/3] completed chai tests --- gulpfile.js | 23 ++++++++++++ index.js | 3 +- lib/filelogger.js | 14 +++---- package.json | 7 ++++ test/filelogger-test.js | 81 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 gulpfile.js create mode 100644 test/filelogger-test.js diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..99cdd70 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,23 @@ +'use strict'; + +const gulp = require('gulp'); +const eslint = require('gulp-eslint'); +const mocha = require('gulp-mocha'); + +gulp.task('test', function gulpTest(){ + gulp.src('./test/*-test.js', {read: false}) + .pipe(mocha({reporter: 'spec'})); +}); + +gulp.task('lint', function gulpLint(){ + return gulp.src(['**/*.js', '!node_modules/**']) + .pipe(eslint()) + .pipe(eslint.format()) + .pipe(eslint.failAfterError()); +}); + +gulp.task('dev', function gulpDev(){ + gulp.watch(['**/*.js', '!node_modules/**'], ['lint', 'test']); +}); + +gulp.task('default', ['dev']); diff --git a/index.js b/index.js index eeb5cf2..75f9444 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const fileLogger = require('./lib/filelogger.js'); -fileLogger([`${__dirname}/data/one.txt`, `${__dirname}/data/two.txt`, `${__dirname}/data/three.txt`], function(result) { +fileLogger([`${__dirname}/data/one.txt`, `${__dirname}/data/two.txt`, `${__dirname}/data/three.txt`], function(err, result) { + if(err) throw err; console.log(result); }); diff --git a/lib/filelogger.js b/lib/filelogger.js index d8f5947..488d1f7 100644 --- a/lib/filelogger.js +++ b/lib/filelogger.js @@ -2,28 +2,26 @@ const fs = require('fs'); -const fileLogger = module.exports = function(files, callback) { +module.exports = function fileLogger(files, callback) { let resultArray = files.map(function(file){ return { path: file, }; }); - let filterBufferHex = function(file) { - return file.toString('hex', 0, 8); + let filterBufferHex = function(bufferObj) { + return bufferObj.toString('hex', 0, 8); }; for(let i=0; i < files.length; i++) { let readFile = files[i]; fs.readFile(readFile, function(err, data) { - if (err) throw err; - resultArray[files.indexOf(readFile)].result = data; + if (err) return callback(err); + resultArray[files.indexOf(readFile)].result = filterBufferHex(data); if (resultArray.filter(function(file){ return Object.keys(file).includes('result'); }).length === files.length) { - resultArray.forEach(function(file) { - callback(`File '${file.path.substr(file.path.lastIndexOf('/') + 1)}': ${filterBufferHex(file.result)}`); - }); + return callback(null, resultArray); } }); } diff --git a/package.json b/package.json index ebb83a4..e045643 100644 --- a/package.json +++ b/package.json @@ -22,5 +22,12 @@ "homepage": "https://github.com/remilonwheels/03-parallel_file_processing#readme", "dependencies": { "bluebird": "^3.4.7" + }, + "devDependencies": { + "chai": "^3.5.0", + "gulp": "^3.9.1", + "gulp-eslint": "^3.0.1", + "gulp-mocha": "^3.0.1", + "mocha": "^3.2.0" } } diff --git a/test/filelogger-test.js b/test/filelogger-test.js new file mode 100644 index 0000000..8428f72 --- /dev/null +++ b/test/filelogger-test.js @@ -0,0 +1,81 @@ +'use strict'; + +const fileLogger = require('../lib/fileLogger.js'); +const expect = require('chai').expect; + +describe('FileLogger Module', function() { + describe('with an improper file path', function(){ + it('should return an error', function(done) { + let badFilePath = 'badfile/sobad'; + fileLogger([badFilePath], function(err) { + expect(err).to.be.an('error'); + done(); + }); + }); + }); + + describe('with one proper file path passed in an array', function() { + let input = [`${__dirname}/../data/one.txt`]; + it('should return an array of the same length as the input array', function(done) { + fileLogger(input , function(err, data) { + expect(err).to.equal(null); + expect(data).to.be.an('array'); + expect(data.length).to.equal(input.length); + done(); + }); + }); + + it('returned array should have a result property that has value of the first 8 bits in hex', function(done) { + fileLogger(input , function(err, data) { + expect(data[0]).to.have.property('result'); + expect(data[0].result.length).to.equal(16); + expect(data[0].result).to.equal('41696e2774206e6f'); + done(); + }); + }); + }); + + describe('with three files passed in array', function(){ + let input = [`${__dirname}/../data/one.txt`, `${__dirname}/../data/two.txt`, `${__dirname}/../data/three.txt`]; + it('should return an array of the same length as the input array', function(done){ + fileLogger(input, function(err, data) { + expect(err).to.equal(null); + expect(data).to.be.an('array'); + expect(data.length).to.equal(input.length); + done(); + }); + }); + + it('returned array should be in the same order as the input array', function(done){ + fileLogger(input, function(err, data) { + expect(data[0].path).to.equal(input[0]); + expect(data[1].path).to.equal(input[1]); + expect(data[2].path).to.equal(input[2]); + done(); + }); + + let inputDiff = [ `${__dirname}/../data/three.txt`,`${__dirname}/../data/one.txt`, `${__dirname}/../data/two.txt`]; + fileLogger(inputDiff, function(err, data) { + expect(data[0].path).to.equal(input[0]); + expect(data[1].path).to.equal(input[1]); + expect(data[2].path).to.equal(input[2]); + done(); + }); + }); + + it('returned array should have a result property that has value of the first 8 bits in hex', function(done) { + fileLogger(input , function(err, data) { + expect(data[0]).to.have.property('result'); + expect(data[1]).to.have.property('result'); + expect(data[2]).to.have.property('result'); + expect(data[0].result.length).to.equal(16); + expect(data[1].result.length).to.equal(16); + expect(data[2].result.length).to.equal(16); + expect(data[0].result).to.equal('41696e2774206e6f'); + expect(data[1].result).to.equal('54686520646f7070'); + expect(data[2].result).to.equal('416c6c206861696c'); + done(); + }); + }); + }); +});