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

# Created by https://www.gitignore.io/api/node,macos

### macOS ###
*.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

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


# End of https://www.gitignore.io/api/node,macos
1 change: 1 addition & 0 deletions lab-andrew/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In my reader.js file I am exporting one module, to which two functions are attached. They both do the same thing, one is non-recursive and one which is recursive. The non-recursive function has two parameters, an array (which is expected to have a length of three) and a callback. The recursive function takes the same parameters, however, there is no limit to the length of the array allowed. For both functions, the array is expected to contain strings containing file paths, and will return an array of the text contained in each file. The tests are written to check that both functions will return the expected output for a given valid input, as well as checking that incorrect file paths will return errors. Also the tests are checking that the input is an array, and not an incorrect type (which will throw an error).
80 changes: 80 additions & 0 deletions lab-andrew/__test__/reader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';

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

describe('reader.test.js', () => {

describe('testing functionality for reader.readTxts', () => {
test('function should return cat.txt as a string', done => {
reader.readTxts([`${__dirname}/../assets/cat.txt`, `${__dirname}/../assets/dog.txt`, `${__dirname}/../assets/fish.txt`], (error, data) => {
expect(error).toBeNull();
expect(data).toEqual(['cat\n', 'dog\n', 'fish\n']);
done();
});
});
});

describe('testing invalid paths for reader.readTxts', () => {
test('function should throw an error if path to cat.txt is invalid', done => {
reader.readTxts([`${__dirname}/../assets/cat.tttttt`, `${__dirname}/../assets/dog.txt`, `${__dirname}/../assets/fish.txt`], (error) => {
expect(error).not.toBeNull();
done();
});
});
test('function should throw an error if path to dog.txt is invalid', done => {
reader.readTxts([`${__dirname}/../assets/cat.txt`, `${__dirname}/../assets/dog.sssst`, `${__dirname}/../assets/fish.txt`], (error) => {
expect(error).not.toBeNull();
done();
});
});
test('function should throw an error if path to fish.txt is invalid', done => {
reader.readTxts([`${__dirname}/../assets/cat.txt`, `${__dirname}/../assets/dog.txt`, `${__dirname}/../assets/fish.es`], (error) => {
expect(error).not.toBeNull();
done();
});
});
});

describe('testing invalid input for array in reader.readTxts', () => {
test('function should throw error if not given an array', () => {
expect(() => reader.readTxts('bla')).toThrow();
});
});

describe('testing functionality for reader.recursiveRead', () => {
test('function should return cat.txt as a string', done => {
reader.recursiveRead([`${__dirname}/../assets/cat.txt`, `${__dirname}/../assets/dog.txt`, `${__dirname}/../assets/fish.txt`], (error, data) => {
expect(error).toBeNull();
expect(data).toEqual(['cat\n', 'dog\n', 'fish\n']);
done();
});
});
});

describe('testing invalid paths for reader.recursiveRead', () => {
test('function should throw an error if path to cat.txt is invalid', done => {
reader.recursiveRead([`${__dirname}/../assets/cat.tttttt`, `${__dirname}/../assets/dog.txt`, `${__dirname}/../assets/fish.txt`], (error) => {
expect(error).not.toBeNull();
done();
});
});
test('function should throw an error if path to dog.txt is invalid', done => {
reader.recursiveRead([`${__dirname}/../assets/cat.txt`, `${__dirname}/../assets/dog.sssst`, `${__dirname}/../assets/fish.txt`], (error) => {
expect(error).not.toBeNull();
done();
});
});
test('function should throw an error if path to fish.txt is invalid', done => {
reader.recursiveRead([`${__dirname}/../assets/cat.txt`, `${__dirname}/../assets/dog.txt`, `${__dirname}/../assets/fish.es`], (error) => {
expect(error).not.toBeNull();
done();
});
});
});

describe('testing invalid input for array in reader.recursiveRead', () => {
test('function should throw error if not given an array', () => {
expect(() => reader.recursiveRead('bla')).toThrow();
});
});
});
1 change: 1 addition & 0 deletions lab-andrew/assets/cat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cat
1 change: 1 addition & 0 deletions lab-andrew/assets/dog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dog
1 change: 1 addition & 0 deletions lab-andrew/assets/fish.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fish
40 changes: 40 additions & 0 deletions lab-andrew/lib/reader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const reader = module.exports = {};

const fs = require('fs');

// Non-recursive method
reader.readTxts = (arr, callback) => {
if (!Array.isArray(arr)) throw new TypeError('arr must be an Array');
const fileArr = [];
fs.readFile(arr[0], (error, data) => {
if (error) return callback(error);
fileArr.push(data.toString());
fs.readFile(arr[1], (error, data) => {
if (error) return callback(error);
fileArr.push(data.toString());
fs.readFile(arr[2], (error, data) => {
if (error) return callback(error);
fileArr.push(data.toString());
callback(null, fileArr);
});
});
});
};

// Recursive method
reader.fileArr = [];
reader.recursiveRead = (arr, callback) => {
if (!Array.isArray(arr)) throw new TypeError('arr must be an Array');
if (arr.length > 0) {
let currentPath = arr.splice(0, 1)[0];
fs.readFile(currentPath, (error, data) => {
if (error) return callback(error);
reader.fileArr.push(data.toString());
reader.recursiveRead(arr, callback);
});
} else {
callback(null, reader.fileArr);
}
};
Loading