From 5a53445ddc1d6fe23a96b910cb0522f1601b8e98 Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Wed, 13 Sep 2017 23:28:00 -0700 Subject: [PATCH 1/5] current progress on readdir() --- lab-christina/.eslintignore | 0 lab-christina/.eslintrc | 0 lab-christina/.gitignore | 128 ++++++++++++++++++++++++ lab-christina/README.md | 0 lab-christina/__test__/asset/betcha.txt | 1 + lab-christina/__test__/asset/data.txt | 1 + lab-christina/__test__/asset/lastly.txt | 1 + lab-christina/__test__/reader.test.js | 20 ++++ lab-christina/lib/reader.js | 11 ++ lab-christina/package.json | 18 ++++ 10 files changed, 180 insertions(+) create mode 100644 lab-christina/.eslintignore create mode 100644 lab-christina/.eslintrc create mode 100644 lab-christina/.gitignore create mode 100644 lab-christina/README.md create mode 100644 lab-christina/__test__/asset/betcha.txt create mode 100644 lab-christina/__test__/asset/data.txt create mode 100644 lab-christina/__test__/asset/lastly.txt create mode 100644 lab-christina/__test__/reader.test.js create mode 100644 lab-christina/lib/reader.js create mode 100644 lab-christina/package.json diff --git a/lab-christina/.eslintignore b/lab-christina/.eslintignore new file mode 100644 index 0000000..e69de29 diff --git a/lab-christina/.eslintrc b/lab-christina/.eslintrc new file mode 100644 index 0000000..e69de29 diff --git a/lab-christina/.gitignore b/lab-christina/.gitignore new file mode 100644 index 0000000..8922811 --- /dev/null +++ b/lab-christina/.gitignore @@ -0,0 +1,128 @@ + +# Created by https://www.gitignore.io/api/osx,node,linux,windows + +### 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* + +### 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 + + +### 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 + +### 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/osx,node,linux,windows diff --git a/lab-christina/README.md b/lab-christina/README.md new file mode 100644 index 0000000..e69de29 diff --git a/lab-christina/__test__/asset/betcha.txt b/lab-christina/__test__/asset/betcha.txt new file mode 100644 index 0000000..48b3b46 --- /dev/null +++ b/lab-christina/__test__/asset/betcha.txt @@ -0,0 +1 @@ +food diff --git a/lab-christina/__test__/asset/data.txt b/lab-christina/__test__/asset/data.txt new file mode 100644 index 0000000..54578ea --- /dev/null +++ b/lab-christina/__test__/asset/data.txt @@ -0,0 +1 @@ +for diff --git a/lab-christina/__test__/asset/lastly.txt b/lab-christina/__test__/asset/lastly.txt new file mode 100644 index 0000000..71af1a9 --- /dev/null +++ b/lab-christina/__test__/asset/lastly.txt @@ -0,0 +1 @@ +words diff --git a/lab-christina/__test__/reader.test.js b/lab-christina/__test__/reader.test.js new file mode 100644 index 0000000..a7f680e --- /dev/null +++ b/lab-christina/__test__/reader.test.js @@ -0,0 +1,20 @@ +'use strict'; + +const reader = require('../lib/reader.js'); + +describe('reader', () => { + test('an invalid path will result in an error message', (done) => { + reader(`${__dirname}/mkdir`, (err, data) => { + expect(err).not.toBeNull(); + expect(data).toBeUndefined(); + done(); + }); + }); + test('a valid path should resolve in a string', (done) => { + reader([`${__dirname}/asset/betcha.txt`,`${__dirname}/asset/data.txt`,`${__dirname}/asset/betcha.txt`],(err, data) => { + expect(err).toBeNull(); + expect(data).toEqual('words', 'for', 'food'); + done(); + }); + }); +}); diff --git a/lab-christina/lib/reader.js b/lab-christina/lib/reader.js new file mode 100644 index 0000000..0d34c3b --- /dev/null +++ b/lab-christina/lib/reader.js @@ -0,0 +1,11 @@ +'use strict'; + +const fs = require('fs'); +module.exports = (path, callback) => { + fs.readFile(path, (err, data) => { + if (err) { + callback(err); + } + console.log('something', data.string()); + }); +}; diff --git a/lab-christina/package.json b/lab-christina/package.json new file mode 100644 index 0000000..f418286 --- /dev/null +++ b/lab-christina/package.json @@ -0,0 +1,18 @@ +{ + "name": "lab-christina", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "jest -i" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "jest": "^21.0.2" + }, + "dependencies": { + "fs": "0.0.1-security" + } +} From ca5a2ce98326d1692dc36f47f0768a08ad267ed4 Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Wed, 13 Sep 2017 23:55:05 -0700 Subject: [PATCH 2/5] moved on for loop - undefined output --- lab-christina/__test__/reader.test.js | 2 +- lab-christina/lib/reader.js | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lab-christina/__test__/reader.test.js b/lab-christina/__test__/reader.test.js index a7f680e..9e96b95 100644 --- a/lab-christina/__test__/reader.test.js +++ b/lab-christina/__test__/reader.test.js @@ -11,7 +11,7 @@ describe('reader', () => { }); }); test('a valid path should resolve in a string', (done) => { - reader([`${__dirname}/asset/betcha.txt`,`${__dirname}/asset/data.txt`,`${__dirname}/asset/betcha.txt`],(err, data) => { + reader([`${__dirname}/asset/betcha.txt`,`${__dirname}/asset/data.txt`, `${__dirname}/asset/lastly.txt`], (err, data) => { expect(err).toBeNull(); expect(data).toEqual('words', 'for', 'food'); done(); diff --git a/lab-christina/lib/reader.js b/lab-christina/lib/reader.js index 0d34c3b..86a8529 100644 --- a/lab-christina/lib/reader.js +++ b/lab-christina/lib/reader.js @@ -2,10 +2,12 @@ const fs = require('fs'); module.exports = (path, callback) => { - fs.readFile(path, (err, data) => { - if (err) { - callback(err); - } - console.log('something', data.string()); - }); + for(var i = 0; i < path.length; i++){ + fs.readFile(path, (err, data) => { + if (err) { + callback(err); + } + console.log(data); + }); + } }; From bb147fb0b1a8d3677cc7f22342f3fe3ec68e79de Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Mon, 25 Sep 2017 11:46:24 -0700 Subject: [PATCH 3/5] README added, functionality added, returning correct data, not mapped yet --- lab-christina/.eslintignore | 5 +++++ lab-christina/.eslintrc.json | 26 ++++++++++++++++++++++++++ lab-christina/README.md | 12 ++++++++++++ lab-christina/__test__/reader.test.js | 7 +++---- lab-christina/{.eslintrc => index.js} | 0 lab-christina/lib/reader.js | 5 +++-- lab-christina/package.json | 3 ++- 7 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 lab-christina/.eslintrc.json rename lab-christina/{.eslintrc => index.js} (100%) diff --git a/lab-christina/.eslintignore b/lab-christina/.eslintignore index e69de29..05b1cf3 100644 --- a/lab-christina/.eslintignore +++ b/lab-christina/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/lab-christina/.eslintrc.json b/lab-christina/.eslintrc.json new file mode 100644 index 0000000..3315dc6 --- /dev/null +++ b/lab-christina/.eslintrc.json @@ -0,0 +1,26 @@ +{ + "env": { + "browser": true, + "node": true, + "commonjs": true, + "mocha": true, + "es6": true + }, + "globals": { + "err": true, + "req": true, + "res": true, + "next": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": ["error", "single", { "allowTemplateLiterals": true }], + "comma-dangle": ["error", "always-multiline"], + "semi": [ "error", "always" ] + } +} diff --git a/lab-christina/README.md b/lab-christina/README.md index e69de29..8097474 100644 --- a/lab-christina/README.md +++ b/lab-christina/README.md @@ -0,0 +1,12 @@ +## 03-asynchronous-callbacks +### reader.js module +* Exports a function that loops though an array of filepaths. The function has an arity of two. +* Using fs readFIle the files are translated from buffer objects to 'utf-8' +* fs readFile is in error first data last format. +* If the filepath is undefined you will recieve an error. + +### reader.test.js + +* In the test file I've created two test. +* The first test ensures that an error will be returned in an invalid filepath is provided. +* The second tests purpose is to test the data is returned in a mapped array of strings. I am currently looping through all files and recieved the correct data. I've yet to accomplish mapping the return into an array. diff --git a/lab-christina/__test__/reader.test.js b/lab-christina/__test__/reader.test.js index 9e96b95..e868ff8 100644 --- a/lab-christina/__test__/reader.test.js +++ b/lab-christina/__test__/reader.test.js @@ -5,15 +5,14 @@ const reader = require('../lib/reader.js'); describe('reader', () => { test('an invalid path will result in an error message', (done) => { reader(`${__dirname}/mkdir`, (err, data) => { - expect(err).not.toBeNull(); expect(data).toBeUndefined(); done(); }); }); + test('a valid path should resolve in a string', (done) => { - reader([`${__dirname}/asset/betcha.txt`,`${__dirname}/asset/data.txt`, `${__dirname}/asset/lastly.txt`], (err, data) => { - expect(err).toBeNull(); - expect(data).toEqual('words', 'for', 'food'); + reader([`${__dirname}/asset/betcha.txt`,`${__dirname}/asset/data.txt`,`${__dirname}/asset/lastly.txt`], (err, data) => { + expect(data).toEqual(['words', 'for', 'food']); done(); }); }); diff --git a/lab-christina/.eslintrc b/lab-christina/index.js similarity index 100% rename from lab-christina/.eslintrc rename to lab-christina/index.js diff --git a/lab-christina/lib/reader.js b/lab-christina/lib/reader.js index 86a8529..4fcf6b7 100644 --- a/lab-christina/lib/reader.js +++ b/lab-christina/lib/reader.js @@ -1,13 +1,14 @@ 'use strict'; const fs = require('fs'); + module.exports = (path, callback) => { for(var i = 0; i < path.length; i++){ - fs.readFile(path, (err, data) => { + fs.readFile(path[i], 'utf-8', (err, data) => { if (err) { callback(err); } - console.log(data); + callback(null, data); }); } }; diff --git a/lab-christina/package.json b/lab-christina/package.json index f418286..6beaf1f 100644 --- a/lab-christina/package.json +++ b/lab-christina/package.json @@ -4,7 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "test": "jest -i" + "test": "jest -i", + "test-watch":"test --watch -i" }, "keywords": [], "author": "", From 2dd1a1236a82416311816270107515ed8ed9b9f7 Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Mon, 2 Oct 2017 18:38:44 -0700 Subject: [PATCH 4/5] reading through each file only once now, still unordered/unpredictable, not returning an array --- lab-christina/__test__/reader.test.js | 8 ++++---- lab-christina/index.js | 2 ++ lab-christina/lib/reader.js | 21 ++++++++++++++++----- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lab-christina/__test__/reader.test.js b/lab-christina/__test__/reader.test.js index e868ff8..87e9c43 100644 --- a/lab-christina/__test__/reader.test.js +++ b/lab-christina/__test__/reader.test.js @@ -4,14 +4,14 @@ const reader = require('../lib/reader.js'); describe('reader', () => { test('an invalid path will result in an error message', (done) => { - reader(`${__dirname}/mkdir`, (err, data) => { + reader(`${__dirname}/fake`, (err, data) => { expect(data).toBeUndefined(); done(); }); }); - - test('a valid path should resolve in a string', (done) => { - reader([`${__dirname}/asset/betcha.txt`,`${__dirname}/asset/data.txt`,`${__dirname}/asset/lastly.txt`], (err, data) => { + test('a valid path should resolve in an array of strings', (done) => { + reader([`${__dirname}/asset/betcha.txt`,`${__dirname}/asset/data.txt`, `${__dirname}/asset/lastly.txt`], (err, data) => { + expect(err).toBeNull(); expect(data).toEqual(['words', 'for', 'food']); done(); }); diff --git a/lab-christina/index.js b/lab-christina/index.js index e69de29..1a46695 100644 --- a/lab-christina/index.js +++ b/lab-christina/index.js @@ -0,0 +1,2 @@ + +const reader = require('./lib/reader'); diff --git a/lab-christina/lib/reader.js b/lab-christina/lib/reader.js index 4fcf6b7..a4f6604 100644 --- a/lab-christina/lib/reader.js +++ b/lab-christina/lib/reader.js @@ -2,13 +2,24 @@ const fs = require('fs'); -module.exports = (path, callback) => { - for(var i = 0; i < path.length; i++){ - fs.readFile(path[i], 'utf-8', (err, data) => { + +module.exports = (filepath, callback) => { + let pathArray = []; + for(let i = 0; i < filepath.length; i++){ + fs.readFile(filepath[i], 'utf-8', (err, data) => { if (err) { - callback(err); + return callback(err); } - callback(null, data); + if(pathArray <= 3) + pathArray[i] = (data); + pathArray[i] + 1; + console.log(data); + if(pathArray === 3) + callback(null, data); }); } }; + +/* module.exports = (filepath, callback) => { + +*/ From de4916bc114f4ef5639e145812dc42307b0d737e Mon Sep 17 00:00:00 2001 From: Christina Thomas Date: Wed, 4 Oct 2017 16:31:27 -0700 Subject: [PATCH 5/5] test passing --- lab-christina/__test__/reader.test.js | 2 +- lab-christina/lib/reader.js | 29 +++++++++++++++------------ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/lab-christina/__test__/reader.test.js b/lab-christina/__test__/reader.test.js index 87e9c43..093e48e 100644 --- a/lab-christina/__test__/reader.test.js +++ b/lab-christina/__test__/reader.test.js @@ -12,7 +12,7 @@ describe('reader', () => { test('a valid path should resolve in an array of strings', (done) => { reader([`${__dirname}/asset/betcha.txt`,`${__dirname}/asset/data.txt`, `${__dirname}/asset/lastly.txt`], (err, data) => { expect(err).toBeNull(); - expect(data).toEqual(['words', 'for', 'food']); + expect(data).toEqual(['words\n', 'for\n', 'food\n']); done(); }); }); diff --git a/lab-christina/lib/reader.js b/lab-christina/lib/reader.js index a4f6604..eaf1ba0 100644 --- a/lab-christina/lib/reader.js +++ b/lab-christina/lib/reader.js @@ -5,21 +5,24 @@ const fs = require('fs'); module.exports = (filepath, callback) => { let pathArray = []; - for(let i = 0; i < filepath.length; i++){ - fs.readFile(filepath[i], 'utf-8', (err, data) => { + fs.readFile(filepath[0], 'utf-8', (err, data) => { + if (err) { + return callback(err); + } + pathArray[2] = (data); + fs.readFile(filepath[1], 'utf-8', (err, data) => { if (err) { return callback(err); } - if(pathArray <= 3) - pathArray[i] = (data); - pathArray[i] + 1; - console.log(data); - if(pathArray === 3) - callback(null, data); + pathArray[1] = (data); + fs.readFile(filepath[2], 'utf-8', (err, data) => { + if (err) { + return callback(err); + } + pathArray[0] = (data); + console.log(pathArray); + callback(null, pathArray); + }); }); - } + }); }; - -/* module.exports = (filepath, callback) => { - -*/