From 7f35912e1623ddce2d4f4a15c714e026695f1476 Mon Sep 17 00:00:00 2001 From: Shannon Dillon Date: Tue, 28 Nov 2017 15:23:37 -0800 Subject: [PATCH 1/8] write and tests .map function --- lab-shannon/.eslintignore | 7 ++ lab-shannon/.eslintrc | 28 ++++++ lab-shannon/.gitignore | 148 ++++++++++++++++++++++++++++++++ lab-shannon/README.md | 3 + lab-shannon/__test__/fp.test.js | 11 +++ lab-shannon/lib/fp.js | 7 ++ lab-shannon/package.json | 14 +++ 7 files changed, 218 insertions(+) create mode 100644 lab-shannon/.eslintignore create mode 100644 lab-shannon/.eslintrc create mode 100644 lab-shannon/.gitignore create mode 100644 lab-shannon/README.md create mode 100644 lab-shannon/__test__/fp.test.js create mode 100644 lab-shannon/lib/fp.js create mode 100644 lab-shannon/package.json diff --git a/lab-shannon/.eslintignore b/lab-shannon/.eslintignore new file mode 100644 index 0000000..9ec4b48 --- /dev/null +++ b/lab-shannon/.eslintignore @@ -0,0 +1,7 @@ +# Created by Vinicio Vladimir Sanchez Trejo + +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/lab-shannon/.eslintrc b/lab-shannon/.eslintrc new file mode 100644 index 0000000..3b3fa9d --- /dev/null +++ b/lab-shannon/.eslintrc @@ -0,0 +1,28 @@ +# Created by Vinicio Vladimir Sanchez Trejo + +{ + "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/lab-shannon/.gitignore b/lab-shannon/.gitignore new file mode 100644 index 0000000..8fe2634 --- /dev/null +++ b/lab-shannon/.gitignore @@ -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 diff --git a/lab-shannon/README.md b/lab-shannon/README.md new file mode 100644 index 0000000..d751979 --- /dev/null +++ b/lab-shannon/README.md @@ -0,0 +1,3 @@ +Functions: +Function parameters (type & #): +Function output (for valid & invalid input): diff --git a/lab-shannon/__test__/fp.test.js b/lab-shannon/__test__/fp.test.js new file mode 100644 index 0000000..75f150d --- /dev/null +++ b/lab-shannon/__test__/fp.test.js @@ -0,0 +1,11 @@ +'use strict'; + +const fp = require('../lib/fp'); + +describe(`fp.test.js`, () => { + describe(`fp.map function should return`, () => { + test(`fp.map should return a new array with each element following the result of the callback function on the input provided`, () => { + expect(fp.map(x => x + 1, [1,2,3])).toEqual([2,3,4]); + }) + }) +}); diff --git a/lab-shannon/lib/fp.js b/lab-shannon/lib/fp.js new file mode 100644 index 0000000..de8d22d --- /dev/null +++ b/lab-shannon/lib/fp.js @@ -0,0 +1,7 @@ +'use strict'; + +const fp = module.exports = {}; + +fp.map = (callback, collection) => { + return Array.prototype.map.call(collection, callback); +}; diff --git a/lab-shannon/package.json b/lab-shannon/package.json new file mode 100644 index 0000000..cce91d5 --- /dev/null +++ b/lab-shannon/package.json @@ -0,0 +1,14 @@ +{ + "name": "lab-shannon", + "version": "1.0.0", + "description": "writing stand-alone map, reduce, filter, and slice methods", + "main": "index.js", + "directories": { + "lib": "lib" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "shannon", + "license": "MIT" +} From 58d4d3d6ef10592bbf0f9d1bef468b7f643b0a80 Mon Sep 17 00:00:00 2001 From: Shannon Dillon Date: Tue, 28 Nov 2017 16:01:15 -0800 Subject: [PATCH 2/8] write and pass tests for .filter method --- lab-shannon/README.md | 4 +++- lab-shannon/__test__/fp.test.js | 16 +++++++++++++--- lab-shannon/lib/fp.js | 8 ++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lab-shannon/README.md b/lab-shannon/README.md index d751979..d24ef5c 100644 --- a/lab-shannon/README.md +++ b/lab-shannon/README.md @@ -1,3 +1,5 @@ -Functions: +The program contains custom .map, .filter, .slice, and .reduce functions. +.map: The .map function takes two parameters- a callback function and an array of values. +.filter: The .filter functions takes two parameters- a callback function and an array of values. Function parameters (type & #): Function output (for valid & invalid input): diff --git a/lab-shannon/__test__/fp.test.js b/lab-shannon/__test__/fp.test.js index 75f150d..08379fb 100644 --- a/lab-shannon/__test__/fp.test.js +++ b/lab-shannon/__test__/fp.test.js @@ -3,9 +3,19 @@ const fp = require('../lib/fp'); describe(`fp.test.js`, () => { - describe(`fp.map function should return`, () => { - test(`fp.map should return a new array with each element following the result of the callback function on the input provided`, () => { + describe(`fp.map function`, () => { + test(`fp.map should return a new array with each element altered based on the callback function`, () => { expect(fp.map(x => x + 1, [1,2,3])).toEqual([2,3,4]); - }) + expect(fp.map(x => x + 1, [])).toEqual([]); + expect(fp.map(x => x * 2, [1,2,3])).toEqual([2,4,6]); + }); + // test(`fp.map should throw an error if the callback provided is not a function`, () => { + // expect(fp.map(`I'm not a function`, [1,2,3])).toThrow(); + // }); + }) + describe(`fp.filter`, () => { + test(`fp.filter should return a new array with only the elements which fit the specifications provided`, () => { + expect(fp.filter(num => num > 8, [7,8,9])).toEqual([9]); + }); }) }); diff --git a/lab-shannon/lib/fp.js b/lab-shannon/lib/fp.js index de8d22d..7fab081 100644 --- a/lab-shannon/lib/fp.js +++ b/lab-shannon/lib/fp.js @@ -3,5 +3,13 @@ const fp = module.exports = {}; fp.map = (callback, collection) => { + if(typeof callback !== 'function'){ + throw new TypeError('The callback you provided is not a function'); + } + return Array.prototype.map.call(collection, callback); }; + +fp.filter = (callback, collection) => { + return Array.prototype.filter.call(collection, callback); +} From 7a200fc7bf4a30e2725cad6571cebc0d01fd7c72 Mon Sep 17 00:00:00 2001 From: Shannon Dillon Date: Tue, 28 Nov 2017 16:41:15 -0800 Subject: [PATCH 3/8] get test checking if callback is a function or not to work --- lab-shannon/__test__/fp.test.js | 15 ++++++++++++--- lab-shannon/lib/fp.js | 4 ++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lab-shannon/__test__/fp.test.js b/lab-shannon/__test__/fp.test.js index 08379fb..cae1f7a 100644 --- a/lab-shannon/__test__/fp.test.js +++ b/lab-shannon/__test__/fp.test.js @@ -9,13 +9,22 @@ describe(`fp.test.js`, () => { expect(fp.map(x => x + 1, [])).toEqual([]); expect(fp.map(x => x * 2, [1,2,3])).toEqual([2,4,6]); }); - // test(`fp.map should throw an error if the callback provided is not a function`, () => { - // expect(fp.map(`I'm not a function`, [1,2,3])).toThrow(); - // }); + test(`fp.map should throw an error if the callback provided is not a function`, () => { + expect(() => { + fp.map(`I'm not a function`, [1,2,3]) + }).toThrow(`The callback you provided is not a function`) + }); }) describe(`fp.filter`, () => { test(`fp.filter should return a new array with only the elements which fit the specifications provided`, () => { expect(fp.filter(num => num > 8, [7,8,9])).toEqual([9]); + expect(fp.filter(string => string.length > 2, ['a', 'hello', 'eh', 'goodbye'])).toEqual(['hello', 'goodbye']); }); }) + + // describe(`fp.slice`, () => { + // test(`fp.slice should return a portion of the input from the specified start point to end point (not inclusive`, () => { + // expect(fp.slice(0, 3, 'coding')).toEqual('cod'); + // }) + // }) }); diff --git a/lab-shannon/lib/fp.js b/lab-shannon/lib/fp.js index 7fab081..cbb7ba5 100644 --- a/lab-shannon/lib/fp.js +++ b/lab-shannon/lib/fp.js @@ -13,3 +13,7 @@ fp.map = (callback, collection) => { fp.filter = (callback, collection) => { return Array.prototype.filter.call(collection, callback); } + +fp.slice = (start, stop, collection) => { + return Array.prototype.slice.call(collection, start, stop); +} From 02fdd7c366a3fc9bc763b60b61694567f97a8b46 Mon Sep 17 00:00:00 2001 From: Shannon Dillon Date: Tue, 28 Nov 2017 16:54:34 -0800 Subject: [PATCH 4/8] write and pass tests for fp.reduce --- lab-shannon/__test__/fp.test.js | 16 +++++++++++----- lab-shannon/lib/fp.js | 4 ++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lab-shannon/__test__/fp.test.js b/lab-shannon/__test__/fp.test.js index cae1f7a..66f75da 100644 --- a/lab-shannon/__test__/fp.test.js +++ b/lab-shannon/__test__/fp.test.js @@ -22,9 +22,15 @@ describe(`fp.test.js`, () => { }); }) - // describe(`fp.slice`, () => { - // test(`fp.slice should return a portion of the input from the specified start point to end point (not inclusive`, () => { - // expect(fp.slice(0, 3, 'coding')).toEqual('cod'); - // }) - // }) + describe(`fp.slice`, () => { + test(`fp.slice should return a portion of the input from the specified start point to end point (not inclusive`, () => { + expect(fp.slice(0, 4, ['coding', 'is', 'the', 'best', 'ever'])).toEqual(['coding', 'is', 'the', 'best']); + }) + }) + + describe(`fp.reduce`, () => { + test(`fp.reduce should return a single value by adding the current element's value in the initial collection onto the starting value provided`, () => { + expect(fp.reduce((accumulator, currentValue) => {return accumulator + currentValue}, [0,1,2,3], 0)).toEqual(6); + }) + }) }); diff --git a/lab-shannon/lib/fp.js b/lab-shannon/lib/fp.js index cbb7ba5..487a976 100644 --- a/lab-shannon/lib/fp.js +++ b/lab-shannon/lib/fp.js @@ -17,3 +17,7 @@ fp.filter = (callback, collection) => { fp.slice = (start, stop, collection) => { return Array.prototype.slice.call(collection, start, stop); } + +fp.reduce = (callback, collection, initialValue) => { + return Array.prototype.reduce.call(collection, callback, initialValue) +} From 90f8af68706e8141a747199b9992199bce871353 Mon Sep 17 00:00:00 2001 From: Shannon Dillon Date: Tue, 28 Nov 2017 17:43:13 -0800 Subject: [PATCH 5/8] write tests for if callback is a function for all functions --- lab-shannon/README.md | 12 ++++++++---- lab-shannon/__test__/fp.test.js | 21 ++++++++++++++++++--- lab-shannon/lib/fp.js | 7 ++++--- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lab-shannon/README.md b/lab-shannon/README.md index d24ef5c..e311a19 100644 --- a/lab-shannon/README.md +++ b/lab-shannon/README.md @@ -1,5 +1,9 @@ The program contains custom .map, .filter, .slice, and .reduce functions. -.map: The .map function takes two parameters- a callback function and an array of values. -.filter: The .filter functions takes two parameters- a callback function and an array of values. -Function parameters (type & #): -Function output (for valid & invalid input): + +.map: The .map function takes two parameters- a callback function and an array of values. It returns a new array containing elements altered from the initial array according to the callback function provided. If the argument provided for the callback is not a function a Type Error is thrown. If one of the elements in the collection is of the wrong type for the callback function provided (i.e. trying to add a string to numbers) a Type Error will be thrown. + +.filter: The .filter function takes two parameters- a callback function and an array of values. It returns a new array containing only the elements which fit the criteria provided in the callback function. If the argument provided for the callback is not a function a Type Error is thrown. If one of the elements in the collection is of the wrong type for the callback function provided (i.e. trying to add a string to numbers) a Type Error will be thrown. + +.slice: The .slice function takes three parameters- a starting position (number), a ending position (number), and an array of data. It returns a new array of elements from the starting point to the ending point (not inclusive) in the collection provided. If the argument provided for the callback is not a function a Type Error is thrown. If one of the elements in the collection is of the wrong type for the callback function provided (i.e. trying to add a string to numbers) a Type Error will be thrown. + +.reduce: The .reduce function takes three parameters- a callback function, an array of data, and an initial value. It returns a single value resulting from adding each value in the collection onto the initial value provided. If the argument provided for the callback is not a function a Type Error is thrown. If one of the elements in the collection is of the wrong type for the callback function provided (i.e. trying to add a string to numbers) a Type Error will be thrown. diff --git a/lab-shannon/__test__/fp.test.js b/lab-shannon/__test__/fp.test.js index 66f75da..a6a1364 100644 --- a/lab-shannon/__test__/fp.test.js +++ b/lab-shannon/__test__/fp.test.js @@ -9,10 +9,10 @@ describe(`fp.test.js`, () => { expect(fp.map(x => x + 1, [])).toEqual([]); expect(fp.map(x => x * 2, [1,2,3])).toEqual([2,4,6]); }); - test(`fp.map should throw an error if the callback provided is not a function`, () => { + test.only(`fp.map should throw a Type Error if the callback provided is not a function`, () => { expect(() => { - fp.map(`I'm not a function`, [1,2,3]) - }).toThrow(`The callback you provided is not a function`) + fp.map('I am not a function',[1,2,3]) + }).toThrow(); }); }) describe(`fp.filter`, () => { @@ -20,17 +20,32 @@ describe(`fp.test.js`, () => { expect(fp.filter(num => num > 8, [7,8,9])).toEqual([9]); expect(fp.filter(string => string.length > 2, ['a', 'hello', 'eh', 'goodbye'])).toEqual(['hello', 'goodbye']); }); + test(`fp.filter should throw a Type Error if the callback provided is not a function`, () => { + expect(() => { + fp.filter(`I'm not a function`), [1,2,3] + }).toThrow(); + }) }) describe(`fp.slice`, () => { test(`fp.slice should return a portion of the input from the specified start point to end point (not inclusive`, () => { expect(fp.slice(0, 4, ['coding', 'is', 'the', 'best', 'ever'])).toEqual(['coding', 'is', 'the', 'best']); }) + test(`fp.slice should throw a Type Error if the callback provided is not a function`, () => { + expect(() => { + fp.slice(`I am not a function!`, [1,2,3]) + }).toThrow(); + }) }) describe(`fp.reduce`, () => { test(`fp.reduce should return a single value by adding the current element's value in the initial collection onto the starting value provided`, () => { expect(fp.reduce((accumulator, currentValue) => {return accumulator + currentValue}, [0,1,2,3], 0)).toEqual(6); }) + test(`fp.reduce should throw a Type Error if the callback provided is not a function`, () => { + expect(() => { + fp.reduce(`I am not a function!`, [1,2,3]) + }).toThrow(); + }) }) }); diff --git a/lab-shannon/lib/fp.js b/lab-shannon/lib/fp.js index 487a976..3800a32 100644 --- a/lab-shannon/lib/fp.js +++ b/lab-shannon/lib/fp.js @@ -3,9 +3,10 @@ const fp = module.exports = {}; fp.map = (callback, collection) => { - if(typeof callback !== 'function'){ - throw new TypeError('The callback you provided is not a function'); - } + // if(typeof callback !== 'function'){ + // console.log(typeof callback, `callback`); + // throw new TypeError('The callback you provided is not a function'); + // } return Array.prototype.map.call(collection, callback); }; From 0dd192211e11eabd1b0c850eb216f26bf1c741cb Mon Sep 17 00:00:00 2001 From: Shannon Dillon Date: Tue, 28 Nov 2017 17:47:00 -0800 Subject: [PATCH 6/8] clarify wording in README --- lab-shannon/README.md | 4 ++-- lab-shannon/__test__/fp.test.js | 11 +++++------ lab-shannon/lib/fp.js | 14 ++++++++++---- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lab-shannon/README.md b/lab-shannon/README.md index e311a19..c243623 100644 --- a/lab-shannon/README.md +++ b/lab-shannon/README.md @@ -2,8 +2,8 @@ The program contains custom .map, .filter, .slice, and .reduce functions. .map: The .map function takes two parameters- a callback function and an array of values. It returns a new array containing elements altered from the initial array according to the callback function provided. If the argument provided for the callback is not a function a Type Error is thrown. If one of the elements in the collection is of the wrong type for the callback function provided (i.e. trying to add a string to numbers) a Type Error will be thrown. -.filter: The .filter function takes two parameters- a callback function and an array of values. It returns a new array containing only the elements which fit the criteria provided in the callback function. If the argument provided for the callback is not a function a Type Error is thrown. If one of the elements in the collection is of the wrong type for the callback function provided (i.e. trying to add a string to numbers) a Type Error will be thrown. +.filter: The .filter function takes two parameters- a callback function and an array of values. It returns a new array containing only the elements which fit the criteria provided in the callback function. If the argument provided for the callback is not a function a Type Error is thrown. -.slice: The .slice function takes three parameters- a starting position (number), a ending position (number), and an array of data. It returns a new array of elements from the starting point to the ending point (not inclusive) in the collection provided. If the argument provided for the callback is not a function a Type Error is thrown. If one of the elements in the collection is of the wrong type for the callback function provided (i.e. trying to add a string to numbers) a Type Error will be thrown. +.slice: The .slice function takes three parameters- a starting position (number), a ending position (number), and an array of data. It returns a new array of elements from the starting point to the ending point (not inclusive) in the collection provided. If one of the elements in the collection is of the wrong type for the callback function provided (i.e. trying to slice a string instead of elements in an array) a Type Error will be thrown. .reduce: The .reduce function takes three parameters- a callback function, an array of data, and an initial value. It returns a single value resulting from adding each value in the collection onto the initial value provided. If the argument provided for the callback is not a function a Type Error is thrown. If one of the elements in the collection is of the wrong type for the callback function provided (i.e. trying to add a string to numbers) a Type Error will be thrown. diff --git a/lab-shannon/__test__/fp.test.js b/lab-shannon/__test__/fp.test.js index a6a1364..92d44b0 100644 --- a/lab-shannon/__test__/fp.test.js +++ b/lab-shannon/__test__/fp.test.js @@ -9,11 +9,12 @@ describe(`fp.test.js`, () => { expect(fp.map(x => x + 1, [])).toEqual([]); expect(fp.map(x => x * 2, [1,2,3])).toEqual([2,4,6]); }); - test.only(`fp.map should throw a Type Error if the callback provided is not a function`, () => { + test(`fp.map should throw a Type Error if the callback provided is not a function`, () => { expect(() => { fp.map('I am not a function',[1,2,3]) }).toThrow(); }); + //check the type of the arguments provided }) describe(`fp.filter`, () => { test(`fp.filter should return a new array with only the elements which fit the specifications provided`, () => { @@ -25,17 +26,14 @@ describe(`fp.test.js`, () => { fp.filter(`I'm not a function`), [1,2,3] }).toThrow(); }) + //check the type of the arguments provided }) describe(`fp.slice`, () => { test(`fp.slice should return a portion of the input from the specified start point to end point (not inclusive`, () => { expect(fp.slice(0, 4, ['coding', 'is', 'the', 'best', 'ever'])).toEqual(['coding', 'is', 'the', 'best']); }) - test(`fp.slice should throw a Type Error if the callback provided is not a function`, () => { - expect(() => { - fp.slice(`I am not a function!`, [1,2,3]) - }).toThrow(); - }) + //check the type of the arguments provided }) describe(`fp.reduce`, () => { @@ -47,5 +45,6 @@ describe(`fp.test.js`, () => { fp.reduce(`I am not a function!`, [1,2,3]) }).toThrow(); }) + //check the type of the arguments provided }) }); diff --git a/lab-shannon/lib/fp.js b/lab-shannon/lib/fp.js index 3800a32..f244aff 100644 --- a/lab-shannon/lib/fp.js +++ b/lab-shannon/lib/fp.js @@ -3,22 +3,28 @@ const fp = module.exports = {}; fp.map = (callback, collection) => { - // if(typeof callback !== 'function'){ - // console.log(typeof callback, `callback`); - // throw new TypeError('The callback you provided is not a function'); - // } + if(typeof callback !== 'function'){ + throw new TypeError('The callback you provided is not a function'); + } return Array.prototype.map.call(collection, callback); }; fp.filter = (callback, collection) => { + if(typeof callback !== 'function'){ + throw new TypeError('The callback you provided is not a function'); + } return Array.prototype.filter.call(collection, callback); } fp.slice = (start, stop, collection) => { + return Array.prototype.slice.call(collection, start, stop); } fp.reduce = (callback, collection, initialValue) => { + if(typeof callback !== 'function'){ + throw new TypeError('The callback you provided is not a function'); + } return Array.prototype.reduce.call(collection, callback, initialValue) } From 936c0e00d4b00ae9c47fa8b6fe50a65e7465dff4 Mon Sep 17 00:00:00 2001 From: Shannon Dillon Date: Tue, 28 Nov 2017 21:33:29 -0800 Subject: [PATCH 7/8] check that collection is an array for each method; check that slice's start is a positive number --- lab-shannon/__test__/fp.test.js | 37 ++++++++++++++++++++++++++++----- lab-shannon/lib/fp.js | 28 +++++++++++++++++++++---- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/lab-shannon/__test__/fp.test.js b/lab-shannon/__test__/fp.test.js index 92d44b0..4fa7a3a 100644 --- a/lab-shannon/__test__/fp.test.js +++ b/lab-shannon/__test__/fp.test.js @@ -6,7 +6,7 @@ describe(`fp.test.js`, () => { describe(`fp.map function`, () => { test(`fp.map should return a new array with each element altered based on the callback function`, () => { expect(fp.map(x => x + 1, [1,2,3])).toEqual([2,3,4]); - expect(fp.map(x => x + 1, [])).toEqual([]); + expect(fp.map(x => x + 3, [])).toEqual([]); expect(fp.map(x => x * 2, [1,2,3])).toEqual([2,4,6]); }); test(`fp.map should throw a Type Error if the callback provided is not a function`, () => { @@ -14,8 +14,13 @@ describe(`fp.test.js`, () => { fp.map('I am not a function',[1,2,3]) }).toThrow(); }); - //check the type of the arguments provided + test(`fp.map should throw an error if the collection provided is not an array`, () => { + expect(() => { + fp.map(x => x + 1, 'I am not an array') + }).toThrow(); + }) }) + describe(`fp.filter`, () => { test(`fp.filter should return a new array with only the elements which fit the specifications provided`, () => { expect(fp.filter(num => num > 8, [7,8,9])).toEqual([9]); @@ -26,14 +31,27 @@ describe(`fp.test.js`, () => { fp.filter(`I'm not a function`), [1,2,3] }).toThrow(); }) - //check the type of the arguments provided + test(`fp.filter should throw an error if the collection provided is not an array`, () => { + expect(() => { + fp.filter(string => string.length < 8, 'I am not an array') + }).toThrow(); + }) }) describe(`fp.slice`, () => { test(`fp.slice should return a portion of the input from the specified start point to end point (not inclusive`, () => { expect(fp.slice(0, 4, ['coding', 'is', 'the', 'best', 'ever'])).toEqual(['coding', 'is', 'the', 'best']); }) - //check the type of the arguments provided + test(`fp.slice should throw an error if the collection provided is not an array`, () => { + expect(() => { + fp.slice(1, 3, 'I am not an array') + }).toThrow(); + }) + test(`fp.slice should throw an error if start is not a positive number`, () => { + expect(() => { + fp.slice(-1, 3, [`do`, `you`, `like`, `coding`]) + }).toThrow(); + }) }) describe(`fp.reduce`, () => { @@ -45,6 +63,15 @@ describe(`fp.test.js`, () => { fp.reduce(`I am not a function!`, [1,2,3]) }).toThrow(); }) - //check the type of the arguments provided + test(`fp.reduce should throw an error if the collection provided is not an array`, () => { + expect(() => { + fp.reduce((accumulator, currentValue) => {return accumulator + currentValue}, 'I am not an array', 0) + }).toThrow(); + }) + test(`fp.reduce should throw an error if the accumulator's initial value is not a number`, () => { + expect(() => { + fp.reduce((accumulator, currentValue) => {return accumulator + currentValue}, [0,1,2], 'banana') + }).toThrow(); + }) }) }); diff --git a/lab-shannon/lib/fp.js b/lab-shannon/lib/fp.js index f244aff..2434364 100644 --- a/lab-shannon/lib/fp.js +++ b/lab-shannon/lib/fp.js @@ -3,28 +3,48 @@ const fp = module.exports = {}; fp.map = (callback, collection) => { - if(typeof callback !== 'function'){ + if (typeof callback !== 'function'){ throw new TypeError('The callback you provided is not a function'); } + else if (!Array.isArray(collection)){ + throw new TypeError(`The collection provided must be an array`); + } return Array.prototype.map.call(collection, callback); }; fp.filter = (callback, collection) => { - if(typeof callback !== 'function'){ + if (typeof callback !== 'function'){ throw new TypeError('The callback you provided is not a function'); } + else if (!Array.isArray(collection)){ + throw new TypeError(`The collection provided must be an array`); + } + return Array.prototype.filter.call(collection, callback); } fp.slice = (start, stop, collection) => { - + if (!Array.isArray(collection)){ + throw new TypeError(`The collection provided must be an array`); + } + else if (typeof start !== `number` || start < 0){ + throw new Error(`Start must be a positive whole number`) + } + return Array.prototype.slice.call(collection, start, stop); } fp.reduce = (callback, collection, initialValue) => { - if(typeof callback !== 'function'){ + if (typeof callback !== 'function'){ throw new TypeError('The callback you provided is not a function'); } + else if (!Array.isArray(collection)){ + throw new TypeError(`The collection provided must be an array`); + } + else if (typeof initialValue !== `number`){ + throw new TypeError(`The accumulator must be a number`); + } + return Array.prototype.reduce.call(collection, callback, initialValue) } From 6f651d4f67ab83827b9486e7dd0bed848983f784 Mon Sep 17 00:00:00 2001 From: Shannon Dillon Date: Tue, 28 Nov 2017 21:54:02 -0800 Subject: [PATCH 8/8] check that all elements in the reduce array are numbers --- lab-shannon/__test__/fp.test.js | 10 ++++++++++ lab-shannon/lib/fp.js | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/lab-shannon/__test__/fp.test.js b/lab-shannon/__test__/fp.test.js index 4fa7a3a..6046b0a 100644 --- a/lab-shannon/__test__/fp.test.js +++ b/lab-shannon/__test__/fp.test.js @@ -52,6 +52,11 @@ describe(`fp.test.js`, () => { fp.slice(-1, 3, [`do`, `you`, `like`, `coding`]) }).toThrow(); }) + test(`fp.slice should throw an error if stop is not a positive number`, () => { + expect(() => { + fp.slice(1, -3, [`do`, `you`, `like`, `coding`]) + }).toThrow(); + }) }) describe(`fp.reduce`, () => { @@ -73,5 +78,10 @@ describe(`fp.test.js`, () => { fp.reduce((accumulator, currentValue) => {return accumulator + currentValue}, [0,1,2], 'banana') }).toThrow(); }) + test(`fp.reduce should throw an error if one of the elements in the collection is not a number`, () => { + expect(() => { + fp.reduce((accumulator, currentValue) => {return accumulator + currentValue}, [1,2,`pie`], 0 + )}).toThrow(); + }) }) }); diff --git a/lab-shannon/lib/fp.js b/lab-shannon/lib/fp.js index 2434364..bee9406 100644 --- a/lab-shannon/lib/fp.js +++ b/lab-shannon/lib/fp.js @@ -31,6 +31,9 @@ fp.slice = (start, stop, collection) => { else if (typeof start !== `number` || start < 0){ throw new Error(`Start must be a positive whole number`) } + else if (typeof stop !== `number` || stop < 0){ + throw new Error(`Start must be a positive whole number`) + } return Array.prototype.slice.call(collection, start, stop); } @@ -45,6 +48,11 @@ fp.reduce = (callback, collection, initialValue) => { else if (typeof initialValue !== `number`){ throw new TypeError(`The accumulator must be a number`); } + else if (!collection.every((num) => { + return typeof num === `number`; + })){ + throw new Error(`All elements of the array must be a number`); + } return Array.prototype.reduce.call(collection, callback, initialValue) }