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 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" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..345130c --- /dev/null +++ b/.gitignore @@ -0,0 +1,136 @@ +# Created by https://www.gitignore.io/api/osx,vim,node,macos,windows + +### macOS ### +*.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 + +### 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 ### + +# Icon must end with two \r + +# Thumbnails + +# Files that might appear in the root of a volume + +# Directories potentially created on remote AFP share + +### 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,macos,windows diff --git a/README.md b/README.md index 67e3b00..c14f211 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,7 @@ -![cf](https://i.imgur.com/7v5ASc8.png) 02: Tools and Context -====== - -## To Submit this Assignment - * fork this repository & create a new branch for your work - * write all of your code in a directory named `lab-` + `` **e.g.** `lab-susan` - * push to your repository - * submit a pull request to this repository - * submit a link to your PR in canvas - * write a question and observation on canvas - -## Directions - * create a `/lib` and `/test` directory - * add a robust `.gitignore` file - * include a `package.json` - * include the provided `.eslintrc` file - * create a `lint` script for running eslint - * create a `test` script for running mocha - * create a `lib/fp.js` file for holding the following: - * create stand alone `map`, `filter`, `reduce`, `concat`, and `splice` functions using the `call`, and `apply` methods we discussed today - * you are welcome to use/experiment with `bind` too! - * *note:* - all functions should be created using the es6 lexical arrow function syntax - -# Tests - * create a test for each of your custom functions in `lib/fp.js` - * note - feel free to create additional tests, if you choose! - -## Bonus 2pts - * create a CLI (`index.js`) that will use your new `map` function to uppercase all command line arguments and print them to the screen: 1pt - * **hint:** - you'll want to check out the `process` object (and it's methods) to get an array of provided command line arguments - * test your CLI: 1pt +## Lab-James Lab-02 Documentation + * index.js calls function 'main' function which returns CLI parameters as uppercase words using the fp.splice method from ./lib/fp.js. + * ./lib/fp.js defines the map, filter, reduce, concat, and splice methods using both call and apply. + * Tests + * ./test/fp-test.js tests the above methods + * **Stretch Goal:** ./test/index-test.js tests the index.js CLI functionality. + * Project passes Mocha (chai) tests and esLint. diff --git a/index.js b/index.js new file mode 100644 index 0000000..2a15f14 --- /dev/null +++ b/index.js @@ -0,0 +1,13 @@ +'use strict'; + +const fp = require('./lib/fp.js'); + +let main = module.exports = () => { + let list = fp.splice(process.argv, 2); + list = fp.map(list, (word) => word.toUpperCase()); + let result = list.join(' '); + console.log(result); + return result; +}; + +main(); diff --git a/lib/fp.js b/lib/fp.js new file mode 100644 index 0000000..f2f9177 --- /dev/null +++ b/lib/fp.js @@ -0,0 +1,29 @@ +'use strict'; + +module.exports = exports = {}; + +exports.map = (list, ...args) => { + if (!list) throw new Error('array not provided'); + return Array.prototype.map.call(list, ...args); +}; + + +exports.filter = (arr, callback) => { + if (!arr) throw new Error('array not provided'); + return Array.prototype.filter.call(arr, callback); +}; + +exports.concat = (arr1, arr2) => { + if (!arr1) throw new Error('initial array not provided'); + return Array.prototype.concat.apply(arr1, arr2); +}; + +exports.reduce = (arr, idx, callback) => { + if (!arr) throw new Error('array not provided'); + return Array.prototype.reduce.call(arr, idx, callback); +}; + +exports.splice = (list, ...args) => { + if (!list) throw new Error('array not provided'); + return Array.prototype.splice.call(list, ...args); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..7d62c3a --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "02-tools_and_context", + "version": "1.0.0", + "description": "![cf](https://i.imgur.com/7v5ASc8.png) 02: Tools and Context ======", + "main": "index.js", + "directories": { + "test": "test" + }, + "scripts": { + "start": "node index.js", + "test": "mocha", + "lint": "eslint" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/codefellows-javascript-401d17/02-tools_and_context.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/codefellows-javascript-401d17/02-tools_and_context/issues" + }, + "homepage": "https://github.com/codefellows-javascript-401d17/02-tools_and_context#readme", + "devDependencies": { + "chai": "^4.1.0", + "expect": "^1.20.2", + "mocha": "^3.4.2" + } +} diff --git a/test/test-fp.js b/test/test-fp.js new file mode 100644 index 0000000..bde0986 --- /dev/null +++ b/test/test-fp.js @@ -0,0 +1,70 @@ +'use strict'; + +const fp = require('../lib/fp.js'); +const expect = require('chai').expect; + +describe('fp', () => { + describe('#map', () => { + it('should throw array not provided error', () => { + let mapArrErr = fp.map; + expect(mapArrErr).to.throw(Error); + }); + it('should return an array with doubled numbers', () => { + let validMap = fp.map([1,2,3,4], (n) => { return n * 2; }); + expect(validMap).to.be.an('array').that.includes.members([2,4,6,8]); + }); + }); + describe('#filter', () =>{ + it('should throw array not provided error', () => { + let filterArrErr = fp.filter; + expect(filterArrErr).to.throw(Error); + }); + it('should return an array that is missing the number 2', () => { + let validFilter = fp.filter([1, 2, 3, 4], (n) => { return n !== 2; }); + expect(validFilter).to.be.an('array').to.not.have.members([2]); + }); + }); + describe('#concat', () => { + it('should throw an intial array not provided error', () => { + let concatArrErr = fp.concat; + expect(concatArrErr).to.throw(Error); + }); + it('should be an array the is a combination of two arrays', () => { + let validConcat = fp.concat([1,2,3], [4,5,6]); + expect(validConcat).to.be.an('array').to.include.members([1,2,3,4,5,6]); + }); + }); + describe('#reduce', () =>{ + it('should throw an intial array not provided error', () => { + let reduceArrErr = fp.concat; + expect(reduceArrErr).to.throw(Error); + }); + it('should reduce an array down to a single value', () => { + let validReduce = fp.reduce([0, 1, 2, 3], (acc, n) => {return acc + n; }, 0); + expect(validReduce).to.equal(6); + }); + it('should count amount of each name in an array and return it in an object', () => { + var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']; + var validNameReduce = fp.reduce(names, (allNames, name) => { + if (name in allNames) { + allNames[name]++; + } + else { + allNames[name] = 1; + } + return allNames; + }, {}); + expect(validNameReduce).to.be.an('object'); + }); + }); + describe('#splice', () => { + it('should throw an intial array not provided error', () =>{ + let spliceArrErr = fp.concat; + expect(spliceArrErr).to.throw(Error); + }); + it('should return [6,7,8]', () => { + let result = fp.splice([1,2,3,4,5,6,7,8], 5); + expect(result).to.be.an('array').to.include.members([6,7,8]); + }); + }); +}); diff --git a/test/test-index.js b/test/test-index.js new file mode 100644 index 0000000..a2e5881 --- /dev/null +++ b/test/test-index.js @@ -0,0 +1,24 @@ +'use strict'; + +const expect = require('chai').expect; +const main = require('../index.js'); + +describe('main', () => {var oldArgv = process.argv; + before(() => { + process.argv = [ + 'node path', + 'file path', + 'cat', + 'dog', + ]; + }); + + after(() => { + process.argv = oldArgv; + }); + + it('should return \'CAT DOG\'', () => { + let result = main(); + expect(result).to.be.a('string').to.have.string('CAT DOG'); + }); +});