From 53d735a6f7343c7b351966053cb3b21fa12cb2e7 Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Tue, 28 Nov 2017 14:24:45 -0800 Subject: [PATCH 01/12] scaffolding and base files --- .eslintignore | 5 +++ .eslintrc.json | 26 +++++++++++++++ .gitignore | 80 +++++++++++++++++++++++++++++++++++++++++++++ DIRECTIONS.md | 48 +++++++++++++++++++++++++++ README.md | 49 +-------------------------- __test__/fp.test.js | 13 ++++++++ index.js | 33 +++++++++++++++++++ lib/fp.js | 15 +++++++++ package.json | 18 ++++++++++ 9 files changed, 239 insertions(+), 48 deletions(-) create mode 100644 .eslintignore create mode 100644 .eslintrc.json create mode 100644 .gitignore create mode 100644 DIRECTIONS.md create mode 100644 __test__/fp.test.js create mode 100644 index.js create mode 100644 lib/fp.js create mode 100644 package.json diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..05b1cf3 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..840d336 --- /dev/null +++ b/.eslintrc.json @@ -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" ] + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c116a4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,80 @@ + +# Created by https://www.gitignore.io/api/node,linux + +### 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 + + +# End of https://www.gitignore.io/api/node,linux diff --git a/DIRECTIONS.md b/DIRECTIONS.md new file mode 100644 index 0000000..70bd927 --- /dev/null +++ b/DIRECTIONS.md @@ -0,0 +1,48 @@ +![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-` + `` **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 diff --git a/README.md b/README.md index 70bd927..2d1d7b3 100644 --- a/README.md +++ b/README.md @@ -1,48 +1 @@ -![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-` + `` **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 +# Lab 02 diff --git a/__test__/fp.test.js b/__test__/fp.test.js new file mode 100644 index 0000000..e99415d --- /dev/null +++ b/__test__/fp.test.js @@ -0,0 +1,13 @@ +'use strict'; + +const customFunct = require('../lib/custom-functions.js'); + +// P 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 diff --git a/index.js b/index.js new file mode 100644 index 0000000..b15bb63 --- /dev/null +++ b/index.js @@ -0,0 +1,33 @@ +'use strict'; + +const uuidv1 = require('uuid/v1'); + +function TagObject(name = '', age = 0){ + this.id = uuidv1(); + this.timestamp = new Date(); + this.name = name; + this.age = age; +} + +let taggedObject = new TagObject(); +// console.log(taggedObject); + +//using call +// let myCustomContext = { +// extraContextValue : 'I was assigned earlier in the code', +// }; +// console.log(myCustomContext); +// TagObject.call(myCustomContext); +// console.log(myCustomContext); + +//using apply +// let myCustomContext = { +// extraContextValue : 'I was assigned earlier in the code', +// }; +// TagObject.apply(myCustomContext, ['Mario', 30]); + +//using bind +let contextToBeBound = {}; +let boundTaggedObject = TagObject.bind(contextToBeBound); +boundTaggedObject(); +console.log(contextToBeBound); diff --git a/lib/fp.js b/lib/fp.js new file mode 100644 index 0000000..001873d --- /dev/null +++ b/lib/fp.js @@ -0,0 +1,15 @@ +'use strict'; + + +const fp = module.exports = {}; + + + +// #### 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` diff --git a/package.json b/package.json new file mode 100644 index 0000000..27578cd --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "call-bind-apply", + "version": "1.0.0", + "description": "##When to use exceptions", + "main": "index.js", + "dependencies": { + "uuid": "^3.1.0" + }, + "devDependencies": { + "jest": "^21.2.1", + "lint" : "eslint" + }, + "scripts": { + "test": "jest -i --coverage" + }, + "author": "", + "license": "ISC" +} From 715da8dbc081b7208c54e609517b667d2e918a8b Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Tue, 28 Nov 2017 14:41:32 -0800 Subject: [PATCH 02/12] wrote first test, wrote error throw in fp.js --- __test__/fp.test.js | 32 +++++++++++++++++++++++++++++++- lib/fp.js | 20 ++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/__test__/fp.test.js b/__test__/fp.test.js index e99415d..05d3674 100644 --- a/__test__/fp.test.js +++ b/__test__/fp.test.js @@ -1,6 +1,6 @@ 'use strict'; -const customFunct = require('../lib/custom-functions.js'); +const customFunct = require('../lib/fp.js'); // P Module Tests // @@ -11,3 +11,33 @@ const customFunct = require('../lib/custom-functions.js'); // 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 + +describe('fp.js', () => { + describe('fp.reduce', () => { + // parameters are f(collection, callback, initialValue) + test('return value should be the sum of the collection', ()=>{ + expect(customFunct.reduce( + (x, y) => x+y, + [1,2,3], + 0) + ).toBe(6); + }); + test('an exception should be thrown if error', ()=>{ + expect( + () => { + customFunct.reduce( + (x, y) => x+y, + ['1',2, null], + 0); + }).toThrow(); + // expect(customFunct.addWithES6(1,2,3)).toBe(6); + }); + + // test('return value should be the sum of all arguments', ()=>{ + // // parameter is f(x, y) + // // arguments are f(1, 2) + // expect(customFunct.addWithES6()).toBe(0); + // }); + + }); +}); diff --git a/lib/fp.js b/lib/fp.js index 001873d..f0a16e3 100644 --- a/lib/fp.js +++ b/lib/fp.js @@ -3,8 +3,28 @@ const fp = module.exports = {}; +fp.reduce = (callback, collection, initialValue) => { + //assumes that collection is an array like object, we should test for this + if(typeof callback !== 'function' ){ + throw new TypeError(' must be a function'); + } + if(typeof collection !== 'object' ){ + throw new TypeError(' must be an array or array like object'); + } + if(typeof callback !== 'number' || callback !== null){ + throw new TypeError(' must be a number'); + } + return Array.prototype.reduce.call(collection, callback, initialValue); +}; +// fp.sum = () => { +// if(typeof a !== 'number' || typeof b !== 'number'){ +// return null; +// } +// return a+b; +// }; + // #### 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. // From d307830c2552523954d0d082b0e2c638b5a4d3df Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Tue, 28 Nov 2017 15:17:24 -0800 Subject: [PATCH 03/12] set up second set of tests, beginning write of .map --- __test__/fp.test.js | 94 ++++++++++++++++++++++++++++++++++++--------- lib/fp.js | 15 +++++++- 2 files changed, 90 insertions(+), 19 deletions(-) diff --git a/__test__/fp.test.js b/__test__/fp.test.js index 05d3674..5c46256 100644 --- a/__test__/fp.test.js +++ b/__test__/fp.test.js @@ -2,16 +2,6 @@ const customFunct = require('../lib/fp.js'); -// P 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 - describe('fp.js', () => { describe('fp.reduce', () => { // parameters are f(collection, callback, initialValue) @@ -22,6 +12,13 @@ describe('fp.js', () => { 0) ).toBe(6); }); + test('return value should be the product of the collection', ()=>{ + expect(customFunct.reduce( + (x, y) => x*y, + [1,5,3], + 1) + ).toBe(15); + }); test('an exception should be thrown if error', ()=>{ expect( () => { @@ -30,14 +27,75 @@ describe('fp.js', () => { ['1',2, null], 0); }).toThrow(); - // expect(customFunct.addWithES6(1,2,3)).toBe(6); + expect( + () => { + customFunct.reduce( + (x, y) => x+y, + 1, + 0); + }).toThrow(); + expect( + () => { + customFunct.reduce( + (x, y) => x+y, + [1,2,3], + 'a'); + }).toThrow(); + expect( + () => { + customFunct.reduce( + 'lets do this', + [1, 2, 3], + 0); + }).toThrow(); }); - - // test('return value should be the sum of all arguments', ()=>{ - // // parameter is f(x, y) - // // arguments are f(1, 2) - // expect(customFunct.addWithES6()).toBe(0); - // }); - }); + describe('fp.map', () => { + // parameters are f(collection, callback) + test('return values should increment by two', ()=>{ + expect(customFunct.map( + (x) => x+2, + [1,2,3] + ) + ).toBe([3,4,5]); + }); + test('return values should double', ()=>{ + expect(customFunct.map( + (x) => x*2, + [1,2,3] + ) + ).toBe([2,4,6]); + }); + test('an exception should be thrown if error', ()=>{ + expect( + () => { + customFunct.map( + (x) => x+2, + ['1',2, null], + 0); + }).toThrow(); + expect( + () => { + customFunct.map( + (x) => x+2, + 1 + ); + }).toThrow(); + expect( + () => { + customFunct.map( + (x) => x+'a', + [1,2,3] + ); + }).toThrow(); + expect( + () => { + customFunct.map( + 'lets do this', + [1, 2, 3] + ); + }).toThrow(); + }); + }); + }); diff --git a/lib/fp.js b/lib/fp.js index f0a16e3..5076cf7 100644 --- a/lib/fp.js +++ b/lib/fp.js @@ -11,12 +11,25 @@ fp.reduce = (callback, collection, initialValue) => { if(typeof collection !== 'object' ){ throw new TypeError(' must be an array or array like object'); } - if(typeof callback !== 'number' || callback !== null){ + collection.forEach((x) => {if(typeof x !== 'number'){throw new TypeError(' inputs must be numbers');}}); + if(typeof initialValue !== 'number'){ + console.log('callback'); throw new TypeError(' must be a number'); } return Array.prototype.reduce.call(collection, callback, initialValue); }; +fp.map = (callback, collection) => { + //assumes that collection is an array like object, we should test for this + if(typeof callback !== 'function' ){ + throw new TypeError(' must be a function'); + } + if(typeof collection !== 'object' ){ + throw new TypeError(' must be an array or array like object'); + } + collection.forEach((x) => {if(typeof x !== 'number'){throw new TypeError(' inputs must be numbers');}}); + return Array.prototype.reduce.call(collection, callback, initialValue); +}; // fp.sum = () => { // if(typeof a !== 'number' || typeof b !== 'number'){ From be0742eb160584fc8045d483865e625cc6242688 Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Tue, 28 Nov 2017 15:27:06 -0800 Subject: [PATCH 04/12] .map tests all pass --- __test__/fp.test.js | 15 ++++----------- lib/fp.js | 6 +++++- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/__test__/fp.test.js b/__test__/fp.test.js index 5c46256..34286a4 100644 --- a/__test__/fp.test.js +++ b/__test__/fp.test.js @@ -56,15 +56,15 @@ describe('fp.js', () => { expect(customFunct.map( (x) => x+2, [1,2,3] - ) - ).toBe([3,4,5]); + ).toString() + ).toBe('3,4,5'); }); test('return values should double', ()=>{ expect(customFunct.map( (x) => x*2, [1,2,3] - ) - ).toBe([2,4,6]); + ).toString() + ).toBe('2,4,6'); }); test('an exception should be thrown if error', ()=>{ expect( @@ -81,13 +81,6 @@ describe('fp.js', () => { 1 ); }).toThrow(); - expect( - () => { - customFunct.map( - (x) => x+'a', - [1,2,3] - ); - }).toThrow(); expect( () => { customFunct.map( diff --git a/lib/fp.js b/lib/fp.js index 5076cf7..322bf26 100644 --- a/lib/fp.js +++ b/lib/fp.js @@ -28,9 +28,13 @@ fp.map = (callback, collection) => { throw new TypeError(' must be an array or array like object'); } collection.forEach((x) => {if(typeof x !== 'number'){throw new TypeError(' inputs must be numbers');}}); - return Array.prototype.reduce.call(collection, callback, initialValue); + return Array.prototype.map.call(collection, callback); }; +// fp.filter +// fp.slice + + // fp.sum = () => { // if(typeof a !== 'number' || typeof b !== 'number'){ // return null; From f7e3342b2fcd60f2c40a8214eb984378826d516c Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Tue, 28 Nov 2017 15:33:07 -0800 Subject: [PATCH 05/12] success tests for fp.filter --- __test__/fp.test.js | 46 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/__test__/fp.test.js b/__test__/fp.test.js index 34286a4..db3424c 100644 --- a/__test__/fp.test.js +++ b/__test__/fp.test.js @@ -90,5 +90,49 @@ describe('fp.js', () => { }).toThrow(); }); }); - + describe('fp.filter', () => { + // parameters are f(collection, callback) + test('return values should only have a length greater than 6', ()=>{ + expect(customFunct.filter( + (password) => password.length > 6, + ['password','pass','1234'] + ).toString() + ).toBe('password','pass','1234'); + }); + test('return values should be larger than 90', ()=>{ + expect(customFunct.filter( + () => grades > 90, + [95, 97.5, 81, 99, 23] + ).toString() + ).toBe('95, 97.5, 81, 99, 23'); + }); + test('an exception should be thrown if error', ()=>{ + expect( + () => { + customFunct.filter( + (x) => x+2, + ['1',2, null], + 0); + }).toThrow(); + expect( + () => { + customFunct.filter( + (x) => x+2, + 1 + ); + }).toThrow(); + expect( + () => { + customFunct.filter( + 'lets do this', + [1, 2, 3] + ); + }).toThrow(); + }); + }); }); + + +// `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` From ddaa796f8f2ad7787794fe5d810c43780bd2d6e2 Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Tue, 28 Nov 2017 15:37:39 -0800 Subject: [PATCH 06/12] added all error tests --- __test__/fp.test.js | 47 +++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/__test__/fp.test.js b/__test__/fp.test.js index db3424c..60bce55 100644 --- a/__test__/fp.test.js +++ b/__test__/fp.test.js @@ -1,19 +1,19 @@ 'use strict'; -const customFunct = require('../lib/fp.js'); +const fp = require('../lib/fp.js'); describe('fp.js', () => { describe('fp.reduce', () => { // parameters are f(collection, callback, initialValue) test('return value should be the sum of the collection', ()=>{ - expect(customFunct.reduce( + expect(fp.reduce( (x, y) => x+y, [1,2,3], 0) ).toBe(6); }); test('return value should be the product of the collection', ()=>{ - expect(customFunct.reduce( + expect(fp.reduce( (x, y) => x*y, [1,5,3], 1) @@ -22,28 +22,28 @@ describe('fp.js', () => { test('an exception should be thrown if error', ()=>{ expect( () => { - customFunct.reduce( + fp.reduce( (x, y) => x+y, ['1',2, null], 0); }).toThrow(); expect( () => { - customFunct.reduce( + fp.reduce( (x, y) => x+y, 1, 0); }).toThrow(); expect( () => { - customFunct.reduce( + fp.reduce( (x, y) => x+y, [1,2,3], 'a'); }).toThrow(); expect( () => { - customFunct.reduce( + fp.reduce( 'lets do this', [1, 2, 3], 0); @@ -53,14 +53,14 @@ describe('fp.js', () => { describe('fp.map', () => { // parameters are f(collection, callback) test('return values should increment by two', ()=>{ - expect(customFunct.map( + expect(fp.map( (x) => x+2, [1,2,3] ).toString() ).toBe('3,4,5'); }); test('return values should double', ()=>{ - expect(customFunct.map( + expect(fp.map( (x) => x*2, [1,2,3] ).toString() @@ -69,21 +69,21 @@ describe('fp.js', () => { test('an exception should be thrown if error', ()=>{ expect( () => { - customFunct.map( + fp.map( (x) => x+2, ['1',2, null], 0); }).toThrow(); expect( () => { - customFunct.map( + fp.map( (x) => x+2, 1 ); }).toThrow(); expect( () => { - customFunct.map( + fp.map( 'lets do this', [1, 2, 3] ); @@ -93,14 +93,14 @@ describe('fp.js', () => { describe('fp.filter', () => { // parameters are f(collection, callback) test('return values should only have a length greater than 6', ()=>{ - expect(customFunct.filter( + expect(fp.filter( (password) => password.length > 6, ['password','pass','1234'] ).toString() ).toBe('password','pass','1234'); }); test('return values should be larger than 90', ()=>{ - expect(customFunct.filter( + expect(fp.filter( () => grades > 90, [95, 97.5, 81, 99, 23] ).toString() @@ -109,22 +109,23 @@ describe('fp.js', () => { test('an exception should be thrown if error', ()=>{ expect( () => { - customFunct.filter( - (x) => x+2, - ['1',2, null], - 0); + fp.filter( + () => grades > 90, + ['kerry', 97.5, 'nicholas', 99, 23] + ); }).toThrow(); expect( () => { - customFunct.filter( - (x) => x+2, - 1 + fp.filter( + () => grades > 90, + 99 ); + }).toThrow(); expect( () => { - customFunct.filter( - 'lets do this', + fp.filter( + 'greater than 90', [1, 2, 3] ); }).toThrow(); From 0a6134cc68e008a06bbad96c542f8e41dc02d306 Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Tue, 28 Nov 2017 15:44:02 -0800 Subject: [PATCH 07/12] all fp.filter tests pass --- __test__/fp.test.js | 8 ++++---- lib/fp.js | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/__test__/fp.test.js b/__test__/fp.test.js index 60bce55..de59ce6 100644 --- a/__test__/fp.test.js +++ b/__test__/fp.test.js @@ -101,23 +101,23 @@ describe('fp.js', () => { }); test('return values should be larger than 90', ()=>{ expect(fp.filter( - () => grades > 90, + (grades) => grades > 90, [95, 97.5, 81, 99, 23] ).toString() - ).toBe('95, 97.5, 81, 99, 23'); + ).toBe('95,97.5,99'); }); test('an exception should be thrown if error', ()=>{ expect( () => { fp.filter( - () => grades > 90, + (grades) => grades > 90, ['kerry', 97.5, 'nicholas', 99, 23] ); }).toThrow(); expect( () => { fp.filter( - () => grades > 90, + (grades) => grades > 90, 99 ); diff --git a/lib/fp.js b/lib/fp.js index 322bf26..5e5a128 100644 --- a/lib/fp.js +++ b/lib/fp.js @@ -31,6 +31,19 @@ fp.map = (callback, collection) => { return Array.prototype.map.call(collection, callback); }; +fp.filter = (callback, collection) => { + //assumes that collection is an array like object, we should test for this + if(typeof callback !== 'function' ){ + throw new TypeError(' must be a function'); + } + if(typeof collection !== 'object' ){ + throw new TypeError(' must be an array or array like object'); + } + for(let i=0; i inputs must be of same type');} + } + return Array.prototype.filter.call(collection, callback); +}; // fp.filter // fp.slice From 053f9a48612be9f88224bfe544a4c7ed5aac0027 Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Tue, 28 Nov 2017 15:53:59 -0800 Subject: [PATCH 08/12] fp.slice tests written --- __test__/fp.test.js | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/__test__/fp.test.js b/__test__/fp.test.js index de59ce6..5f16725 100644 --- a/__test__/fp.test.js +++ b/__test__/fp.test.js @@ -131,9 +131,39 @@ describe('fp.js', () => { }).toThrow(); }); }); + describe('fp.slice', () => { + // parameters are f(collection, begin, end) + test('return value should be the last three values', ()=>{ + expect(fp.slice( + [1, 2, 3, 4, 5], + 2 + ).toString() + ).toBe('3,4,5'); + }); + test('return value should be the middle 3 values', ()=>{ + expect(fp.slice( + [1, 2, 3, 4, 5], + 1, + 3 + ).toString() + ).toBe('2,3,4'); + }); + test('an exception should be thrown if error', ()=>{ + expect( + () => { + fp.slice( + [1, 2, 3, 4, 5], + 5, + 1 + ); + }).toThrow(); + expect( + () => { + fp.slice( + 1, + 1, + 1); + }).toThrow(); + }); + }); }); - - -// `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` From 5face168670c23ab547b43109cfc282e21d4b38a Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Tue, 28 Nov 2017 16:02:53 -0800 Subject: [PATCH 09/12] both filter and slice dont pass tests --- __test__/fp.test.js | 2 +- lib/fp.js | 35 +++++++++++++++-------------------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/__test__/fp.test.js b/__test__/fp.test.js index 5f16725..2bc4362 100644 --- a/__test__/fp.test.js +++ b/__test__/fp.test.js @@ -97,7 +97,7 @@ describe('fp.js', () => { (password) => password.length > 6, ['password','pass','1234'] ).toString() - ).toBe('password','pass','1234'); + ).toBe('password'); }); test('return values should be larger than 90', ()=>{ expect(fp.filter( diff --git a/lib/fp.js b/lib/fp.js index 5e5a128..23dec3d 100644 --- a/lib/fp.js +++ b/lib/fp.js @@ -20,7 +20,6 @@ fp.reduce = (callback, collection, initialValue) => { }; fp.map = (callback, collection) => { - //assumes that collection is an array like object, we should test for this if(typeof callback !== 'function' ){ throw new TypeError(' must be a function'); } @@ -32,7 +31,6 @@ fp.map = (callback, collection) => { }; fp.filter = (callback, collection) => { - //assumes that collection is an array like object, we should test for this if(typeof callback !== 'function' ){ throw new TypeError(' must be a function'); } @@ -44,22 +42,19 @@ fp.filter = (callback, collection) => { } return Array.prototype.filter.call(collection, callback); }; -// fp.filter -// fp.slice - - -// fp.sum = () => { -// if(typeof a !== 'number' || typeof b !== 'number'){ -// return null; -// } -// return a+b; -// }; -// #### 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` +fp.filter = (collection, begin, end) => { + if(!Number.isInteger(begin) || !Number.isInteger(end)){ + throw new TypeError('begin and end must both be integers'); + } + if(typeof collection !== 'object' ){ + throw new TypeError(' must be an array or array like object'); + } + for(let i=0; i inputs must be of same type');} + } + if(begin >= collection.length || end >= collection.length){ + throw new TypeError(' and must not be larger than the length of the '); + } + return Array.prototype.filter.call(collection, begin, end); +}; From 3ab6bc78b450d880d72615a3211b4fb3bd2e8d99 Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Tue, 28 Nov 2017 16:18:08 -0800 Subject: [PATCH 10/12] filter broke because of syntax error, all slice tests work --- __test__/fp.test.js | 22 +++++++++++++++++++--- lib/fp.js | 16 ++++++++-------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/__test__/fp.test.js b/__test__/fp.test.js index 2bc4362..1a3a6e2 100644 --- a/__test__/fp.test.js +++ b/__test__/fp.test.js @@ -142,11 +142,11 @@ describe('fp.js', () => { }); test('return value should be the middle 3 values', ()=>{ expect(fp.slice( - [1, 2, 3, 4, 5], + [1, 2, 'three', 4, 5], 1, - 3 + 4 ).toString() - ).toBe('2,3,4'); + ).toBe('2,three,4'); }); test('an exception should be thrown if error', ()=>{ expect( @@ -157,6 +157,22 @@ describe('fp.js', () => { 1 ); }).toThrow(); + expect( + () => { + fp.slice( + [1, 2, 3, 4, 5], + 'start', + 1 + ); + }).toThrow(); + expect( + () => { + fp.slice( + [1, 2, 3, 4, 5], + 1, + 'five' + ); + }).toThrow(); expect( () => { fp.slice( diff --git a/lib/fp.js b/lib/fp.js index 23dec3d..cdd352e 100644 --- a/lib/fp.js +++ b/lib/fp.js @@ -43,18 +43,18 @@ fp.filter = (callback, collection) => { return Array.prototype.filter.call(collection, callback); }; -fp.filter = (collection, begin, end) => { - if(!Number.isInteger(begin) || !Number.isInteger(end)){ - throw new TypeError('begin and end must both be integers'); +fp.slice = (collection, begin, end) => { + if(!Number.isInteger(begin)){ + throw new TypeError('begin must be an integer'); + } + if(end && !Number.isInteger(end)){ + throw new TypeError('end must be an integer or null'); } if(typeof collection !== 'object' ){ throw new TypeError(' must be an array or array like object'); } - for(let i=0; i inputs must be of same type');} - } - if(begin >= collection.length || end >= collection.length){ + if(begin >= collection.length || end >= collection.length+1){ throw new TypeError(' and must not be larger than the length of the '); } - return Array.prototype.filter.call(collection, begin, end); + return Array.prototype.slice.call(collection, begin, end); }; From 24b321cc07285d8cb0a9c7212daf0b178cdd3e4c Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Tue, 28 Nov 2017 16:27:03 -0800 Subject: [PATCH 11/12] added README.md details for exports --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 2d1d7b3..0f68f75 100644 --- a/README.md +++ b/README.md @@ -1 +1,14 @@ # Lab 02 + +## exports + +###fp +fp exports four customized functions that affect arrays +####fp.map +takes in a callback function, and an array or array like object and returns an array of modified values. it also catches errors and returns a descriptive error message +####fp.slice +takes in an array or array like object, a begin, and an optional end and returns an array containing a subset of the original array. it also catches errors and returns a descriptive error message +####fp.reduce +takes in a callback function, an array or array like object and an inital value and returns a value or string built out of the original array. it also catches errors and returns a descriptive error message +####fp.filter +takes in a callback function, and an array or array like object and returns an array of values that match the conditions. it also catches errors and returns a descriptive error message From ce641f06b4a30386633a02852db84eeb5161ab95 Mon Sep 17 00:00:00 2001 From: Nicholas Carignan Date: Wed, 29 Nov 2017 09:23:47 -0800 Subject: [PATCH 12/12] removed skeleton code and dev comments --- __test__/fp.test.js | 2 +- index.js | 33 --------------------------------- package.json | 8 +++++--- 3 files changed, 6 insertions(+), 37 deletions(-) delete mode 100644 index.js diff --git a/__test__/fp.test.js b/__test__/fp.test.js index 1a3a6e2..7f7f135 100644 --- a/__test__/fp.test.js +++ b/__test__/fp.test.js @@ -147,7 +147,7 @@ describe('fp.js', () => { 4 ).toString() ).toBe('2,three,4'); - }); + });2, 'three', 4; test('an exception should be thrown if error', ()=>{ expect( () => { diff --git a/index.js b/index.js deleted file mode 100644 index b15bb63..0000000 --- a/index.js +++ /dev/null @@ -1,33 +0,0 @@ -'use strict'; - -const uuidv1 = require('uuid/v1'); - -function TagObject(name = '', age = 0){ - this.id = uuidv1(); - this.timestamp = new Date(); - this.name = name; - this.age = age; -} - -let taggedObject = new TagObject(); -// console.log(taggedObject); - -//using call -// let myCustomContext = { -// extraContextValue : 'I was assigned earlier in the code', -// }; -// console.log(myCustomContext); -// TagObject.call(myCustomContext); -// console.log(myCustomContext); - -//using apply -// let myCustomContext = { -// extraContextValue : 'I was assigned earlier in the code', -// }; -// TagObject.apply(myCustomContext, ['Mario', 30]); - -//using bind -let contextToBeBound = {}; -let boundTaggedObject = TagObject.bind(contextToBeBound); -boundTaggedObject(); -console.log(contextToBeBound); diff --git a/package.json b/package.json index 27578cd..967b293 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,13 @@ "uuid": "^3.1.0" }, "devDependencies": { - "jest": "^21.2.1", - "lint" : "eslint" + "eslint": "^4.12.0", + "jest": "^21.2.1" }, "scripts": { - "test": "jest -i --coverage" + "test": "jest -i --coverage", + "lint": "eslint ." + }, "author": "", "license": "ISC"