lodown-awinste
v1.0.2
Published
An npm functional programming library project
Downloads
3
Readme
lodown
An npm functional programming library project
In this project, you're going to create your own functional library of code and release this code for use through the npm
(Node Package Management) platform.
You'll then use your lodown library to solve problems in the next project.
Setup
Fork this repository to your own GitHub account.
Clone your forked version of the lodown repository to a new Cloud9 workspace.
We'll need a
package.json
file, which describes fornpm
our library project and lists any dependant libraries of code. To create apackage.json
configuration file for our lodown library, from the command-line, run the command:npm init
IMPORTANT CONFIGURATION: Fill in the prompts with your own details:
When asked to name your lodown project, give it a unique name by appending your GitHub username to the name of the project (of course, replacing
<my-github-username>
with your actual GitHub username.), like so:lodown-<my-github-username>
When asked for test command, enter:
istanbul cover _mocha -- test/ -R spec
When asked for your git repository, you will probably be given the correct url as the default, so just select this by pressing
return
, otherwise, provide your forked GitHub repository's url.
Once you're done creating your
package.json
file, go to the command line and install the required test libraries by running the following command:npm install i -D mocha chai sinon istanbul
Implement your library in the file
index.js
. Remember thatindex.js
is a Node.js module, so you must export your API usingmodule.exports
, for example:function each(collection, action) { // ...code } module.exports.each = each;
Write clear docs for each Function using the proper JavaScript documentation format. You should clearly explain how each Function works, the type of each paramater and how each paramater is used, and explain any return value. It should be evident from your docs you clearly understand how each Function works. Bonus points for listing usage examples in your docs. Example:
/** * filter: Designed to filter values in a collection based on a test. * Takes a collection, Array or Object, and passes each value * in the collection through a test Function. The test Function returns * true if the value passes the test, false otherwise. Values that pass * the test are collected and returned in an output Array. * * @param {Array or Object} collection: The collection to filter. * @param {Function} test: The Function to be applied to each value in * the collection. The test Function must return a Boolean based on some * logic which tests the value given to it. * * @return {Array}: An Array containing the filtered collection values. * The Array will contain only the values that passed the test. * * Usage: * * const letters = ['a', 'b', 'b', 'c']; * const onlyBs = filter(letters, function(letter) { * return letter === 'b'; * }); * console.log(onlyBs); // -> ['b', 'b'] */ function filter(collection, test) { // ...code } module.exports.each = each;
Once you're done with all Functions and their docs, release your lodown library to
npm
(node package manager), following the steps in this video to do so (using your GitHub username to create a unique name for your library):publishing-npm-packages
Optional: Unit tests
If you want to learn about and create unit tests for your code, write your tests in the file test/index.spec.js
. Check out the chai api for examples on asserting expectations and be careful when asserting/expecting against complex types - you'll have to make use of the .eql
api, and not .equal
. For example:
expect(lodown.filter([1, 2, 3], function(value) { return value > 2; } )).to.eql([3]);
To run your tests, run the following command:
npm test
Check the console output for passes and failures.
Preview (Cloud9) or open the file at
coverage/lcov-report/index.html
for your coverage report. Make certain all lines of code are traversed in your tests!
Resources:
Docs
- https://mochajs.org
- http://chaijs.com/api/bdd/
- http://sinonjs.org/docs/#spies-api
Reading
- http://marcofranssen.nl/using-mocha-chai-sinon-to-test-node-js/
- http://stackoverflow.com/questions/19298118/what-is-the-role-of-describe-in-mocha
- http://www.vapidspace.com/coding/2014/10/29/code-coverage-metrics-with-mocha-and-istanbul/
Quick Videos
- https://egghead.io/lessons/javascript-how-to-write-a-javascript-library-setting-up-unit-testing-with-mocha-and-chai
- https://egghead.io/lessons/javascript-how-to-write-a-javascript-library-unit-testing-with-mocha-and-chai