diff --git a/Exercises/1-pipe.js b/Exercises/1-pipe.js index d09882a..ea0858f 100644 --- a/Exercises/1-pipe.js +++ b/Exercises/1-pipe.js @@ -1,5 +1,17 @@ 'use strict'; -const pipe = (...fns) => x => null; +const pipe = (...fns) => { + fns.forEach(itm => { + if (typeof itm !== 'function') { + throw new Error('Functions\'s args must be a function'); + } + }); + return x => { + fns.forEach(fn => { + x = fn(x); + }); + return x; + }; +}; module.exports = { pipe }; diff --git a/Exercises/2-compose.js b/Exercises/2-compose.js index 368e521..be525db 100644 --- a/Exercises/2-compose.js +++ b/Exercises/2-compose.js @@ -1,5 +1,35 @@ 'use strict'; -const compose = (...fns) => x => null; +const compose = (...fns) => { + + const events = {}; + const fnComp = function (x) { + try { + fns.reverse().forEach(fn => { + x = fn(x); + }); + return x; + } catch (e) { + fnComp.emit('error', e); + return; + } + }; + + fnComp.on = function (name, fn) { + const event = events[name]; + if (event) { + event.push(fn); + } else { + events[name] = [fn]; + } + }; + + fnComp.emit = function (name, ...data) { + const event = events[name]; + if (event) event.forEach(fn => fn(...data)); + }; + + return fnComp; +}; module.exports = { compose };
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: