From c8bc50efcb0f31a00c04fdc4a0d42b7774aab3e0 Mon Sep 17 00:00:00 2001 From: Edwin Del Rio Date: Tue, 18 Jul 2017 13:33:19 -0700 Subject: [PATCH 01/10] Created folder scaffolding, created package.JSON with dev dependancies Will serve as the foundation for the tests and js files. --- .gitignore | 128 +++++++++++++++++++++++++++++++++++++++++ lab-eddie/package.json | 19 ++++++ 2 files changed, 147 insertions(+) create mode 100644 .gitignore create mode 100644 lab-eddie/package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3befe59 --- /dev/null +++ b/.gitignore @@ -0,0 +1,128 @@ + +# Created by https://www.gitignore.io/api/osx,vim,node,windows + +### 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 + +### 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,windows +/lab-eddie/node_modules diff --git a/lab-eddie/package.json b/lab-eddie/package.json new file mode 100644 index 0000000..b8b6df7 --- /dev/null +++ b/lab-eddie/package.json @@ -0,0 +1,19 @@ +{ + "name": "lab-eddie", + "version": "1.0.0", + "description": "", + "main": "index.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "devDependencies": { + "chai": "^4.1.0", + "mocha": "^3.4.2" + } +} From 26ab8d9a10d13fa84681882c3e3281fdaff819f8 Mon Sep 17 00:00:00 2001 From: Edwin Del Rio Date: Tue, 18 Jul 2017 14:16:31 -0700 Subject: [PATCH 02/10] Created fp.js to write stand alone array methods --- lab-eddie/lib/fp.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 lab-eddie/lib/fp.js diff --git a/lab-eddie/lib/fp.js b/lab-eddie/lib/fp.js new file mode 100644 index 0000000..e69de29 From 5bdac1eb09b9daa17446e11a9b75118d9ac741eb Mon Sep 17 00:00:00 2001 From: Edwin Del Rio Date: Tue, 18 Jul 2017 15:00:17 -0700 Subject: [PATCH 03/10] Created error handling functions to reuse in stand alone functions --- lab-eddie/lib/fp.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lab-eddie/lib/fp.js b/lab-eddie/lib/fp.js index e69de29..6c6295e 100644 --- a/lab-eddie/lib/fp.js +++ b/lab-eddie/lib/fp.js @@ -0,0 +1,38 @@ +'use strict'; + + +const argumentLengthError = (argLength, expectedLength) => { + if(argLength !== expectedLength) throw new Error(`Function requires ${expectedLength} arguments. Only ${argLength} provided.`); +}; +const typeError = (objType, expectedType) => { + if(objType !== expectedType) throw new Error(`Function expected ${expectedType} type parameter but recieved ${objType} instead.`); +}; + + +function map (arr, callback) { + let argumentLength = arguments.length; + argumentLengthError(argumentLength, 2); + typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + typeError(typeof callback, 'function'); +} + +function map (arr, callback) { + let argumentLength = arguments.length; + argumentLengthError(argumentLength, 2); + typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + typeError(typeof callback, 'function'); +} + +function map (arr, callback) { + let argumentLength = arguments.length; + argumentLengthError(argumentLength, 2); + typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + typeError(typeof callback, 'function'); +} + +function map (arr, callback) { + let argumentLength = arguments.length; + argumentLengthError(argumentLength, 2); + typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + typeError(typeof callback, 'function'); +} From 4c2d987df77fba3f924777eeba1acddb4414c337 Mon Sep 17 00:00:00 2001 From: Edwin Del Rio Date: Tue, 18 Jul 2017 15:37:20 -0700 Subject: [PATCH 04/10] Wrote code for all standalone Array methods --- lab-eddie/lib/fp.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/lab-eddie/lib/fp.js b/lab-eddie/lib/fp.js index 6c6295e..6279596 100644 --- a/lab-eddie/lib/fp.js +++ b/lab-eddie/lib/fp.js @@ -9,30 +9,42 @@ const typeError = (objType, expectedType) => { }; -function map (arr, callback) { +function map(arr, callback) { let argumentLength = arguments.length; argumentLengthError(argumentLength, 2); typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); typeError(typeof callback, 'function'); + return Map.prototype.map.call(arr, callback); } -function map (arr, callback) { +function filter(arr, callback) { let argumentLength = arguments.length; argumentLengthError(argumentLength, 2); typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); typeError(typeof callback, 'function'); + return Map.prototype.filter.call(arr, callback); } -function map (arr, callback) { +function reduce(arr, callback, counter=arr[0]) { let argumentLength = arguments.length; - argumentLengthError(argumentLength, 2); + argumentLengthError(argumentLength, 3); typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); typeError(typeof callback, 'function'); + return Map.prototype.reduce.call(arr, callback, counter); } -function map (arr, callback) { +function concat(firstArray, secondArray) { let argumentLength = arguments.length; argumentLengthError(argumentLength, 2); - typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); - typeError(typeof callback, 'function'); + typeError(Array.isArray(firstArray) ? 'array' : 'not an array', 'array'); + typeError(Array.isArray(secondArray) ? 'array' : 'not an array', 'array'); + Map.prototype.concat.apply(firstArray, secondArray); +} + +function splice (startingIndex, amountToDelete) { + let argumentLength = arguments.length; + argumentLengthError(argumentLength, 1); + typeError(typeof startingIndex, 'number'); + typeError(typeof amountToDelete, 'number'); + Map.prototype.splice.apply(firstArray, secondArray); } From 23ad034724f314f7cb297d8416376b6406af2dcb Mon Sep 17 00:00:00 2001 From: Edwin Del Rio Date: Tue, 18 Jul 2017 16:05:29 -0700 Subject: [PATCH 05/10] Fixed bug in splice stand alone --- lab-eddie/lib/fp.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lab-eddie/lib/fp.js b/lab-eddie/lib/fp.js index 6279596..929fd29 100644 --- a/lab-eddie/lib/fp.js +++ b/lab-eddie/lib/fp.js @@ -2,7 +2,7 @@ const argumentLengthError = (argLength, expectedLength) => { - if(argLength !== expectedLength) throw new Error(`Function requires ${expectedLength} arguments. Only ${argLength} provided.`); + if(argLength < expectedLength) throw new Error(`Function requires ${expectedLength} arguments. Only ${argLength} provided.`); }; const typeError = (objType, expectedType) => { if(objType !== expectedType) throw new Error(`Function expected ${expectedType} type parameter but recieved ${objType} instead.`); @@ -14,7 +14,7 @@ function map(arr, callback) { argumentLengthError(argumentLength, 2); typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); typeError(typeof callback, 'function'); - return Map.prototype.map.call(arr, callback); + return Array.prototype.map.call(arr, callback); } function filter(arr, callback) { @@ -22,7 +22,7 @@ function filter(arr, callback) { argumentLengthError(argumentLength, 2); typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); typeError(typeof callback, 'function'); - return Map.prototype.filter.call(arr, callback); + return Array.prototype.filter.call(arr, callback); } function reduce(arr, callback, counter=arr[0]) { @@ -30,7 +30,7 @@ function reduce(arr, callback, counter=arr[0]) { argumentLengthError(argumentLength, 3); typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); typeError(typeof callback, 'function'); - return Map.prototype.reduce.call(arr, callback, counter); + return Array.prototype.reduce.call(arr, callback, counter); } function concat(firstArray, secondArray) { @@ -38,13 +38,14 @@ function concat(firstArray, secondArray) { argumentLengthError(argumentLength, 2); typeError(Array.isArray(firstArray) ? 'array' : 'not an array', 'array'); typeError(Array.isArray(secondArray) ? 'array' : 'not an array', 'array'); - Map.prototype.concat.apply(firstArray, secondArray); + Array.prototype.concat.apply(firstArray, secondArray); } -function splice (startingIndex, amountToDelete) { +function splice(arr, startingIndex, amountToDelete) { let argumentLength = arguments.length; - argumentLengthError(argumentLength, 1); + argumentLengthError(argumentLength, 2); + typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); typeError(typeof startingIndex, 'number'); - typeError(typeof amountToDelete, 'number'); - Map.prototype.splice.apply(firstArray, secondArray); + if(amountToDelete) typeError(typeof amountToDelete, 'number'); + Array.prototype.splice.call(arr, startingIndex, amountToDelete); } From 5c8119a7d4562cf7d7b6049a520830379cdeb063 Mon Sep 17 00:00:00 2001 From: Edwin Del Rio Date: Tue, 18 Jul 2017 16:58:38 -0700 Subject: [PATCH 06/10] Wrote test for map, filter and reduce --- lab-eddie/lib/fp.js | 65 ++++++++++++++++++++------------------- lab-eddie/package.json | 2 +- lab-eddie/test/fp-test.js | 56 +++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 33 deletions(-) create mode 100644 lab-eddie/test/fp-test.js diff --git a/lab-eddie/lib/fp.js b/lab-eddie/lib/fp.js index 929fd29..0bc416f 100644 --- a/lab-eddie/lib/fp.js +++ b/lab-eddie/lib/fp.js @@ -1,51 +1,52 @@ 'use strict'; +module.exports = exports = {}; -const argumentLengthError = (argLength, expectedLength) => { - if(argLength < expectedLength) throw new Error(`Function requires ${expectedLength} arguments. Only ${argLength} provided.`); +exports.argumentLengthError = (argLength, expectedLength) => { + if(argLength < expectedLength) throw new Error(`exports.requires ${expectedLength} arguments. Only ${argLength} provided.`); }; -const typeError = (objType, expectedType) => { - if(objType !== expectedType) throw new Error(`Function expected ${expectedType} type parameter but recieved ${objType} instead.`); +exports.typeError = (objType, expectedType) => { + if(objType !== expectedType) throw new Error(`exports.expected ${expectedType} type parameter but recieved ${objType} instead.`); }; -function map(arr, callback) { +exports.map = function(arr, callback) { let argumentLength = arguments.length; - argumentLengthError(argumentLength, 2); - typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); - typeError(typeof callback, 'function'); + this.argumentLengthError(argumentLength, 2); + this.typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + this.typeError(typeof callback, 'function'); return Array.prototype.map.call(arr, callback); -} +}; -function filter(arr, callback) { +exports.filter = function(arr, callback) { let argumentLength = arguments.length; - argumentLengthError(argumentLength, 2); - typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); - typeError(typeof callback, 'function'); + this.argumentLengthError(argumentLength, 2); + this.typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + this.typeError(typeof callback, 'function'); return Array.prototype.filter.call(arr, callback); -} +}; -function reduce(arr, callback, counter=arr[0]) { +exports.reduce = function(arr, callback, counter=arr[0]) { let argumentLength = arguments.length; - argumentLengthError(argumentLength, 3); - typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); - typeError(typeof callback, 'function'); + this.argumentLengthError(argumentLength, 3); + this.typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + this.typeError(typeof callback, 'function'); return Array.prototype.reduce.call(arr, callback, counter); -} +}; -function concat(firstArray, secondArray) { +exports.concat = function(firstArray, secondArray) { let argumentLength = arguments.length; - argumentLengthError(argumentLength, 2); - typeError(Array.isArray(firstArray) ? 'array' : 'not an array', 'array'); - typeError(Array.isArray(secondArray) ? 'array' : 'not an array', 'array'); - Array.prototype.concat.apply(firstArray, secondArray); -} + this.argumentLengthError(argumentLength, 2); + this.typeError(Array.isArray(firstArray) ? 'array' : 'not an array', 'array'); + this.typeError(Array.isArray(secondArray) ? 'array' : 'not an array', 'array'); + return Array.prototype.concat.apply(firstArray, secondArray); +}, -function splice(arr, startingIndex, amountToDelete) { +exports.splice = function(arr, startingIndex, amountToDelete, ...items) { let argumentLength = arguments.length; - argumentLengthError(argumentLength, 2); - typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); - typeError(typeof startingIndex, 'number'); - if(amountToDelete) typeError(typeof amountToDelete, 'number'); - Array.prototype.splice.call(arr, startingIndex, amountToDelete); -} + this.argumentLengthError(argumentLength, 2); + this.typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + this.typeError(typeof startingIndex, 'number'); + if(amountToDelete) this.typeError(typeof amountToDelete, 'number'); + return Array.prototype.splice.call(arr, startingIndex, amountToDelete, ...items); +}; diff --git a/lab-eddie/package.json b/lab-eddie/package.json index b8b6df7..361f2b9 100644 --- a/lab-eddie/package.json +++ b/lab-eddie/package.json @@ -4,7 +4,7 @@ "description": "", "main": "index.js", "directories": { - "test": "test" + "test": "mocha" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" diff --git a/lab-eddie/test/fp-test.js b/lab-eddie/test/fp-test.js new file mode 100644 index 0000000..7e2821e --- /dev/null +++ b/lab-eddie/test/fp-test.js @@ -0,0 +1,56 @@ +'use strict'; + +const fp = require('../lib/fp.js'); +const expect = require('chai').expect; + +let testingArray = [1, 5, 7, 8]; +let secondTestArray = [9, 1, 1, 4]; + +describe('Test for fp module', () => { + + describe('#map', () => { + it('Should return a new modified array', () => { + let mappedArray = fp.map(testingArray, (val) => val + 1); + let secondMappedArray = fp.map(testingArray, (val) => Math.pow(val, 2)); + expect(mappedArray).to.deep.equal([2, 6, 8, 9]); + expect(secondMappedArray).to.deep.equal([1, 25, 49, 64]); + }); + it('Should throw an error when invalid arguements are entered', () => { + expect(fp.map()).to.throw(Error); + expect(fp.map([1, 2, 3])).to.throw(Error); + expect(fp.map([1, 2, 3], 'not a callback')).to.throw(Error); + expect(fp.map('not an array', (val) => 1 + val)).to.throw(Error); + }); + + }); + + describe('#filter', () => { + it('Should return a new filtered array', () => { + let filterArray = fp.filter(testingArray, (val) => val < 6); + let secondFilterArray = fp.filter(testingArray, (val) => val > 5); + expect(filterArray).to.deep.equal([1, 5]); + expect(secondFilterArray).to.deep.equal([7, 8]); + }); + it('Should throw an error when invalid arguements are entered', () => { + expect(fp.filter()).to.throw(Error); + expect(fp.filter([1, 2, 3])).to.throw(Error); + expect(fp.filter([1, 2, 3], 'not a callback')).to.throw(Error); + expect(fp.filter('not an array', (val) => val > 5)).to.throw(Error); + }); + + describe('#reduce', () => { + it('Should return a new reduceed array', () => { + let reduceArray = fp.reduce(testingArray, (count, val) => count + val, 0); + let secondReduceArray = fp.reduce(testingArray, (count, val) => { + count.push(val + 1); + return count; + }, [1]); + expect(reduceArray).to.equal(21); + expect(secondReduceArray).to.deep.equal([1, 2, 6, 8, 9]); + }); + it('Should throw an error when invalid arguements are entered', () => { + expect(fp.reduce()).to.throw(Error); + }); + }); + }); +}); From 4306584889e747744256d22b9569304ea62c2293 Mon Sep 17 00:00:00 2001 From: Edwin Del Rio Date: Tue, 18 Jul 2017 21:57:06 -0700 Subject: [PATCH 07/10] Wrote and successfully passed tests for stand alone methods w/ appropriate error handling --- lab-eddie/lib/fp.js | 21 ++++++---- lab-eddie/test/fp-test.js | 86 ++++++++++++++++++++++++++++----------- 2 files changed, 75 insertions(+), 32 deletions(-) diff --git a/lab-eddie/lib/fp.js b/lab-eddie/lib/fp.js index 0bc416f..c57ea2f 100644 --- a/lab-eddie/lib/fp.js +++ b/lab-eddie/lib/fp.js @@ -13,7 +13,7 @@ exports.typeError = (objType, expectedType) => { exports.map = function(arr, callback) { let argumentLength = arguments.length; this.argumentLengthError(argumentLength, 2); - this.typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + this.typeError(Array.isArray(arr) ? 'array' : typeof arr, 'array'); this.typeError(typeof callback, 'function'); return Array.prototype.map.call(arr, callback); }; @@ -21,31 +21,34 @@ exports.map = function(arr, callback) { exports.filter = function(arr, callback) { let argumentLength = arguments.length; this.argumentLengthError(argumentLength, 2); - this.typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + this.typeError(Array.isArray(arr) ? 'array' : typeof arr, 'array'); this.typeError(typeof callback, 'function'); return Array.prototype.filter.call(arr, callback); }; -exports.reduce = function(arr, callback, counter=arr[0]) { +exports.reduce = function(arr, callback, count) { + let argumentLength = arguments.length; - this.argumentLengthError(argumentLength, 3); - this.typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + this.argumentLengthError(argumentLength, 2); + if(count === undefined) count = arr[0]; + + this.typeError(Array.isArray(arr) ? 'array' : typeof arr, 'array'); this.typeError(typeof callback, 'function'); - return Array.prototype.reduce.call(arr, callback, counter); + return Array.prototype.reduce.call(arr, callback, count); }; exports.concat = function(firstArray, secondArray) { let argumentLength = arguments.length; this.argumentLengthError(argumentLength, 2); - this.typeError(Array.isArray(firstArray) ? 'array' : 'not an array', 'array'); - this.typeError(Array.isArray(secondArray) ? 'array' : 'not an array', 'array'); + this.typeError(Array.isArray(firstArray) ? 'array' : typeof firstArray, 'array'); + this.typeError(Array.isArray(secondArray) ? 'array' : typeof secondArray, 'array'); return Array.prototype.concat.apply(firstArray, secondArray); }, exports.splice = function(arr, startingIndex, amountToDelete, ...items) { let argumentLength = arguments.length; this.argumentLengthError(argumentLength, 2); - this.typeError(Array.isArray(arr) ? 'array' : 'not an array', 'array'); + this.typeError(Array.isArray(arr) ? 'array' : typeof arr, 'array'); this.typeError(typeof startingIndex, 'number'); if(amountToDelete) this.typeError(typeof amountToDelete, 'number'); return Array.prototype.splice.call(arr, startingIndex, amountToDelete, ...items); diff --git a/lab-eddie/test/fp-test.js b/lab-eddie/test/fp-test.js index 7e2821e..dc0f8a6 100644 --- a/lab-eddie/test/fp-test.js +++ b/lab-eddie/test/fp-test.js @@ -9,48 +9,88 @@ let secondTestArray = [9, 1, 1, 4]; describe('Test for fp module', () => { describe('#map', () => { + it('Should return a new modified array', () => { let mappedArray = fp.map(testingArray, (val) => val + 1); let secondMappedArray = fp.map(testingArray, (val) => Math.pow(val, 2)); expect(mappedArray).to.deep.equal([2, 6, 8, 9]); expect(secondMappedArray).to.deep.equal([1, 25, 49, 64]); }); + it('Should throw an error when invalid arguements are entered', () => { - expect(fp.map()).to.throw(Error); - expect(fp.map([1, 2, 3])).to.throw(Error); - expect(fp.map([1, 2, 3], 'not a callback')).to.throw(Error); - expect(fp.map('not an array', (val) => 1 + val)).to.throw(Error); + expect(fp.map.bind(fp)).to.throw(Error); + expect(fp.map.bind(fp, [1, 2, 3, 4])).to.throw(Error); + expect(fp.map.bind(fp, [1, 2, 3], 'not a callback')).to.throw(Error); + expect(fp.map.bind(fp, 'not an array', (val) => 1 + val)).to.throw(Error); }); }); describe('#filter', () => { + it('Should return a new filtered array', () => { let filterArray = fp.filter(testingArray, (val) => val < 6); let secondFilterArray = fp.filter(testingArray, (val) => val > 5); expect(filterArray).to.deep.equal([1, 5]); expect(secondFilterArray).to.deep.equal([7, 8]); }); + + it('Should throw an error when invalid arguements are entered', () => { + expect(fp.filter.bind(fp)).to.throw(Error); + expect(fp.filter.bind(fp, [1, 2, 3, 4])).to.throw(Error); + expect(fp.filter.bind(fp, [1, 2, 3], 'not a callback')).to.throw(Error); + expect(fp.filter.bind(fp, 'not an array', (val) => 1 + val)).to.throw(Error); + }); + }) + + describe('#reduce', () => { + + it('Should return a reduced array', () => { + let reduceArray = fp.reduce(testingArray, (count, val) => { + count += val; + return count; + }, 0); + let secondReduceArray = fp.reduce(testingArray, (count, val) => { + count.push(val + 1); + return count; + }, [1]); + expect(reduceArray).to.equal(21); + expect(secondReduceArray).to.deep.equal([1, 2, 6, 8, 9]); + }); + + it('Should throw an error when invalid arguements are entered', () => { + expect(fp.reduce.bind(fp)).to.throw(Error); + expect(fp.reduce.bind(fp, 1)).to.throw(Error); + expect(fp.reduce.bind(fp, [], 1)).to.throw(Error); + expect(fp.reduce.bind(fp, 'a', (val) => val)).to.throw(Error); + }); + }); + + describe('#concat', () => { + + it('Should return a new concated array', () => { + expect(fp.concat(testingArray, secondTestArray)) + .to.deep.equal([1, 5, 7, 8, 9, 1, 1, 4]); + }); + + it('Should throw an error when invalid arguements are entered', () => { + expect(fp.concat.bind(fp)).to.throw(Error); + expect(fp.concat.bind(fp, [], 'a')).to.throw(Error); + expect(fp.concat.bind(fp, 'a', [])).to.throw(Error); + }); + }); + describe('#splice', () => { + + it('Should return a new spliceed array', () => { + expect(fp.splice(testingArray, 0, 2)).to.deep.equal([1, 5]); + expect(fp.splice(testingArray, 0, 1, 4, 5, 6, 7)).to.deep.equal([7]); + expect(testingArray).to.deep.equal([4, 5, 6, 7, 8]) + }); + it('Should throw an error when invalid arguements are entered', () => { - expect(fp.filter()).to.throw(Error); - expect(fp.filter([1, 2, 3])).to.throw(Error); - expect(fp.filter([1, 2, 3], 'not a callback')).to.throw(Error); - expect(fp.filter('not an array', (val) => val > 5)).to.throw(Error); - }); - - describe('#reduce', () => { - it('Should return a new reduceed array', () => { - let reduceArray = fp.reduce(testingArray, (count, val) => count + val, 0); - let secondReduceArray = fp.reduce(testingArray, (count, val) => { - count.push(val + 1); - return count; - }, [1]); - expect(reduceArray).to.equal(21); - expect(secondReduceArray).to.deep.equal([1, 2, 6, 8, 9]); - }); - it('Should throw an error when invalid arguements are entered', () => { - expect(fp.reduce()).to.throw(Error); - }); + expect(fp.splice.bind(fp)).to.throw(Error); + expect(fp.splice.bind(fp, 'a', 1)).to.throw(Error); + expect(fp.splice.bind(fp, 3, 'a')).to.throw(Error); }); }); }); From bc0ca421c4c949b8636bc2b3b2d3a41c983f326f Mon Sep 17 00:00:00 2001 From: Edwin Del Rio Date: Tue, 18 Jul 2017 22:18:58 -0700 Subject: [PATCH 08/10] Added eslint file --- lab-eddie/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lab-eddie/package.json b/lab-eddie/package.json index 361f2b9..14417ca 100644 --- a/lab-eddie/package.json +++ b/lab-eddie/package.json @@ -7,7 +7,8 @@ "test": "mocha" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha", + "lint": "eslint" }, "keywords": [], "author": "", From 7f2f1d5ce8b1af7f59c60888918b82c26aae8548 Mon Sep 17 00:00:00 2001 From: Edwin Del Rio Date: Wed, 19 Jul 2017 11:51:54 -0700 Subject: [PATCH 09/10] Added isArray method to exports method --- lab-eddie/lib/fp.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lab-eddie/lib/fp.js b/lab-eddie/lib/fp.js index c57ea2f..4ae243b 100644 --- a/lab-eddie/lib/fp.js +++ b/lab-eddie/lib/fp.js @@ -9,11 +9,13 @@ exports.typeError = (objType, expectedType) => { if(objType !== expectedType) throw new Error(`exports.expected ${expectedType} type parameter but recieved ${objType} instead.`); }; +exports.isArray = (arr) => Array.isArray(arr) ? 'array' : typeof arr; + exports.map = function(arr, callback) { let argumentLength = arguments.length; this.argumentLengthError(argumentLength, 2); - this.typeError(Array.isArray(arr) ? 'array' : typeof arr, 'array'); + this.typeError(this.isArray(arr), 'array'); this.typeError(typeof callback, 'function'); return Array.prototype.map.call(arr, callback); }; @@ -21,7 +23,7 @@ exports.map = function(arr, callback) { exports.filter = function(arr, callback) { let argumentLength = arguments.length; this.argumentLengthError(argumentLength, 2); - this.typeError(Array.isArray(arr) ? 'array' : typeof arr, 'array'); + this.typeError(this.isArray(arr), 'array'); this.typeError(typeof callback, 'function'); return Array.prototype.filter.call(arr, callback); }; @@ -32,7 +34,7 @@ exports.reduce = function(arr, callback, count) { this.argumentLengthError(argumentLength, 2); if(count === undefined) count = arr[0]; - this.typeError(Array.isArray(arr) ? 'array' : typeof arr, 'array'); + this.typeError(this.isArray(arr), 'array'); this.typeError(typeof callback, 'function'); return Array.prototype.reduce.call(arr, callback, count); }; @@ -40,15 +42,15 @@ exports.reduce = function(arr, callback, count) { exports.concat = function(firstArray, secondArray) { let argumentLength = arguments.length; this.argumentLengthError(argumentLength, 2); - this.typeError(Array.isArray(firstArray) ? 'array' : typeof firstArray, 'array'); - this.typeError(Array.isArray(secondArray) ? 'array' : typeof secondArray, 'array'); + this.typeError(this.isArray(firstArray), 'array'); + this.typeError(this.isArray(secondArray), 'array'); return Array.prototype.concat.apply(firstArray, secondArray); }, exports.splice = function(arr, startingIndex, amountToDelete, ...items) { let argumentLength = arguments.length; this.argumentLengthError(argumentLength, 2); - this.typeError(Array.isArray(arr) ? 'array' : typeof arr, 'array'); + this.typeError(this.isArray(arr), 'array'); this.typeError(typeof startingIndex, 'number'); if(amountToDelete) this.typeError(typeof amountToDelete, 'number'); return Array.prototype.splice.call(arr, startingIndex, amountToDelete, ...items); From 08e4b217d853de68c237a43ecd0b93d4128d0f60 Mon Sep 17 00:00:00 2001 From: Edwin Del Rio Date: Wed, 19 Jul 2017 12:55:57 -0700 Subject: [PATCH 10/10] Added eslintrc file --- .eslintrc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .eslintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..8dc6807 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,21 @@ +{ + "rules": { + "no-console": "off", + "indent": [ "error", 2 ], + "quotes": [ "error", "single" ], + "semi": ["error", "always"], + "linebreak-style": [ "error", "unix" ] + }, + "env": { + "es6": true, + "node": true, + "mocha": true, + "jasmine": true + }, + "ecmaFeatures": { + "modules": true, + "experimentalObjectRestSpread": true, + "impliedStrict": true + }, + "extends": "eslint:recommended" +}