Implementing a functional library for working with arrays
- Code style
- No bundling and modules
- The library should provide the following functionality:
- take(array, n)
- skip(array, n)
- map(array, callback)
- reduce(array, callback, initialValue)
- filter(array, callback)
- foreach(array, callback) Here is an example of calling: nameOfYourLibrary.take([1, 2, 3, 4], 2) will return [1, 2] The rest of the functions work the same way. nameOfYourLibrary.map([1, 2, 3], a => a * 2 ) will return [2, 4, 6]
- After finishing the implementation of these methods, you should implement new chain and value functions, which will allow using the library in the following way: arrayLib.chain([1, 2, 3]).take(2).skip(1).value() will return [2]
- NOTE: it should still be possible to call the function in the old way. First, you should implement the library using ES5, after completing the chain and value functions, the same task should be implemented with ES6.
- Cover the code with unit tests.
- Implement memoization function, for example, sum.
I used Jest testing framework for unit tests.
You can run unit tests from command line (npm install jest --global
) and then just run: jest
. File jest.config.js is required in order to run jest through CLI.