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 .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 .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"
]
}
}
93 changes: 93 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

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

### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
node_modules

# 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

# End of https://www.gitignore.io/api/osx,node
64 changes: 22 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,28 @@
![cf](https://i.imgur.com/7v5ASc8.png) 02: Tools and Context
======

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

## Configuration
Configure the root of your repository with the following files and directories. Thoughtfully name and organize any additional configuration or module files.
* **README.md** - contains documentation
* **.gitignore** - contains a [robust](http://gitignore.io) `.gitignore` file
* **.eslintrc** - 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
* **\_\_test\_\_/** - contains unit tests

## Feature Tasks
#### fp Module
Create a NodeJS module in the lib/ directory named fp.js that exports an object. Create stand-alone `map`, `filter`, `reduce`, and `slice` functions using the `call` and `apply` function methods. Define each function using ES6 lexical arrow function syntax.

In each function error-check each parameter and throw an Error with a meaningful message if the function is invoked with invalid arguments. Do not use any third party libraries in the FP module.

* `fp.map` and `fp.filter` should have the function signature `(callback, collection) => Array`
* `fp.reduce` should have the function signature `(callback, initialState, collection) => data`
* `fp.slice` should have the function signature `(begin, end, collection) => Array`

## Testing
#### FP Module Tests
Create a NodeJS module in the \_\_test\_\_/ named fp.test.js that asserts the correctness of the fp module.

* 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 fp module functions correctly error-check parameters
* Assert that the correct errors are thrown with invalid arguments
* Write tests to ensure the fp module functions returns the correct results when invoked with valid arguments

## 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 parameters (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.

## Bonus 2pts
* Create a second module fp-curry.js that is a refactored version of fp.js, where each function has curried arguments
* Create a fp-curry.test.js that is a refactored version of fp.curry.js that tests fp-curry.js

## Reduce function and test

Reduce includes a function that takes in three parameters, a callback to the reduce function, an array, and a starting value. The expeted data type for each parameter is function, object, and integer. The array may contain integers or strings and for valid use, this will return a total of each of the integers in the array. For invalid use, this will return a type error for the function if a callback is not passed.

## Map function and test

Map includes a function that takes in two parameters, a callback to the map function and an array. The expeted data type for each parameter is function and object. The array may contain integers and for valid use, return a shallow copy of the original array with each indice doubled. For invalid use, this will return a type error for the function if a callback is not passed.

##Filter function and test

Filter includes a function that takes in two parameters, a callback to the filter function and an array. The expeted data type for each parameter is function and object. The array may contain integers and for valid use, will return the odd numbers. For invalid use, this will return a type error for the function if a callback is not passed.

##Slice function and test

Slice includes a function that takes in three parameters, the beginning index, ending index, and an array. The expeted data type for each parameter is an integer, integer and an array. The array may contain integers and for valid use, it will return a shallow copy of the original array with the first five indices. For invalid use, this will return a type error for the type error for the array if an object is not passed.

##

##

##
65 changes: 65 additions & 0 deletions __test__/fp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict';

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

describe('fp.test.js', () => {
describe('ourFunctions.ourReduce', () => {

test('ourFunctions.ourReduce should return a sum of the array passed to it.', () => {
expect(ourFunctions.reduce((acc, curVal) => {
return acc + curVal;
},
[1, 2, 3],
0)).toBe(6);
});
test('If not a function, throw a error.', () => {
expect( () => {
ourFunctions.reduce ('', [1,2,3], 0);
}
).toThrow();
});
});

describe('ourFunctions.ourMap', () => {

test('ourFunctions.ourMap should iterate over an array and produce a result.', () => {
expect(ourFunctions.map(x => x * 2, [2, 3, 4])).toEqual([4, 6, 8]);
});

test('If not a function, throw a error.', () => {
expect(() => {
ourFunctions.map('', [2, 3, 4]);
}
).toThrow();
});
});

describe('ourFunctions.ourFilter', () => {

test('ourFunctions.ourFilter should take in an array and filter out the odd numbers.', () => {
expect(ourFunctions.filter(x => !(x % 2), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).toEqual([2, 4, 6, 8, 10]);
});

test('If not a function, throw a error.', () => {
expect(() => {
ourFunctions.filter('', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}
).toThrow();
});
});

describe('ourFunctions.ourSlice', () => {

test('ourFunctions.ourSlice should take in an array and remove the first five numbers.', () => {
expect(ourFunctions.slice(0, 5, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).toEqual([1, 2, 3, 4, 5]);
});

test('If not an object, throw a error.', () => {
expect(() => {
ourFunctions.slice('', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}
).toThrow();
});
});

});
35 changes: 35 additions & 0 deletions lib/fp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const ourFunctions = module.exports = {};

ourFunctions.reduce = (callback, collection, initialValue) => {
if (typeof callback !== 'function')
throw new TypeError ('<callback> must be a function');

return [].reduce.call(collection, callback, initialValue);

};

ourFunctions.map = (callback, collection) => {
if (typeof callback !== 'function')
throw new TypeError('<callback> must be a function');

return [].map.call(collection, callback);

};

ourFunctions.filter = (callback, collection) => {
if (typeof callback !== 'function')
throw new TypeError('<callback> must be a function');

return [].filter.call(collection, callback);

};

ourFunctions.slice = (begin, end, collection) => {
if (typeof collection !== 'object')
throw new TypeError('<collection> must be a function');

return [].slice.call(collection, begin, end);

};
Loading