Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All tasks was done #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Exercises/
node_modules/
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
9 changes: 8 additions & 1 deletion Exercises/1-pipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
'use strict';

const pipe = (...fns) => (x) => null;
const pipe = (...fns) => {
fns.forEach((fn) => {
if (typeof fn !== 'function') {
throw new Error('Type of fn is not a function');
}
});
return (x) => fns.reduce((v, f) => f(v), x);
};

module.exports = { pipe };
22 changes: 21 additions & 1 deletion Exercises/2-compose.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
'use strict';

const compose = (...fns) => (x) => null;
const compose = (...fns) => {
const handlers = [];
const fn = (x) => {
if (fns.length === 0) return x;
const last = fns.length - 1;
let res = x;
try {
for (let i = last; i >= 0; i--) {
res = fns[i](res);
}
} catch (error) {
res = undefined;
handlers.forEach((handler) => handler(error));
}
return res;
};
fn.on = (name, handler) => {
if (name === 'error') handlers.push(handler);
};
return fn;
};

module.exports = { compose };