From 99da1b4db181a7c3b6fdca6d9be4f0ade9a9fc30 Mon Sep 17 00:00:00 2001 From: KatherineHanson Date: Wed, 13 Sep 2017 14:14:52 -0700 Subject: [PATCH 1/8] created starting files and directories --- .eslintignore | 5 ++ .eslintrc.json | 26 +++++++++ .gitignore | 145 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 25 +++++++++ 4 files changed, 201 insertions(+) create mode 100644 .eslintignore create mode 100644 .eslintrc.json create mode 100644 .gitignore create mode 100644 package.json diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..05b1cf3 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..840d336 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,26 @@ +{ + "env": { + "browser": true, + "node": true, + "commonjs": true, + "jest": 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/.gitignore b/.gitignore new file mode 100644 index 0000000..9497282 --- /dev/null +++ b/.gitignore @@ -0,0 +1,145 @@ +# 401 JS +db +.env +temp +build + +# Created by https://www.gitignore.io/api/vim,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 + +### 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 + +### 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/vim,osx,node,linux,windows diff --git a/package.json b/package.json new file mode 100644 index 0000000..a47edeb --- /dev/null +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name": "03-asynchronous-callbacks", + "version": "1.0.0", + "description": "![CF](https://camo.githubusercontent.com/70edab54bba80edb7493cad3135e9606781cbb6b/687474703a2f2f692e696d6775722e636f6d2f377635415363382e706e67) 03: Parallel File Processing ===", + "main": "index.js", + "scripts": { + "lint": "eslint .", + "test": "jest --coverage -i", + "test-watch": "jest --watch -i" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/KatherineHanson/03-asynchronous-callbacks.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/KatherineHanson/03-asynchronous-callbacks/issues" + }, + "homepage": "https://github.com/KatherineHanson/03-asynchronous-callbacks#readme", + "devDependencies": { + "jest": "^21.0.2" + } +} From a5a390cf0392a7e11b7181d742f0a194d87c182c Mon Sep 17 00:00:00 2001 From: KatherineHanson Date: Wed, 13 Sep 2017 16:33:51 -0700 Subject: [PATCH 2/8] added Reader module and testing functionality --- __test__/assets/cats.txt | 1 + __test__/assets/dogs.txt | 1 + __test__/assets/horses.txt | 1 + __test__/reader.test.js | 22 ++++++++++++++++++++++ lib/reader.js | 13 +++++++++++++ 5 files changed, 38 insertions(+) create mode 100644 __test__/assets/cats.txt create mode 100644 __test__/assets/dogs.txt create mode 100644 __test__/assets/horses.txt create mode 100644 __test__/reader.test.js create mode 100644 lib/reader.js diff --git a/__test__/assets/cats.txt b/__test__/assets/cats.txt new file mode 100644 index 0000000..f0dddb0 --- /dev/null +++ b/__test__/assets/cats.txt @@ -0,0 +1 @@ +ANIMAL diff --git a/__test__/assets/dogs.txt b/__test__/assets/dogs.txt new file mode 100644 index 0000000..f0dddb0 --- /dev/null +++ b/__test__/assets/dogs.txt @@ -0,0 +1 @@ +ANIMAL diff --git a/__test__/assets/horses.txt b/__test__/assets/horses.txt new file mode 100644 index 0000000..f0dddb0 --- /dev/null +++ b/__test__/assets/horses.txt @@ -0,0 +1 @@ +ANIMAL diff --git a/__test__/reader.test.js b/__test__/reader.test.js new file mode 100644 index 0000000..971ae77 --- /dev/null +++ b/__test__/reader.test.js @@ -0,0 +1,22 @@ +'use strict'; + +const reader = require('../lib/reader.js'); + +describe('reader', () => { + test('an invalid path should reject an error', (done) => { + reader([`${__dirname}/assets/pets.txt`,`${__dirname}/assets/pets.txt`,`${__dirname}/assets/pets.txt`], (err, data) => { + expect(err).not.toBe(null); + expect(data).toBeUndefined(); + done(); + }); + }); + + test('a valid path should resolve a lowercased string', (done) => { + reader([`${__dirname}/assets/horses.txt`,`${__dirname}/assets/cats.txt`,`${__dirname}/assets/dogs.txt`], (err, data) => { + + expect(err).toBeNull(); + expect(data).toEqual('animal\n'); + done(); + }); + }); +}); diff --git a/lib/reader.js b/lib/reader.js new file mode 100644 index 0000000..936132d --- /dev/null +++ b/lib/reader.js @@ -0,0 +1,13 @@ +'use strict'; + +const fs = require('fs'); + +module.exports = (paths, callback) => { + for (var i = 0; i < paths.length; i++) { + fs.readFile(paths[i], (err, data) => { + if(err) + return callback(err); + callback(null, data.toString().toLowerCase()); + }); + } +}; From 0332bf3dd476396b9a33929a385bfaca8813460e Mon Sep 17 00:00:00 2001 From: KatherineHanson Date: Wed, 13 Sep 2017 16:54:44 -0700 Subject: [PATCH 3/8] refactored code --- __test__/reader.test.js | 4 ++-- lib/reader.js | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/__test__/reader.test.js b/__test__/reader.test.js index 971ae77..5b0dcdd 100644 --- a/__test__/reader.test.js +++ b/__test__/reader.test.js @@ -4,7 +4,7 @@ const reader = require('../lib/reader.js'); describe('reader', () => { test('an invalid path should reject an error', (done) => { - reader([`${__dirname}/assets/pets.txt`,`${__dirname}/assets/pets.txt`,`${__dirname}/assets/pets.txt`], (err, data) => { + reader.read([`${__dirname}/assets/pets.txt`,`${__dirname}/assets/pets.txt`,`${__dirname}/assets/pets.txt`], (err, data) => { expect(err).not.toBe(null); expect(data).toBeUndefined(); done(); @@ -12,7 +12,7 @@ describe('reader', () => { }); test('a valid path should resolve a lowercased string', (done) => { - reader([`${__dirname}/assets/horses.txt`,`${__dirname}/assets/cats.txt`,`${__dirname}/assets/dogs.txt`], (err, data) => { + reader.read([`${__dirname}/assets/horses.txt`,`${__dirname}/assets/cats.txt`,`${__dirname}/assets/dogs.txt`], (err, data) => { expect(err).toBeNull(); expect(data).toEqual('animal\n'); diff --git a/lib/reader.js b/lib/reader.js index 936132d..edc1898 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -2,7 +2,9 @@ const fs = require('fs'); -module.exports = (paths, callback) => { +const reader = module.exports = {}; + +reader.read = (paths, callback) => { for (var i = 0; i < paths.length; i++) { fs.readFile(paths[i], (err, data) => { if(err) From 20d45b66481a868f38ee2c9c735c05f0c7d057d4 Mon Sep 17 00:00:00 2001 From: KatherineHanson Date: Wed, 13 Sep 2017 23:50:39 -0700 Subject: [PATCH 4/8] refactored module --- README.md | 40 ++-------------------------------------- lib/reader.js | 11 ++++++++--- 2 files changed, 10 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index e9f352d..dffb929 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,10 @@ ![CF](https://camo.githubusercontent.com/70edab54bba80edb7493cad3135e9606781cbb6b/687474703a2f2f692e696d6775722e636f6d2f377635415363382e706e67) 03: Parallel File Processing === -## Submission Instructions -* Work in a fork of this repository -* Work in a branch on your fork -* Write all of your code in a directory named `lab-` + `` **e.g.** `lab-susan` -* Open a pull request to this repository -* Submit on canvas a question and observation, how long you spent, and a link to your pull request - -## Resources - * [fs module docs](https://nodejs.org/api/fs.html) - -## Configuration -Configure the root of your repository with the following files and directories. Thoughfully name and organize any aditional configuration or module files. -* **README.md** - contains documentation -* **.gitignore** - contains a [robust](http://gitignore.io) `.gitignore` file -* **.eslintrc** - contains the course linter configuratoin -* **.eslintignore** - contains the course linter ignore configuration -* **package.json** - contains npm package config - * create a `lint` script for running eslint - * create a `test` script for running tests -* **lib/** - contains module definitions -* **assets/** - contains the text files used by the program -* **\_\_test\_\_/** - contains unit tests - -## Feature Tasks +## Exported Values #### Reader Module -In the lib/ directory create a reader.js module that exports a single function. The reader module should take an array of three file paths and resolve a mapped array of strings loaded from each file using an error-first callback. The string data should be in the same order as the file path data (mapped). If an error occurs it should immediatly reject the error using the callback and stop execution. +The reader module exports a single function. It takes an array of three file paths and resolves a mapped array of strings loaded from each file using an error-first callback. The string data should be in the same order as the file path data (mapped). If an error occurs it should immediately reject the error using the callback and stop execution. * The reader module should have the function signature `(paths, callback) => undefined` * On a failure the reader module should invoke the callback with an error `callback(error)` * On success the reader module should invoke the callback with null as the first paramiter, and the result as the second paramiter `callback(null, result)` - -## Testing -#### Reader Module Tests -* Use BDD `describe` and `test` methods to define discriptive tests and increase readablity -* Each `test` callback should aim to test a small well defined feature of a function -* Write tests to ensure the reader function rejects errors with invalid file paths -* Write tests to ensure the reader function correctly resolves mapped string data for an array of file paths - -## Bonus 1pt -Write the reader function recursivly so that it will be able to support 0 or more paths. - -## Documentation -In your README.md describe the exported values of each module you have defined. Every function description should include it's airty (expected number of paramiters), the expected data for each paramiter (data-type and limitations), and it's behavior (for both valid and invalued use). Feel free to write any additional information in your README.md. diff --git a/lib/reader.js b/lib/reader.js index edc1898..31017c3 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -2,14 +2,19 @@ const fs = require('fs'); -const reader = module.exports = {}; - -reader.read = (paths, callback) => { +const read = (paths, callback) => { + var strings = []; for (var i = 0; i < paths.length; i++) { fs.readFile(paths[i], (err, data) => { if(err) return callback(err); callback(null, data.toString().toLowerCase()); + strings.push(data.toString().toLowerCase()); + strings; }); } }; + +module.exports = { + read: read, +}; From d2e8834302d30d26aadb89b06dea809ecb0f5a54 Mon Sep 17 00:00:00 2001 From: KatherineHanson Date: Wed, 13 Sep 2017 23:54:41 -0700 Subject: [PATCH 5/8] finished README.md --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index dffb929..a0f0889 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ #### Reader Module The reader module exports a single function. It takes an array of three file paths and resolves a mapped array of strings loaded from each file using an error-first callback. The string data should be in the same order as the file path data (mapped). If an error occurs it should immediately reject the error using the callback and stop execution. -* The reader module should have the function signature `(paths, callback) => undefined` -* On a failure the reader module should invoke the callback with an error `callback(error)` -* On success the reader module should invoke the callback with null as the first paramiter, and the result as the second paramiter `callback(null, result)` +* The reader module's read function has an arity of one. +* The expected data for each parameter is an array holding strings representing file paths.` +* On a failure, it should invoke the callback with an error `callback(error)` +* On success, it should invoke the callback with null as the first paramiter, and the result as the second paramiter `callback(null, result)` From 60bfceb76c9e4fe467795baf4d8ac048c85b3f20 Mon Sep 17 00:00:00 2001 From: KatherineHanson Date: Thu, 14 Sep 2017 08:58:30 -0700 Subject: [PATCH 6/8] refactored reader module --- __test__/assets/cats.txt | 2 +- __test__/assets/dogs.txt | 2 +- __test__/reader.test.js | 2 +- lib/reader.js | 6 ++++-- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/__test__/assets/cats.txt b/__test__/assets/cats.txt index f0dddb0..3335d9d 100644 --- a/__test__/assets/cats.txt +++ b/__test__/assets/cats.txt @@ -1 +1 @@ -ANIMAL +ANIMALER diff --git a/__test__/assets/dogs.txt b/__test__/assets/dogs.txt index f0dddb0..f4cd508 100644 --- a/__test__/assets/dogs.txt +++ b/__test__/assets/dogs.txt @@ -1 +1 @@ -ANIMAL +ANIMALEST diff --git a/__test__/reader.test.js b/__test__/reader.test.js index 5b0dcdd..83de07b 100644 --- a/__test__/reader.test.js +++ b/__test__/reader.test.js @@ -15,7 +15,7 @@ describe('reader', () => { reader.read([`${__dirname}/assets/horses.txt`,`${__dirname}/assets/cats.txt`,`${__dirname}/assets/dogs.txt`], (err, data) => { expect(err).toBeNull(); - expect(data).toEqual('animal\n'); + expect(data).toEqual([ 'animal\n', 'animaler\n', 'animalest\n' ]); done(); }); }); diff --git a/lib/reader.js b/lib/reader.js index 31017c3..b77b918 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -8,9 +8,11 @@ const read = (paths, callback) => { fs.readFile(paths[i], (err, data) => { if(err) return callback(err); - callback(null, data.toString().toLowerCase()); strings.push(data.toString().toLowerCase()); - strings; + if (strings.length === 3) { + console.log(strings.sort()); + callback(null, strings.sort()); + } }); } }; From 0a3e486d76e095482d961a29588d5ba20f0a250b Mon Sep 17 00:00:00 2001 From: KatherineHanson Date: Thu, 14 Sep 2017 09:12:22 -0700 Subject: [PATCH 7/8] updated version and author data in package.json --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a47edeb..e74b789 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "03-asynchronous-callbacks", - "version": "1.0.0", + "version": "0.0.0", "description": "![CF](https://camo.githubusercontent.com/70edab54bba80edb7493cad3135e9606781cbb6b/687474703a2f2f692e696d6775722e636f6d2f377635415363382e706e67) 03: Parallel File Processing ===", "main": "index.js", "scripts": { @@ -13,7 +13,7 @@ "url": "git+https://github.com/KatherineHanson/03-asynchronous-callbacks.git" }, "keywords": [], - "author": "", + "author": "katherinemariehanson@gmail.com", "license": "ISC", "bugs": { "url": "https://github.com/KatherineHanson/03-asynchronous-callbacks/issues" From 40da5222aca80272754e93035708620f74ead71e Mon Sep 17 00:00:00 2001 From: KatherineHanson Date: Thu, 14 Sep 2017 09:24:34 -0700 Subject: [PATCH 8/8] removed unnecessary console.log from module --- lib/reader.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/reader.js b/lib/reader.js index b77b918..4ebad79 100644 --- a/lib/reader.js +++ b/lib/reader.js @@ -10,7 +10,6 @@ const read = (paths, callback) => { return callback(err); strings.push(data.toString().toLowerCase()); if (strings.length === 3) { - console.log(strings.sort()); callback(null, strings.sort()); } });