diff --git a/.eslintrc.json b/.eslintrc.json index 70e7644..47b0939 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -15,7 +15,7 @@ ], "linebreak-style": [ "error", - "unix" + "windows" ], "quotes": [ "error", @@ -262,4 +262,4 @@ "never" ] } -} +} \ No newline at end of file diff --git a/Exercises/1-pipe.js b/Exercises/1-pipe.js index d09882a..5e2f5ef 100644 --- a/Exercises/1-pipe.js +++ b/Exercises/1-pipe.js @@ -1,5 +1,12 @@ 'use strict'; -const pipe = (...fns) => x => null; +const pipe = (...fns) => { + fns.forEach(fn => { + if (typeof fn !== 'function') throw Error('not a Function'); + }); + + return x => fns.reduce((acc, fn) => acc = fn(acc), x); +}; + module.exports = { pipe }; diff --git a/Exercises/2-compose.js b/Exercises/2-compose.js index 368e521..2ebc340 100644 --- a/Exercises/2-compose.js +++ b/Exercises/2-compose.js @@ -1,5 +1,31 @@ 'use strict'; -const compose = (...fns) => x => null; +const compose = (...fns) => { + const errors = []; + + const fn = x => { + if (fns.length === 0) return x; + + const start = fns.length - 1; + let res = x; + + try { + for (let i = start; i >= 0; i--) { + res = fns[i](res); + } + return res; + } catch (err) { + errors.forEach(e => e(err)); + } + }; + + fn.on = (name, callback) => { + if (name === 'error') { + errors.push(callback); + } + }; + + return fn; +}; module.exports = { compose };