From ca86251f072ab723a9e26cd6360b77181be13110 Mon Sep 17 00:00:00 2001 From: beliy-a Date: Tue, 9 Mar 2021 20:42:31 +0300 Subject: [PATCH 1/3] change linebreak-style --- .eslintrc.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 9305d2768cba9d7d575f3b7f7197ef98d1740eca Mon Sep 17 00:00:00 2001 From: beliy-a Date: Tue, 9 Mar 2021 20:43:36 +0300 Subject: [PATCH 2/3] add solving for all tasks --- Exercises/1-pipe.js | 9 ++++++++- Exercises/2-compose.js | 26 +++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) 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..1a564c7 100644 --- a/Exercises/2-compose.js +++ b/Exercises/2-compose.js @@ -1,5 +1,29 @@ '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 = callback => { + errors.forEach(err => callback(err)); + }; + + return fn; +}; module.exports = { compose }; From 4ea6c7b71467316c24dad56dc3ad05d1516d849e Mon Sep 17 00:00:00 2001 From: beliy-a Date: Tue, 9 Mar 2021 20:54:15 +0300 Subject: [PATCH 3/3] change fn.on --- Exercises/2-compose.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Exercises/2-compose.js b/Exercises/2-compose.js index 1a564c7..2ebc340 100644 --- a/Exercises/2-compose.js +++ b/Exercises/2-compose.js @@ -19,8 +19,10 @@ const compose = (...fns) => { } }; - fn.on = callback => { - errors.forEach(err => callback(err)); + fn.on = (name, callback) => { + if (name === 'error') { + errors.push(callback); + } }; return fn;