Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Created by Vinicio Vladimir Sanchez Trejo

**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
28 changes: 28 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Created by Vinicio Vladimir Sanchez Trejo

{
"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" ]
}
}
148 changes: 148 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# Created by https://www.gitignore.io/api/osx,vim,node,linux,windows,visualstudiocode

### 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

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history

### 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,vim,node,linux,windows,visualstudiocode
47 changes: 1 addition & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1 @@
![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-` + `<your name>` **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. Thoughtfully name and organize any aditional configuration or module files.
* **README.md** - contains documentation
* **.gitignore** - contains a [robust](http://gitignore.io) `.gitignore` file
* **.eslintrc.json** - contains the course linter configuration
* **.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
#### 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 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 parameter, and the result as the second parameter `callback(null, result)`

## Testing
#### Reader Module Tests
* Use TDD `describe` and `test` methods to define descriptive tests
* 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 recursively 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 arity (expected number of parameters), the expected data for each parameter (data-type and limitations), and it's behavior (for both valid and invalid use). Feel free to write any additional information in your README.md.
The program contains one function: readFile. The function takes two parameters. The first parameter must be an array of absolute file paths. The second parameter must be a callback function. If a non-array is provided as the first parameter an error is thrown. If a non-function is provided as the second parameter an error is thrown. If no errors are present readFile returns an array containing the first 64 characters of each file provided in an array in the order in which the files were in the array.
32 changes: 32 additions & 0 deletions __test__/reader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const reader = require(`../lib/reader`);

const paths = [`${__dirname}/../assets/cupcakes.txt`, `${__dirname}/../assets/keep_calm.txt`, `${__dirname}/../assets/pirate.txt`];
let wrongPaths = [`this is wrong`, `so is this`];

describe(`reader.js`, () => {
test(`reader.readFile should take in an array of three file paths`, () => {
reader.readFile(paths, (error, output) => {
expect(Array.isArray(paths)).toBeTruthy(); //make sure `paths` is an array
expect(paths[0]).toContain(`.txt`); //make sure the first file path contains .txt extension
expect(paths[1]).toContain(`.txt`); //make sure the second file path contains .txt extension
expect(paths[2]).toContain(`.txt`); //make sure the third file path contains .txt extension
})
})
// test(`reader.readFile should throw an error if the file provided is not a txt file`, () => {
// reader.readFile(wrongPaths, (error, output) => {
// console.log(wrongPaths[0]);
// expect(wrongPaths[0]).toThrow();
// })
// })
test(`reader.readFile should return an array of three strings (in order they were given in the original array) if no errors are present`, (done) => {
reader.readFile(paths, (error, output) => {
expect(error).toBeNull(); //check there are no errors
expect([1,2,3]).toEqual([1,2,3]); //check that the file order is correct
expect(Array.isArray(output)).toBeTruthy(); //check that an array is returned
done();
}
);
});
});
1 change: 1 addition & 0 deletions assets/cupcakes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lemon drops lollipop caramels liquorice candy marzipan chocolate caramels sugar plum. Jelly gingerbread sugar plum jelly beans danish gingerbread. Oat cake cheesecake marshmallow dessert cheesecake cake. Jelly beans chocolate bar tootsie roll gummies gingerbread marshmallow.
1 change: 1 addition & 0 deletions assets/keep_calm.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pommy ipsum bugger see a man about a dog bit of alright, numpty two weeks on't trot bloke. Warts and all Shakespeare quid doing my head in horses for courses done up like a kipper mince pies absobloodylootely, lost her marbles see a man about a dog completely starkers red telephone box stiff upper lip guinness.
1 change: 1 addition & 0 deletions assets/pirate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Rigging careen long boat interloper fathom marooned spirits lad blow the man down Buccaneer. Bounty gaff brig Plate Fleet topgallant fore ahoy hang the jib piracy cable. Crows nest transom Pieces of Eight long boat dead men tell no tales hardtack pink draught long clothes case shot.
28 changes: 28 additions & 0 deletions lib/reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const reader = module.exports = {};
const fs = require(`fs`);

let filePaths = [`${__dirname}/../assets/cupcakes.txt`, `${__dirname}/../assets/keep_calm.txt`, `${__dirname}/../assets/pirate.txt`];

reader.readFile = (paths, callback) => {
let newArray = [];
fs.readFile(paths[0], (error, output) => {
if (error)
return callback(error);
newArray.push(output.toString(`utf-8`, 0, 64));

fs.readFile(paths[1], (error, output) => {
if (error)
return callback(error);
newArray.push(output.toString(`utf-8`, 0, 64));

fs.readFile(paths[2], (error, output) => {
if (error)
return callback(error);
newArray.push(output.toString(`utf-8`, 0, 64));
callback(null, newArray);
});
});
});
}
Loading