diff --git a/lab-ron/.eslintignore b/lab-ron/.eslintignore new file mode 100644 index 0000000..05b1cf3 --- /dev/null +++ b/lab-ron/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/lab-ron/.eslintrc.json b/lab-ron/.eslintrc.json new file mode 100644 index 0000000..840d336 --- /dev/null +++ b/lab-ron/.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/lab-ron/.gitignore b/lab-ron/.gitignore new file mode 100644 index 0000000..3583e8a --- /dev/null +++ b/lab-ron/.gitignore @@ -0,0 +1,68 @@ +# Created by https://www.gitignore.io/api/osx,linux,node,vim + +### OSX ### +.DS_Store +.AppleDouble +.LSOverride + +### 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-* + + +### Node ### +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# 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 REPL history +.node_repl_history + +### Vim ### +# swap +[._]*.s[a-w][a-z] +[._]s[a-w][a-z] +# session +Session.vim +# temporary +.netrwhist +*~ + +# auto-generated tag files +tags + +###### Personal +.tern-project diff --git a/lab-ron/README.md b/lab-ron/README.md new file mode 100644 index 0000000..c8a28e1 --- /dev/null +++ b/lab-ron/README.md @@ -0,0 +1,21 @@ +# Parallel File Processing + +Class 3 of 401 + +### reader() +`reader()` is used to read the content of multiple files. +reader takes in two parameters, an array of paths, and a callback function e.g. `reader(path,callback)` + +``` +reader(['foo.txt', 'bar.txt, 'wic.txt'], (err, data) => { + if (err) return console.error(err); + console.log(data); +}); + +>> foo +>> +>> bar +>> +>> wic +>> +``` \ No newline at end of file diff --git a/lab-ron/__test__/reader.test.js b/lab-ron/__test__/reader.test.js new file mode 100644 index 0000000..d212abe --- /dev/null +++ b/lab-ron/__test__/reader.test.js @@ -0,0 +1,34 @@ +'use strict'; + +const reader = require('../lib/reader.js'); + +describe('#reader', () => { + test(`invalid path should reject an error`, (done) => { + reader([`${__dirname}/../assets/foo2.txt`], (err, data) => { + expect(err).not.toBeNull(); + expect(data).toBeUndefined(); + done(); + }); + }); + + test(`valid path resolves a maped array of the file data`, (done) => { + reader([`${__dirname}/../assets/foo.txt`], (err, data) => { + expect(err).toBeNull(); + expect(data).toEqual(['foo\n']); + console.log(data); + done(); + }); + }); + + test(`valid path resolves in the path's name two value`, (done) => { + reader([`${__dirname}/../assets/foo.txt`, `${__dirname}/../assets/bar.txt`], + (err, data) => { + console.log(data); + expect(err).toBeNull(); + expect(data).toEqual(['foo\n', 'bar\n']); + done(); + }); + }); + +}); + diff --git a/lab-ron/assets/bar.txt b/lab-ron/assets/bar.txt new file mode 100644 index 0000000..5716ca5 --- /dev/null +++ b/lab-ron/assets/bar.txt @@ -0,0 +1 @@ +bar diff --git a/lab-ron/assets/bye.txt b/lab-ron/assets/bye.txt new file mode 100644 index 0000000..b023018 --- /dev/null +++ b/lab-ron/assets/bye.txt @@ -0,0 +1 @@ +bye diff --git a/lab-ron/assets/foo.txt b/lab-ron/assets/foo.txt new file mode 100644 index 0000000..257cc56 --- /dev/null +++ b/lab-ron/assets/foo.txt @@ -0,0 +1 @@ +foo diff --git a/lab-ron/assets/hi.txt b/lab-ron/assets/hi.txt new file mode 100644 index 0000000..45b983b --- /dev/null +++ b/lab-ron/assets/hi.txt @@ -0,0 +1 @@ +hi diff --git a/lab-ron/index.js b/lab-ron/index.js new file mode 100644 index 0000000..1fb8681 --- /dev/null +++ b/lab-ron/index.js @@ -0,0 +1,9 @@ +'use strict'; + +const reader = require('./lib/reader'); + +reader(process.argv.slice(2), (err, data) => { + if (err) + return console.log('USAGE ERROR: something went wrong'); + console.log(data.join('\n')); +}); \ No newline at end of file diff --git a/lab-ron/lib/reader.js b/lab-ron/lib/reader.js new file mode 100644 index 0000000..00fd559 --- /dev/null +++ b/lab-ron/lib/reader.js @@ -0,0 +1,23 @@ +'use strict'; + +const fs = require('fs'); + +module.exports = (paths, callback) => { + console.log(paths); + let result = []; + + let loopFiles = (num) => { + if (num === paths.length) + return callback(null, result); + fs.readFile(paths[num], (err, data) => { + if (err) + return callback(err); + result.push(data.toString()); + loopFiles(num + 1); + }); + }; + + loopFiles(0); +}; + + diff --git a/lab-ron/package.json b/lab-ron/package.json new file mode 100644 index 0000000..5409ebf --- /dev/null +++ b/lab-ron/package.json @@ -0,0 +1,19 @@ +{ + "name": "lab-ron", + "version": "0.0.1", + "description": "Lab 3 of 401", + "main": "index.js", + "dependencies": {}, + "devDependencies": { + "eslint": "^4.6.1", + "jest": "^21.0.2" + }, + "scripts": { + "lint": "eslint .", + "test": "jest --coverage -i", + "test-watch": "jest --watch -i" + }, + "keywords": [], + "author": "Ron Barrantes", + "license": "MIT" +}