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
45 changes: 0 additions & 45 deletions README.md

This file was deleted.

6 changes: 6 additions & 0 deletions lab/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

**/node_modules/*
**/vendor/*
**/*.min.js
**/coverage/*
**/build/*
21 changes: 21 additions & 0 deletions lab/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": [ "error", "single" ],
"semi": ["error", "always"],
"linebreak-style": [ "error", "unix" ]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
127 changes: 127 additions & 0 deletions lab/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@

# Created by https://www.gitignore.io/api/osx,vim,node,windows

### 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/osx,vim,node,windows
16 changes: 16 additions & 0 deletions lab/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
the lib directory contains the following files:

file-reader.js

file-reader.js accepts a directory path and a callback as an argument. The directory is then read using fs. An array containing all files is passed to a wrapper function which then uses recursion to iterate through each file. Using the fs module, each file is read as a buffer which is then spliced to the length of 8 bytes. Each of these is console logged and pushed to an array. The containing array will then be returned as a parameter of the callback function.

the test directory contains the following files:

file-reader-test.js

file-reader-test.js contains a test for file-reader.js. It tests if the file-reader module throws an error when an incorrect path is passed. Additionally the test check if file-reader logs the files in the specified order.

--------------------------------------------

--done() explanation
Done is used to prevent the test from completing before an asynchronous operation is done executing. done should be placed in the call back of the asynchronous operations call back. Once the operation is called, the done method will be called. done must be passed as a parameter for the callback of the it method executing the test.
1 change: 1 addition & 0 deletions lab/data/one.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a test for the first file
1 change: 1 addition & 0 deletions lab/data/three.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Bacon grease has all sorts of useful purposes...
1 change: 1 addition & 0 deletions lab/data/two.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I really enjoy eating cake
Empty file added lab/index.js
Empty file.
26 changes: 26 additions & 0 deletions lab/lib/file-reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const fs = require('fs');

const fileReader = module.exports = function(path, callback) {

function asyncWrapper(folder, callback, buffArray) {
let fileName = folder.pop();
return fs.readFile(`${path}/${fileName}`, function(err, asset) {
if(err) return callback(err);
buffArray.push(asset.toString('hex', 0, 8));
console.log(buffArray[buffArray.length -1]);
if(folder.length !== 0) return asyncWrapper(folder, callback, buffArray);
return callback(err, buffArray);
});
}

return fs.readdir(path, (err, dir) => {
if(err) return callback(err);
let buffArray = [];
//This is a hacky POS to get them to read in order....
//May the coding gods have mercy on my soul...
dir.sort((a, b) => a.length - b.length).reverse();
asyncWrapper(dir, callback, buffArray);
});
};
19 changes: 19 additions & 0 deletions lab/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "lab",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"chai": "^4.1.0",
"mocha": "^3.4.2"
}
}
27 changes: 27 additions & 0 deletions lab/test/file-reader-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const fileReader = require('../lib/file-reader.js');
const expect = require('chai').expect;

//Conatains the first 8 bytes from files one, two, and three in order
let fileBytes = ['5468697320697320', '49207265616c6c79', '4261636f6e206772'];

describe('File Reader Module', () => {
describe('With improper file path', () => {
it('Should throw an error', (done) => {
fileReader(`${__dirname}/../not-a-file.txt`, (err) => {
expect(err).to.be.an('error');
});
done();
});
});

describe('With proper file path', () => {
it('Should console log files in order', (done) => {
fileReader(`${__dirname}/../data`, (err, asset) => {
asset.forEach((buff, ind) => expect(buff).to.equal(fileBytes[ind]));
done();
});
});
});
});