From e6a0b0131fe3bcf766490841cf2cf57b35f7da4c Mon Sep 17 00:00:00 2001 From: Dana Kulp Date: Wed, 15 Feb 2017 12:56:12 -0800 Subject: [PATCH 01/10] Scaffolding file structure for lab assignment with package.json and .gitignore created first. --- .gitignore | 114 +++++++++++++++++++++++++++++++++++++++ index.js | 0 lib/file-reader.js | 0 package.json | 20 +++++++ test/file-reader-test.js | 0 5 files changed, 134 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 lib/file-reader.js create mode 100644 package.json create mode 100644 test/file-reader-test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b97253f --- /dev/null +++ b/.gitignore @@ -0,0 +1,114 @@ + +# Created by https://www.gitignore.io/api/node,macos,windows + +### macOS ### +*.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 + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.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 + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# 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 + +# dotenv environment variables file +.env + + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.gitignore.io/api/node,macos,windows diff --git a/index.js b/index.js new file mode 100644 index 0000000..e69de29 diff --git a/lib/file-reader.js b/lib/file-reader.js new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json new file mode 100644 index 0000000..da44a7d --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "lab03", + "version": "1.0.0", + "description": "![CF](https://camo.githubusercontent.com/70edab54bba80edb7493cad3135e9606781cbb6b/687474703a2f2f692e696d6775722e636f6d2f377635415363382e706e67) Lab 03: Parallel File Processing ===", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/dkulp23/03-parallel_file_processing.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/dkulp23/03-parallel_file_processing/issues" + }, + "homepage": "https://github.com/dkulp23/03-parallel_file_processing#readme" +} diff --git a/test/file-reader-test.js b/test/file-reader-test.js new file mode 100644 index 0000000..e69de29 From 4e6c6ec91d48ba97d470bc8fef464c41134b0957 Mon Sep 17 00:00:00 2001 From: Dana Kulp Date: Wed, 15 Feb 2017 13:13:11 -0800 Subject: [PATCH 02/10] Added ipsum text to files in data directory. Started to write reader method but realized that I should do a commit first. --- data/one.txt | 3 +++ data/three.txt | 3 +++ data/two.txt | 3 +++ lib/file-reader.js | 1 + 4 files changed, 10 insertions(+) create mode 100644 data/one.txt create mode 100644 data/three.txt create mode 100644 data/two.txt diff --git a/data/one.txt b/data/one.txt new file mode 100644 index 0000000..2817955 --- /dev/null +++ b/data/one.txt @@ -0,0 +1,3 @@ +Enamel pin fashion axe cred, offal tilde paleo glossier art party unicorn narwhal chicharrones church-key hexagon small batch. Photo booth helvetica quinoa, thundercats lumbersexual deep v mixtape pour-over single-origin coffee whatever. Fam scenester edison bulb farm-to-table, cornhole kinfolk seitan pabst 90's kombucha la croix normcore activated charcoal mumblecore. Polaroid before they sold out 8-bit, etsy pitchfork man bun normcore succulents knausgaard humblebrag franzen kitsch marfa organic actually. Franzen knausgaard kinfolk etsy ugh. Bitters roof party tumblr hammock. Gochujang kinfolk pickled enamel pin jean shorts. + +Enamel pin cold-pressed hoodie, kale chips VHS man braid flexitarian quinoa unicorn pinterest taxidermy vinyl. Echo park tumblr brooklyn, craft beer wayfarers skateboard pug. Forage glossier single-origin coffee kitsch, bicycle rights messenger bag hella chicharrones shabby chic. Tumblr williamsburg truffaut master cleanse unicorn, kickstarter snackwave lo-fi vinyl DIY gentrify photo booth. Umami health goth yuccie, asymmetrical pinterest food truck chartreuse cardigan aesthetic. Drinking vinegar fixie pork belly beard twee, knausgaard hammock typewriter paleo. Microdosing truffaut tofu shabby chic normcore food truck. diff --git a/data/three.txt b/data/three.txt new file mode 100644 index 0000000..0850f8e --- /dev/null +++ b/data/three.txt @@ -0,0 +1,3 @@ +Succulents schlitz skateboard asymmetrical trust fund fingerstache. Pug fingerstache taxidermy, slow-carb meggings messenger bag unicorn subway tile. Hashtag hoodie cornhole quinoa wolf yuccie polaroid, man bun cronut dreamcatcher slow-carb small batch man braid. Sriracha kinfolk whatever, slow-carb gluten-free la croix swag. Copper mug edison bulb tofu aesthetic bespoke try-hard cold-pressed. Direct trade lo-fi small batch, lumbersexual venmo cray ethical post-ironic pork belly lomo poutine tilde try-hard craft beer twee. Dreamcatcher bespoke organic, occupy put a bird on it tumeric literally. + +Keffiyeh fap letterpress succulents, man braid slow-carb activated charcoal. Etsy hammock food truck, jean shorts blue bottle migas small batch bitters bicycle rights yuccie poutine. Cronut 3 wolf moon poke, squid tumblr disrupt offal retro bushwick fingerstache fanny pack williamsburg. Cray actually gluten-free, fashion axe sustainable raw denim keytar flexitarian kogi 90's. Whatever lomo trust fund fam ethical, freegan yuccie next level franzen viral. Man bun literally cornhole, kogi master cleanse brunch marfa seitan farm-to-table XOXO. Church-key meh poutine etsy fap normcore plaid, pickled ugh pabst pinterest. diff --git a/data/two.txt b/data/two.txt new file mode 100644 index 0000000..e35bf6e --- /dev/null +++ b/data/two.txt @@ -0,0 +1,3 @@ +Slow-carb post-ironic la croix, direct trade cray umami irony ugh woke kogi hella. Pickled messenger bag twee, pour-over before they sold out jianbing tbh mustache man braid master cleanse. Leggings wolf man braid heirloom, neutra celiac 8-bit sustainable gastropub tilde glossier four dollar toast you probably haven't heard of them tousled. Banh mi ethical live-edge, trust fund schlitz echo park hot chicken vexillologist. Hot chicken shoreditch live-edge, hexagon tilde twee small batch. Etsy post-ironic pitchfork, kinfolk gastropub chartreuse cred prism humblebrag. Asymmetrical lumbersexual iPhone food truck, 3 wolf moon skateboard cold-pressed slow-carb mixtape biodiesel vaporware cornhole health goth. + +Seitan williamsburg literally, raw denim pork belly wolf hoodie tote bag hammock skateboard. Tumeric subway tile ugh la croix 3 wolf moon. Meh slow-carb af, bushwick church-key tilde kitsch offal swag four loko deep v. Fap scenester portland, chambray forage craft beer deep v sriracha occupy fam affogato. Twee tumblr post-ironic distillery, trust fund freegan mustache YOLO church-key cray unicorn. Pinterest slow-carb banjo 90's. Everyday carry single-origin coffee intelligentsia, mlkshk unicorn aesthetic forage lomo cronut chillwave la croix. diff --git a/lib/file-reader.js b/lib/file-reader.js index e69de29..ad9a93a 100644 --- a/lib/file-reader.js +++ b/lib/file-reader.js @@ -0,0 +1 @@ +'use strict'; From 8ef8b1df045b000ece09fb7be5ce76badae372c2 Mon Sep 17 00:00:00 2001 From: Dana Kulp Date: Wed, 15 Feb 2017 14:56:03 -0800 Subject: [PATCH 03/10] Scaffolded inital readFile function. --- .eslintrc | 21 +++++++++++++++++++++ index.js | 5 +++++ lib/file-reader.js | 13 +++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 .eslintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..8dc6807 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,21 @@ +{ + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": [ "error", "single" ], + "semi": ["error", "always"], + "linebreak-style": [ "error", "unix" ] + }, + "env": { + "es6": true, + "node": true, + "mocha": true, + "jasmine": true + }, + "ecmaFeatures": { + "modules": true, + "experimentalObjectRestSpread": true, + "impliedStrict": true + }, + "extends": "eslint:recommended" +} diff --git a/index.js b/index.js index e69de29..c6ced87 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,5 @@ +'use strict'; + +const logFiles = require('./lib/file-reader.js'); + +logFiles.fileReader('../data/one.txt'); diff --git a/lib/file-reader.js b/lib/file-reader.js index ad9a93a..a7b8492 100644 --- a/lib/file-reader.js +++ b/lib/file-reader.js @@ -1 +1,14 @@ 'use strict'; + +const fs = require('fs'); + +module.exports = exports = {}; + +exports.fileReader = function() { + fs.readFile(`${__dirname}/../data/one.txt`, function(err, data) { + if (err) throw err; + let binary = data.slice(0, 16); + console.log(binary.toString()); + // return callback(null, binary.string()); + }); +}; From 2f48265d0da3831fc9f8dff1176c8d87f3e1ec54 Mon Sep 17 00:00:00 2001 From: Dana Kulp Date: Wed, 15 Feb 2017 14:58:30 -0800 Subject: [PATCH 04/10] Installed mocha and chai as developer dependencies. Also installed gulp hoping to add automation script. --- package.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index da44a7d..d192905 100644 --- a/package.json +++ b/package.json @@ -16,5 +16,10 @@ "bugs": { "url": "https://github.com/dkulp23/03-parallel_file_processing/issues" }, - "homepage": "https://github.com/dkulp23/03-parallel_file_processing#readme" + "homepage": "https://github.com/dkulp23/03-parallel_file_processing#readme", + "devDependencies": { + "chai": "^3.5.0", + "gulp": "^3.9.1", + "mocha": "^3.2.0" + } } From 34c018f249b115e59c8e4a50880c1dc867332b1d Mon Sep 17 00:00:00 2001 From: Dana Kulp Date: Wed, 15 Feb 2017 15:28:12 -0800 Subject: [PATCH 05/10] Added first test for imaginary file path. --- index.js | 2 +- lib/file-reader.js | 8 ++++---- test/file-reader-test.js | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index c6ced87..350e706 100644 --- a/index.js +++ b/index.js @@ -2,4 +2,4 @@ const logFiles = require('./lib/file-reader.js'); -logFiles.fileReader('../data/one.txt'); +logFiles.fileReader('one.txt', 'two.txt', 'three.txt'); diff --git a/lib/file-reader.js b/lib/file-reader.js index a7b8492..2f54ef8 100644 --- a/lib/file-reader.js +++ b/lib/file-reader.js @@ -4,11 +4,11 @@ const fs = require('fs'); module.exports = exports = {}; -exports.fileReader = function() { - fs.readFile(`${__dirname}/../data/one.txt`, function(err, data) { - if (err) throw err; +exports.fileReader = function(file, callback) { + fs.readFile(file, function(err, data) { + if (err) return callback(err); let binary = data.slice(0, 16); console.log(binary.toString()); - // return callback(null, binary.string()); + return callback(null, binary.string()); }); }; diff --git a/test/file-reader-test.js b/test/file-reader-test.js index e69de29..f457fe6 100644 --- a/test/file-reader-test.js +++ b/test/file-reader-test.js @@ -0,0 +1,15 @@ +'use strict'; + +const expect = require('chai').expect; +const readFile = require('../lib/file-reader.js').fileReader; + +describe('File Reader Module', function() { + describe('test false file path', function() { + it('should return an error message', function(done) { + readFile(`${__dirname}/imaginary-file.txt`, function(err) { + expect(err).to.be.an('error'); + done(); + }); + }); + }); +}); From 38c1c8b0c20ad6a6919a251e22ebefa62baca36d Mon Sep 17 00:00:00 2001 From: Dana Kulp Date: Wed, 15 Feb 2017 21:44:27 -0800 Subject: [PATCH 06/10] Struggling to figure out how to make the files log sequentially. --- index.js | 6 ++++-- lib/dir-reader.js | 10 ++++++++++ lib/file-reader.js | 20 ++++++++++++++------ test/file-reader-test.js | 12 +++++++++++- 4 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 lib/dir-reader.js diff --git a/index.js b/index.js index 350e706..cb25be6 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ 'use strict'; -const logFiles = require('./lib/file-reader.js'); +// const dir = require('./lib/dir-reader.js').dataFiles; +const reader = require('./lib/file-reader.js').readFiles; +const handler = require('./lib/file-reader.js').handleData; -logFiles.fileReader('one.txt', 'two.txt', 'three.txt'); +reader(['one.txt', 'two.txt', 'three.txt'], handler); diff --git a/lib/dir-reader.js b/lib/dir-reader.js new file mode 100644 index 0000000..73dc16d --- /dev/null +++ b/lib/dir-reader.js @@ -0,0 +1,10 @@ +// 'use strict'; +// +// const fs = require('fs'); +// +// const dataFiles = module.exports = function() { +// fs.readdir(`${__dirname}/../data`, function(err, data) { +// if (err) throw err; +// return data; +// }); +// }; diff --git a/lib/file-reader.js b/lib/file-reader.js index 2f54ef8..64189cc 100644 --- a/lib/file-reader.js +++ b/lib/file-reader.js @@ -4,11 +4,19 @@ const fs = require('fs'); module.exports = exports = {}; -exports.fileReader = function(file, callback) { - fs.readFile(file, function(err, data) { - if (err) return callback(err); - let binary = data.slice(0, 16); - console.log(binary.toString()); - return callback(null, binary.string()); +exports.readFiles = function(file, callback) { + file.map(function(ele) { + fs.readFile(`${__dirname}/../data/${ele}`, function(err, data) { + if (err) return callback(err); + return callback(null, data); + }); }); }; + +exports.handleData = function(err, data) { + if (err) throw err; + let binary = data.slice(0, 15); + let string = binary.toString(); + console.log(string); + return string; +}; diff --git a/test/file-reader-test.js b/test/file-reader-test.js index f457fe6..6c2d98e 100644 --- a/test/file-reader-test.js +++ b/test/file-reader-test.js @@ -1,7 +1,7 @@ 'use strict'; const expect = require('chai').expect; -const readFile = require('../lib/file-reader.js').fileReader; +const readFile = require('../lib/file-reader.js').readFiles; describe('File Reader Module', function() { describe('test false file path', function() { @@ -12,4 +12,14 @@ describe('File Reader Module', function() { }); }); }); + + describe('test actual file paths', function() { + it('should return the first 16 hex digits', function(done) { + readFile('one.txt', function(err, data) { + expect(err).to.equal(null); + expect(data).to.be.a('string'); + done(); + }); + }); + }); }); From 4aec979a48347f0eb00accfe1330de9b80a9241c Mon Sep 17 00:00:00 2001 From: Dana Kulp Date: Wed, 15 Feb 2017 23:41:31 -0800 Subject: [PATCH 07/10] Getting error message that done is getting called more than once. --- index.js | 12 +++++------- lib/file-reader.js | 39 +++++++++++++++++++++++++++------------ test/file-reader-test.js | 6 +++--- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index cb25be6..b41fea2 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,5 @@ -'use strict'; - -// const dir = require('./lib/dir-reader.js').dataFiles; -const reader = require('./lib/file-reader.js').readFiles; -const handler = require('./lib/file-reader.js').handleData; - -reader(['one.txt', 'two.txt', 'three.txt'], handler); +// 'use strict'; +// +// const reader = require('./lib/file-reader.js').readFiles; +// +// reader(['one.txt', 'two.txt', 'three.txt']); diff --git a/lib/file-reader.js b/lib/file-reader.js index 64189cc..cceb313 100644 --- a/lib/file-reader.js +++ b/lib/file-reader.js @@ -5,18 +5,33 @@ const fs = require('fs'); module.exports = exports = {}; exports.readFiles = function(file, callback) { - file.map(function(ele) { - fs.readFile(`${__dirname}/../data/${ele}`, function(err, data) { - if (err) return callback(err); - return callback(null, data); + if (file instanceof Array) {//linter wouldn't let me use (! instanceof) + file.map(function(ele) { + fs.readFile(`${__dirname}/../data/${ele}`, function(err, data) { + if (err) return callback(err); + let string = data.toString('hex').substring(0, 8); + console.log(string); + return callback(null, string); + }); }); - }); + } else { + throw new Error('file name array not found'); + } }; -exports.handleData = function(err, data) { - if (err) throw err; - let binary = data.slice(0, 15); - let string = binary.toString(); - console.log(string); - return string; -}; +// exports.readFiles = function(file, callback) { +// if (file instanceof Array) {//linter wouldn't let me use (! instanceof) +// let fileResults = file.reduce(function(acc, ele) { +// fs.readFile(`${__dirname}/../data/${ele}`, function(err, data) { +// if (err) throw err; +// let string = data.toString('hex').substring(0, 8); +// acc.push(string); +// return acc; +// }); +// }, []); +// // callback(null, fileResults); +// console.log(fileResults); +// } else { +// throw new Error('file name array not found'); +// } +// }; diff --git a/test/file-reader-test.js b/test/file-reader-test.js index 6c2d98e..9af45be 100644 --- a/test/file-reader-test.js +++ b/test/file-reader-test.js @@ -6,7 +6,7 @@ const readFile = require('../lib/file-reader.js').readFiles; describe('File Reader Module', function() { describe('test false file path', function() { it('should return an error message', function(done) { - readFile(`${__dirname}/imaginary-file.txt`, function(err) { + readFile(['imaginary-file.txt'], function(err) { expect(err).to.be.an('error'); done(); }); @@ -14,8 +14,8 @@ describe('File Reader Module', function() { }); describe('test actual file paths', function() { - it('should return the first 16 hex digits', function(done) { - readFile('one.txt', function(err, data) { + it('should return the first 8 hex digits', function(done) { + readFile(['one.txt', 'two.txt', 'three.txt'], function(err, data) { expect(err).to.equal(null); expect(data).to.be.a('string'); done(); From e081ea898c11eebe240a99a054ef51defe584a7e Mon Sep 17 00:00:00 2001 From: Dana Kulp Date: Sun, 26 Feb 2017 17:25:42 -0800 Subject: [PATCH 08/10] Refactoring file reader and added gulp file. --- gulpfile.js | 23 ++++++++++++++++++++++ index.js | 13 +++++++----- lib/file-reader.js | 49 +++++++++++++++++++++++++++++++++------------- package.json | 2 ++ 4 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 gulpfile.js diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..9ae97d6 --- /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('lint', function() { + return gulp.src(['**/*.js', '!node_modules']) + .pipe(eslint()) + .pipe(eslint.format()) + .pipe(eslint.failAfterError()); +}); + +gulp.task('test', function() { + gulp.src('./test/*-test.js', { read: false }) + .pipe(mocha({ reporter: 'spec'})); +}); + +gulp.task('dev', function() { + gulp.watch(['**/*.js', '!node_modules'], ['lint', 'test']); +}); + +gulp.task('default', ['lint', 'test']); \ No newline at end of file diff --git a/index.js b/index.js index b41fea2..a019466 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,8 @@ -// 'use strict'; -// -// const reader = require('./lib/file-reader.js').readFiles; -// -// reader(['one.txt', 'two.txt', 'three.txt']); +'use strict'; + +const reader = require('./lib/file-reader.js').readFiles; + +reader(['one.txt', 'two.txt', 'three.txt'], function(err, data) { + if (err) console.error(err); + console.log('index.js', data); +}); diff --git a/lib/file-reader.js b/lib/file-reader.js index cceb313..bdadfc9 100644 --- a/lib/file-reader.js +++ b/lib/file-reader.js @@ -4,23 +4,28 @@ const fs = require('fs'); module.exports = exports = {}; -exports.readFiles = function(file, callback) { - if (file instanceof Array) {//linter wouldn't let me use (! instanceof) - file.map(function(ele) { - fs.readFile(`${__dirname}/../data/${ele}`, function(err, data) { - if (err) return callback(err); - let string = data.toString('hex').substring(0, 8); - console.log(string); - return callback(null, string); - }); - }); - } else { - throw new Error('file name array not found'); - } -}; +// exports.readFiles = function(file, callback) { +// if (file instanceof Array) {//linter wouldn't let me use (! instanceof) +// let results = []; +// file.map(function(ele) { +// fs.readFile(`${__dirname}/../data/${ele}`, function(err, data) { +// if (err) return callback(err); +// let string = data.toString('hex').substring(0, 8); +// results.push(string); +// console.log('inside readfile', results); +// }); +// console.log('inside map:', results); +// }); +// console.log('inside if:', results); +// return callback(null, results); +// } else { +// throw new Error('file name array not found'); +// } +// }; // exports.readFiles = function(file, callback) { // if (file instanceof Array) {//linter wouldn't let me use (! instanceof) + // let fileResults = file.reduce(function(acc, ele) { // fs.readFile(`${__dirname}/../data/${ele}`, function(err, data) { // if (err) throw err; @@ -35,3 +40,19 @@ exports.readFiles = function(file, callback) { // throw new Error('file name array not found'); // } // }; + +exports.readFiles = function(file, callback) { + if (file instanceof Array) { + let results = []; + file.forEach(function(ele, index, array) { + fs.readFile(`${__dirname}/../data/${ele}`, function(err, data) { + if (err) callback(err); + let string = data.toString('hex').substring(0, 8); + results.push(string); + if (index == (array.length - 1)) callback(null, results); + }) + }); + } else { + callback(new Error('file array not found')); + } +}; diff --git a/package.json b/package.json index d192905..1544890 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,8 @@ "devDependencies": { "chai": "^3.5.0", "gulp": "^3.9.1", + "gulp-eslint": "^3.0.1", + "gulp-mocha": "^4.0.1", "mocha": "^3.2.0" } } From 6c7695a6667671e445a0b88a82caeda4393df242 Mon Sep 17 00:00:00 2001 From: Dana Kulp Date: Sun, 26 Feb 2017 17:56:55 -0800 Subject: [PATCH 09/10] Completed refactor and tests. Halved third file to test async return. --- data/three.txt | 1 - index.js | 2 +- lib/file-reader.js | 43 ++-------------------------------------- test/file-reader-test.js | 15 ++++++++++---- 4 files changed, 14 insertions(+), 47 deletions(-) diff --git a/data/three.txt b/data/three.txt index 0850f8e..3708a3d 100644 --- a/data/three.txt +++ b/data/three.txt @@ -1,3 +1,2 @@ Succulents schlitz skateboard asymmetrical trust fund fingerstache. Pug fingerstache taxidermy, slow-carb meggings messenger bag unicorn subway tile. Hashtag hoodie cornhole quinoa wolf yuccie polaroid, man bun cronut dreamcatcher slow-carb small batch man braid. Sriracha kinfolk whatever, slow-carb gluten-free la croix swag. Copper mug edison bulb tofu aesthetic bespoke try-hard cold-pressed. Direct trade lo-fi small batch, lumbersexual venmo cray ethical post-ironic pork belly lomo poutine tilde try-hard craft beer twee. Dreamcatcher bespoke organic, occupy put a bird on it tumeric literally. -Keffiyeh fap letterpress succulents, man braid slow-carb activated charcoal. Etsy hammock food truck, jean shorts blue bottle migas small batch bitters bicycle rights yuccie poutine. Cronut 3 wolf moon poke, squid tumblr disrupt offal retro bushwick fingerstache fanny pack williamsburg. Cray actually gluten-free, fashion axe sustainable raw denim keytar flexitarian kogi 90's. Whatever lomo trust fund fam ethical, freegan yuccie next level franzen viral. Man bun literally cornhole, kogi master cleanse brunch marfa seitan farm-to-table XOXO. Church-key meh poutine etsy fap normcore plaid, pickled ugh pabst pinterest. diff --git a/index.js b/index.js index a019466..536c891 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ 'use strict'; -const reader = require('./lib/file-reader.js').readFiles; +const reader = require('./lib/file-reader.js'); reader(['one.txt', 'two.txt', 'three.txt'], function(err, data) { if (err) console.error(err); diff --git a/lib/file-reader.js b/lib/file-reader.js index bdadfc9..da23c87 100644 --- a/lib/file-reader.js +++ b/lib/file-reader.js @@ -2,52 +2,13 @@ const fs = require('fs'); -module.exports = exports = {}; - -// exports.readFiles = function(file, callback) { -// if (file instanceof Array) {//linter wouldn't let me use (! instanceof) -// let results = []; -// file.map(function(ele) { -// fs.readFile(`${__dirname}/../data/${ele}`, function(err, data) { -// if (err) return callback(err); -// let string = data.toString('hex').substring(0, 8); -// results.push(string); -// console.log('inside readfile', results); -// }); -// console.log('inside map:', results); -// }); -// console.log('inside if:', results); -// return callback(null, results); -// } else { -// throw new Error('file name array not found'); -// } -// }; - -// exports.readFiles = function(file, callback) { -// if (file instanceof Array) {//linter wouldn't let me use (! instanceof) - -// let fileResults = file.reduce(function(acc, ele) { -// fs.readFile(`${__dirname}/../data/${ele}`, function(err, data) { -// if (err) throw err; -// let string = data.toString('hex').substring(0, 8); -// acc.push(string); -// return acc; -// }); -// }, []); -// // callback(null, fileResults); -// console.log(fileResults); -// } else { -// throw new Error('file name array not found'); -// } -// }; - -exports.readFiles = function(file, callback) { +const readFiles = module.exports = function(file, callback) { if (file instanceof Array) { let results = []; file.forEach(function(ele, index, array) { fs.readFile(`${__dirname}/../data/${ele}`, function(err, data) { if (err) callback(err); - let string = data.toString('hex').substring(0, 8); + let string = data.toString('hex', 0, 8); results.push(string); if (index == (array.length - 1)) callback(null, results); }) diff --git a/test/file-reader-test.js b/test/file-reader-test.js index 9af45be..96bccb2 100644 --- a/test/file-reader-test.js +++ b/test/file-reader-test.js @@ -1,7 +1,7 @@ 'use strict'; const expect = require('chai').expect; -const readFile = require('../lib/file-reader.js').readFiles; +const readFile = require('../lib/file-reader.js'); describe('File Reader Module', function() { describe('test false file path', function() { @@ -14,10 +14,17 @@ describe('File Reader Module', function() { }); describe('test actual file paths', function() { - it('should return the first 8 hex digits', function(done) { - readFile(['one.txt', 'two.txt', 'three.txt'], function(err, data) { + let files = ['one.txt', 'two.txt', 'three.txt']; + let expected = ['456e616d656c2070', '536c6f772d636172', '53756363756c656e']; + it('should return the first 8 hex digits in order', function(done) { + readFile(files, function(err, data) { expect(err).to.equal(null); - expect(data).to.be.a('string'); + expect(data[0]).to.be.a('string'); + expect(data[0]).to.equal(expected[0]); + expect(data[1]).to.be.a('string'); + expect(data[1]).to.equal(expected[1]); + expect(data[2]).to.be.a('string'); + expect(data[2]).to.equal(expected[2]); done(); }); }); From 8357ad9563258d86b4f24e829052bbfaba433ce5 Mon Sep 17 00:00:00 2001 From: Dana Kulp Date: Sun, 26 Feb 2017 18:09:57 -0800 Subject: [PATCH 10/10] Linter errors fixed as much as I could. --- lib/file-reader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/file-reader.js b/lib/file-reader.js index da23c87..77a5de9 100644 --- a/lib/file-reader.js +++ b/lib/file-reader.js @@ -2,7 +2,7 @@ const fs = require('fs'); -const readFiles = module.exports = function(file, callback) { +module.exports = function(file, callback) { if (file instanceof Array) { let results = []; file.forEach(function(ele, index, array) { @@ -11,7 +11,7 @@ const readFiles = module.exports = function(file, callback) { let string = data.toString('hex', 0, 8); results.push(string); if (index == (array.length - 1)) callback(null, results); - }) + }); }); } else { callback(new Error('file array not found'));