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-jeff/.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-jeff/.eslintrc
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" ]
}
}
148 changes: 148 additions & 0 deletions lab-jeff/.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
22 changes: 22 additions & 0 deletions lab-jeff/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Code 401 lab 2

## Purpose
The purpose of this lab is to learn Test Driven Development (TDD) when writing code. We are using jest.js to test our code.

## File Structure
There are two test files in the __test__ folder which test the functions included in the module in the lib folder.

## greet module
The fp module contains four functions: map, filter, reduce, and slice.

### fp.map
fp.map expects 2 arguments: a function and an array of data. If a function and an array are passed in, it iterates over the array and applies the function to each one, returning an array with the results of those function calls. If the first argument is not a function or the second argument is not an array (or string), it throws a TypeError.

### fp.filter
This function takes two arguments: a function and an array of data. With valid data, it returns a new array with all elements that pass the test implemented by the provided function. If the first argument is not a function or the second argument is not an array (or string), it throws a TypeError.

### fp.reduce
This function takes three arguments: a function, an array of data, and an initial value. With valid data, it applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value, which it returns. If the first argument is not a function or the second argument is not an array (or string), it throws a TypeError.

### fp.slice
This function takes three arguments: a beginning index, an ending index, and an array. With valid data, it returns a portion of the array into a new array selected from begin to end (end not included). If the first or second arguments are not integers, or the third argument is not an array (or string), it throws a TypeError.
98 changes: 98 additions & 0 deletions lab-jeff/__test__/fp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
'use strict';

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

describe('fp.js', () => {
describe('fp.map', () => {
test('map should return a new array which applies a function to each value of input array', () => {
expect(fp.map(x => x * 2, [1,2,3])).toEqual([2,4,6]);
expect(fp.map(x => x, 'hello')).toEqual(['h', 'e', 'l', 'l', 'o']);
});
test('map should return exception if first input is not a function', () => {
expect( () => {
fp.map('not a function', [1,2,3]);
}).toThrow();
});
test('map should return exception if 2nd input is not an array or string', () => {
expect( () => {
fp.map(x => x * 2, true);
}
).toThrow();
expect( () => {
fp.map(x => x * 2, null);
}
).toThrow();
});
});
});

describe('fp.js', () => {
describe('fp.filter', () => {
test('filter should return an array of items which pass the callback', () => {
expect(fp.filter(x => x.length > 5, ['cat', 'dog', 'elephant'])).toEqual(['elephant']);
});
test('filter should return exception if first input is not a function', () => {
expect( () => {
fp.filter('not a function', ['cat', 'dog', 'elephant']);
}).toThrow();
});
test('filter should return exception if 2nd input is not iterable', () => {
expect( () => {
fp.filter(x => x.length > 5, true);
}).toThrow();
});
});
});

describe('fp.js', () => {
describe('fp.reduce', () => {
test('reduce should return value of sum of the collection', () => {
expect(fp.reduce( (acc, curr) => {
return acc + curr;
},
[1,2,3],
0)).toEqual(6);
});
test('reduce should return exception if first input is not a function', () => {
expect( () => {
fp.reduce(('not a function', [1,2,3], 0)).toThrow();
});
});
test('reduce should return exception if 2nd input is not iterable', () => {
expect( () => {
fp.reduce( (acc, curr) => {
return acc + curr;
}, true, 0);
}).toThrow();
expect( () => {
fp.reduce( (acc, curr) => {
return acc + curr;
}, null, 0);
}).toThrow();
});
});
});

describe('fp.js', () => {
describe('fp.slice', () => {
test('slice should return a portion of the given array from begin to end', () => {
expect(fp.slice(0, 2, ['cat', 'dog', 'elephant'])).toEqual(['cat', 'dog']);
});
test('slice throws an exception if the first or second arguments are not integers', () => {
expect( () => {
fp.slice((1.2, 2, ['cat', 'dog', 'elephant'])).toThrow();
});
expect( () => {
fp.slice((1, '', ['cat', 'dog', 'elephant'])).toThrow();
});
});
test('slice throws an exception if the 3rd argument is not iterable', () => {
expect( () => {
fp.slice((0, 2, 1)).toThrow();
});
expect( () => {
fp.slice((0, 2, '')).toThrow();
});
});
});
});
27 changes: 27 additions & 0 deletions lab-jeff/lib/fp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const fp = module.exports = {};

fp.map = (callback, collection) => {
if (!collection.length)
throw new TypeError('<collection> should be a string or array');
return Array.prototype.map.call(collection, callback);
};

fp.filter = (callback, collection) => {
if (!collection.length)
throw new TypeError('<collection> should be a string or array');
return Array.prototype.filter.call(collection, callback);
};

fp.reduce = (callback, collection, initValue) => {
if (!collection.length)
throw new TypeError('<collection> should be a string or array');
return Array.prototype.reduce.call(collection, callback, initValue);
};

fp.slice = (begin, end, collection) => {
if (!collection.length)
throw new TypeError('<collection> should be a string or array');
return Array.prototype.slice.call(collection, begin, end);
};
Loading