diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/.DS_Store differ diff --git a/README.md b/README.md deleted file mode 100644 index 70bd927..0000000 --- a/README.md +++ /dev/null @@ -1,48 +0,0 @@ -![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/lab-pedja/.eslintignore b/lab-pedja/.eslintignore new file mode 100644 index 0000000..05b1cf3 --- /dev/null +++ b/lab-pedja/.eslintignore @@ -0,0 +1,5 @@ +**/node_modules/* +**/vendor/* +**/*.min.js +**/coverage/* +**/build/* diff --git a/lab-pedja/.eslintrc.json b/lab-pedja/.eslintrc.json new file mode 100644 index 0000000..840d336 --- /dev/null +++ b/lab-pedja/.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/lab-pedja/.gitignore b/lab-pedja/.gitignore new file mode 100644 index 0000000..8fe2634 --- /dev/null +++ b/lab-pedja/.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-pedja/README.md b/lab-pedja/README.md new file mode 100644 index 0000000..ff0f50b --- /dev/null +++ b/lab-pedja/README.md @@ -0,0 +1,17 @@ +![cf](https://i.imgur.com/7v5ASc8.png) 02: Lab Tools and Context +====== + +## Feature Tasks +#### 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`. + +* 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. + +* `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. + +* `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 new file mode 100644 index 0000000..f9d6d69 --- /dev/null +++ b/lab-pedja/__test__/fp.test.js @@ -0,0 +1,101 @@ +'use strict'; + +const fp = require('../lib/fp'); + +describe('fp.js', () => { + + // testing fp.map function + describe('fp.map', () => { + test('returns array with items multiplied with 2', () => { + expect(fp.map( + (num) => num * 2, + [0, 1, 2] + )).toEqual([0, 2, 4]); + }); + + test('exception will be thrown if error occurs', () => { + expect( + () => { + fp.map('I\'m not a function ()', [0, 1, 2]); + }).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(); + }); + }); + + // 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(); + }); + }); + + //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/index.js b/lab-pedja/index.js new file mode 100644 index 0000000..ad9a93a --- /dev/null +++ b/lab-pedja/index.js @@ -0,0 +1 @@ +'use strict'; diff --git a/lab-pedja/lib/fp.js b/lab-pedja/lib/fp.js new file mode 100644 index 0000000..39baf27 --- /dev/null +++ b/lab-pedja/lib/fp.js @@ -0,0 +1,33 @@ +'use strict'; + +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); +}; + +fp.slice = (begin, end, collection) => { + 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 new file mode 100644 index 0000000..9882812 --- /dev/null +++ b/lab-pedja/package.json @@ -0,0 +1,15 @@ +{ + "name": "lab_class_02", + "version": "1.0.0", + "description": "lab_class_02_tools", + "main": "index.js", + "scripts": { + "test": "jest -i --coverage", + "lint": "eslint fp.test.js" + }, + "author": "Pedja", + "license": "ISC", + "devDependencies": { + "jest": "^21.2.1" + } +}