From b2846b88a180a3bb979a22154b0d7043856b1807 Mon Sep 17 00:00:00 2001 From: Predrag Josifovic Date: Tue, 28 Nov 2017 15:47:49 -0800 Subject: [PATCH 1/5] Configuring folder structure of project, writing script for eslint and jest in package.json --- .DS_Store | Bin 0 -> 6148 bytes lab-pedja/.eslintignore | 5 ++ lab-pedja/.eslintrc.json | 26 ++++++ lab-pedja/.gitignore | 148 +++++++++++++++++++++++++++++++ README.md => lab-pedja/README.md | 0 lab-pedja/index.js | 1 + lab-pedja/package.json | 16 ++++ 7 files changed, 196 insertions(+) create mode 100644 .DS_Store create mode 100644 lab-pedja/.eslintignore create mode 100644 lab-pedja/.eslintrc.json create mode 100644 lab-pedja/.gitignore rename README.md => lab-pedja/README.md (100%) create mode 100644 lab-pedja/index.js create mode 100644 lab-pedja/package.json diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 Date: Tue, 28 Nov 2017 17:36:34 -0800 Subject: [PATCH 2/5] adding fp.map test and function inside fp.js --- lab-pedja/__test__/fp.test.js | 35 +++++++++++++++++++++++++++++++++++ lab-pedja/lib/fp.js | 9 +++++++++ lab-pedja/package.json | 2 +- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 lab-pedja/__test__/fp.test.js create mode 100644 lab-pedja/lib/fp.js diff --git a/lab-pedja/__test__/fp.test.js b/lab-pedja/__test__/fp.test.js new file mode 100644 index 0000000..cbcfaff --- /dev/null +++ b/lab-pedja/__test__/fp.test.js @@ -0,0 +1,35 @@ +'use strict'; + +const fp = require('../lib/fp'); + +describe('fp.js', () => { + + describe('fp.map', () => { + test('returns array with values multiplied with 2', () => { + expect(fp.map( + (num) => num * 2, + [0, 1, 2] + )).toEqual([0, 2, 4]); + }); + test('exception will be thrown if callback is not a function', () => { + expect( + () => { + fp.map('I\'m not a function ()', [0, 1, 2]); + }).toThrow(); + }); + }); + + // describe('fp.filter', () => { + // test('') + // }); + // + // describe('fp.reduce', () => { + // test('') + // }); + // + // describe('fp.slice', () => { + // test('') + // }); + + +}); diff --git a/lab-pedja/lib/fp.js b/lab-pedja/lib/fp.js new file mode 100644 index 0000000..f7802de --- /dev/null +++ b/lab-pedja/lib/fp.js @@ -0,0 +1,9 @@ +'use strict'; + +const fp = module.exports = {}; + +fp.map = (callback, collection) => { + if(typeof callback !== 'function') + throw new TypeError(' should be a function'); + return Array.prototype.map.call(collection, callback); +}; diff --git a/lab-pedja/package.json b/lab-pedja/package.json index 3f75732..cac7327 100644 --- a/lab-pedja/package.json +++ b/lab-pedja/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "jest -i --coverage", - "lint": "eslint **/*.js" + "lint": "eslint fp.test.js" }, "author": "Pedja", "license": "ISC", From 7eae4a20a857760023bfe3135f32c49c501086af Mon Sep 17 00:00:00 2001 From: Predrag Josifovic Date: Tue, 28 Nov 2017 18:18:39 -0800 Subject: [PATCH 3/5] adding fp.reduce tests and function --- lab-pedja/__test__/fp.test.js | 80 +++++++++++++++++++++++++++++------ lab-pedja/lib/fp.js | 18 ++++++++ 2 files changed, 85 insertions(+), 13 deletions(-) diff --git a/lab-pedja/__test__/fp.test.js b/lab-pedja/__test__/fp.test.js index cbcfaff..0f959d2 100644 --- a/lab-pedja/__test__/fp.test.js +++ b/lab-pedja/__test__/fp.test.js @@ -4,32 +4,86 @@ const fp = require('../lib/fp'); describe('fp.js', () => { + // testing fp.map function describe('fp.map', () => { + test('returns array with values multiplied with 2', () => { expect(fp.map( (num) => num * 2, [0, 1, 2] )).toEqual([0, 2, 4]); }); - test('exception will be thrown if callback is not a function', () => { + + test('exception will be thrown if error occurs', () => { expect( () => { fp.map('I\'m not a function ()', [0, 1, 2]); - }).toThrow(); + }).toThrow(); + expect( + () => { + fp.map( + (num) => num * 2, + 'one' + ); + }).toThrow(); + }); + }); + + // testing fp.filter function + describe('fp.filter', () => { + + test('return value should be number higher than 3', () => { + expect(fp.filter( + (num) => num > 3, + [1, 2, 3, 4, 5] + )).toEqual([4, 5]); + }); + + test('exception will be thrown if error occurs', () => { + expect( + () => { + fp.filter('I\'m not a function ()', [0, 1, 2]); + }).toThrow(); + expect( + () => { + fp.filter( + (num) => num > 3, + 'one' + ); + }).toThrow(); }); }); - // describe('fp.filter', () => { - // test('') - // }); - // - // describe('fp.reduce', () => { - // test('') - // }); - // - // describe('fp.slice', () => { - // test('') - // }); + // testing fp.reduce function + describe('fp.reduce', () => { + test('return value should be sum of the collection if callback is adding them', () => { + expect(fp.reduce( + (accu, curr) => { + return accu + curr; + }, + [1, 2, 3], + 10 + )).toBe(16); + }); + + test('exception will be thrown if error occurs', () => { + expect( + () => { + fp.reduce('I\'m not a function ()', [0, 1, 2], 10); + } + ).toThrow(); + expect( + () => { + fp.reduce( + (accu, curr) => { + return accu + curr; + }, + [0, 1, 2], + 'someString'); + } + ).toThrow(); + }); + }); }); diff --git a/lab-pedja/lib/fp.js b/lab-pedja/lib/fp.js index f7802de..f531b4a 100644 --- a/lab-pedja/lib/fp.js +++ b/lab-pedja/lib/fp.js @@ -5,5 +5,23 @@ const fp = module.exports = {}; fp.map = (callback, collection) => { if(typeof callback !== 'function') throw new TypeError(' should be a function'); + if(typeof collection !== 'object') + throw new TypeError(' should be an array like object'); return Array.prototype.map.call(collection, callback); }; + +fp.filter = (callback, collection) => { + if(typeof callback !== 'function') + throw new TypeError(' should be a function'); + if(typeof collection !== 'object') + throw new TypeError(' should be an array like object'); + return Array.prototype.filter.call(collection, callback); +}; + +fp.reduce = (callback, collection, initialValue) => { + if(typeof callback !== 'function') + throw new TypeError(' is not a function'); + if(typeof initialValue !== 'number') + throw new TypeError(' is not a number') + return Array.prototype.reduce.call(collection, callback, initialValue); +}; From 4714fa700ca7aab4bfa760d6b06eaf55822a28c4 Mon Sep 17 00:00:00 2001 From: Predrag Josifovic Date: Tue, 28 Nov 2017 18:46:14 -0800 Subject: [PATCH 4/5] adding fp.slice tests and function --- lab-pedja/__test__/fp.test.js | 18 +++++++++++++++--- lab-pedja/lib/fp.js | 6 ++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lab-pedja/__test__/fp.test.js b/lab-pedja/__test__/fp.test.js index 0f959d2..7de9a6d 100644 --- a/lab-pedja/__test__/fp.test.js +++ b/lab-pedja/__test__/fp.test.js @@ -6,7 +6,6 @@ describe('fp.js', () => { // testing fp.map function describe('fp.map', () => { - test('returns array with values multiplied with 2', () => { expect(fp.map( (num) => num * 2, @@ -31,7 +30,6 @@ describe('fp.js', () => { // testing fp.filter function describe('fp.filter', () => { - test('return value should be number higher than 3', () => { expect(fp.filter( (num) => num > 3, @@ -56,7 +54,6 @@ describe('fp.js', () => { // testing fp.reduce function describe('fp.reduce', () => { - test('return value should be sum of the collection if callback is adding them', () => { expect(fp.reduce( (accu, curr) => { @@ -86,4 +83,19 @@ describe('fp.js', () => { }); }); + //testing fp.slice function + describe('fp.slice', () => { + test('return new array with first 3 items of an existing array', () => { + expect(fp.slice(0, 3, [0, 1, 2, 3, 4])).toEqual([0, 1, 2]); + }); + test('exception will be thrown if error occurs', () => { + expect( + () => { + fp.slice('negaiveNumber', -1, [0, 1, 2]) + } + ).toThrow(); + }); + + }); + }); diff --git a/lab-pedja/lib/fp.js b/lab-pedja/lib/fp.js index f531b4a..05b2170 100644 --- a/lab-pedja/lib/fp.js +++ b/lab-pedja/lib/fp.js @@ -25,3 +25,9 @@ fp.reduce = (callback, collection, initialValue) => { throw new TypeError(' is not a number') return Array.prototype.reduce.call(collection, callback, initialValue); }; + +fp.slice = (begin, end, collection) => { + if(typeof begin !== 'number') + throw new TypeError(' value is not a number'); + return Array.prototype.slice.call(collection, begin, end); +}; From 9a8e741fae6648eeff0e4bcbc8c946a50b742127 Mon Sep 17 00:00:00 2001 From: Predrag Josifovic Date: Tue, 28 Nov 2017 19:13:36 -0800 Subject: [PATCH 5/5] adding description in README.md file --- lab-pedja/README.md | 51 +++++++---------------------------- lab-pedja/__test__/fp.test.js | 2 +- lab-pedja/lib/fp.js | 4 +-- lab-pedja/package.json | 1 - 4 files changed, 13 insertions(+), 45 deletions(-) diff --git a/lab-pedja/README.md b/lab-pedja/README.md index 70bd927..ff0f50b 100644 --- a/lab-pedja/README.md +++ b/lab-pedja/README.md @@ -1,48 +1,17 @@ -![cf](https://i.imgur.com/7v5ASc8.png) 02: Tools and Context +![cf](https://i.imgur.com/7v5ASc8.png) 02: Lab 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. +Created in lib/ directory fp.js constains fp module that exports an object. +fp module has 4 stand alone functions `map`, `filter`, `reduce`, and `slice`. -* 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 +* Both `fp.map` and `fp.filter` take 2 parameters `(callback, collection)` and will return new Array. +Map will return array with items multiplied with 2. +Filter function will return an array of numbers that are higher than 3. +`callback` parameter has to be a function and `collection` has to be an object. If `fp.map` and `fp.filter` are invoked with not valid arguments exception will be thrown and new TypeError will be printed. -## 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. +* `fp.reduce` takes three parameters `(callback, initialState, collection)` and returns a sum of all items in collection array. +If function is invoked with callback argument that is not a function or with initialValue argument that is not a number, exception will be thrown and new TypeError will be printed. -## 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 +* `fp.slice` takes three parameters `(begin, end, collection)` and returns new array.If function is invoked with `begin` and `end` arguments that are not a number, exception will be thrown and new TypeError will be printed. diff --git a/lab-pedja/__test__/fp.test.js b/lab-pedja/__test__/fp.test.js index 7de9a6d..f9d6d69 100644 --- a/lab-pedja/__test__/fp.test.js +++ b/lab-pedja/__test__/fp.test.js @@ -6,7 +6,7 @@ describe('fp.js', () => { // testing fp.map function describe('fp.map', () => { - test('returns array with values multiplied with 2', () => { + test('returns array with items multiplied with 2', () => { expect(fp.map( (num) => num * 2, [0, 1, 2] diff --git a/lab-pedja/lib/fp.js b/lab-pedja/lib/fp.js index 05b2170..39baf27 100644 --- a/lab-pedja/lib/fp.js +++ b/lab-pedja/lib/fp.js @@ -27,7 +27,7 @@ fp.reduce = (callback, collection, initialValue) => { }; fp.slice = (begin, end, collection) => { - if(typeof begin !== 'number') - throw new TypeError(' value is not a number'); + if(typeof begin !== 'number' || typeof end !== 'number' ) + throw new TypeError(' or argument is not a number'); return Array.prototype.slice.call(collection, begin, end); }; diff --git a/lab-pedja/package.json b/lab-pedja/package.json index cac7327..9882812 100644 --- a/lab-pedja/package.json +++ b/lab-pedja/package.json @@ -10,7 +10,6 @@ "author": "Pedja", "license": "ISC", "devDependencies": { - "eslint": "^4.12.0", "jest": "^21.2.1" } }