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
5 changes: 5 additions & 0 deletions lab-rob/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
41 changes: 41 additions & 0 deletions lab-rob/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"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"
]
}
}
152 changes: 152 additions & 0 deletions lab-rob/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# 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/
coverage/

# 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

node_modules/*
coverage/*
24 changes: 24 additions & 0 deletions lab-rob/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Code Fellows: Code 401d19: Full-Stack JavaScript
## Lab 03: Asynchronous Callbacks / Parallel File Processing
#### 11/29/17 - Robert Reed

### reader.js
reader.js exports a single function, `readFiles(paths, callback)`, that reads any number of files and pushes their contents, in order, to an array.

`paths` must be an array (there is no type check) containing the absolute paths to the files you would like to read.

If the file at a specified path does not exist, an error object is returned.

`callback` must be a function (again, no type checking) and should be in the following form:

```
(error, data) => {
if(error)
// handle error
// do something with data
}
```

`callback` should both handle the error object, as well as determine what to do with the final array.

`readFiles()` is recursive, so be wary of memory usage.
83 changes: 83 additions & 0 deletions lab-rob/__test__/reader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict';

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

const rainText = `Rainy rain is the rainiest rain that rains.
If you live in Seattle, get used to the rain!`;

const snowText = `Clean snow is white, soft, and cold.
Dirty snow can be brown, black, yellow, hard, and possibly dry.
It doesn't really snow in Seattle that much.`;

const sunText = `The sun is a rarity in Seattle. When it comes, it's great!
Unfortunately, it rarely shines in the winter time.`;

const rainPath = reader.getPath('rain.txt');

const snowPath = reader.getPath('snow.txt');

const sunPath = reader.getPath('sun.txt');

describe('reader.js', () => {
describe('reader() should accept an array of text file names, and return an array with the text from those files, in the same order. If there is a filename error, an exception is thrown.', () => {
test('When three files names are given, an array with the text of each file should be returned.', done => {
reader.readFiles([rainPath, snowPath, sunPath], (err, data) => {
expect(err).toBeNull();
expect(data).toEqual([rainText, snowText, sunText]);
done();
});
});

test('When one filename is given, an array with the text of each file should be returned.', done => {
reader.readFiles([rainPath], (err, data) => {
expect(err).toBeNull();
expect(data).toEqual([rainText]);
done();
});
});

test('When no filename is given, an empty array should be returned.', done => {
reader.readFiles([], (err, data) => {
expect(err).toBeNull();
expect(data).toEqual([]);
done();
});
});

test('When 12 files names are given, an array with the text of each file should be returned.', done => {
reader.readFiles([rainPath, snowPath, sunPath, rainPath, snowPath, sunPath, rainPath, snowPath, sunPath, rainPath, snowPath, sunPath], (err, data) => {
expect(err).toBeNull();
expect(data).toEqual([rainText, snowText, sunText, rainText, snowText, sunText, rainText, snowText, sunText, rainText, snowText, sunText]);
done();
});
});

test('When a bad first filename is given, an error should be thrown.', done => {
reader.readFiles(['bad path', snowPath, sunPath], (err, data) => {
expect(err).not.toBeNull();
expect(data).toBeUndefined();
done();
});
});

test('When a bad second filename is given, an error should be thrown.', done => {
reader.readFiles([rainPath, 'snowPath', sunPath], (err, data) => {
expect(err).not.toBeNull();
expect(data).toBeUndefined();
done();
});
});

test('When a bad third filename is given, an error should be thrown.', done => {
reader.readFiles([rainPath, snowPath, 'sunPath'], (err, data) => {
expect(err).not.toBeNull();
expect(data).toBeUndefined();
done();
});
});

test('When a non-string is passed to the getPath method an error is thrown', () => {
expect(reader.getPath).toThrow();
});
});
});
2 changes: 2 additions & 0 deletions lab-rob/assets/rain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Rainy rain is the rainiest rain that rains.
If you live in Seattle, get used to the rain!
3 changes: 3 additions & 0 deletions lab-rob/assets/snow.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Clean snow is white, soft, and cold.
Dirty snow can be brown, black, yellow, hard, and possibly dry.
It doesn't really snow in Seattle that much.
2 changes: 2 additions & 0 deletions lab-rob/assets/sun.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The sun is a rarity in Seattle. When it comes, it's great!
Unfortunately, it rarely shines in the winter time.
39 changes: 39 additions & 0 deletions lab-rob/lib/reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const fs = require('fs');

const reader = module.exports = {};

reader.stringArray = [];

// Recursively read files
reader.readFiles = (paths, callback) => {
// base case - no more paths to read
// - execute callback w/o error
if(paths.length === 0) {
callback(null, reader.stringArray);
reader.stringArray = [];
} else {
// still files to read
let filePath = paths.shift();
fs.readFile(filePath, (err, data) => {
// stop if there is an error
if (err)
return callback(err);

// Add the read data to the stringArray
reader.stringArray.push(data.toString());

// Recursively read the remaining files
reader.readFiles(paths, callback);
});
}
};

// get pathname to file via filename
reader.getPath = fileName => {
if (typeof fileName !== 'string')
throw new TypeError(`Expected fileName to be a string, but got a ${typeof fileName}`);

return `${__dirname}/../assets/${fileName}`;
};
Loading