Skip to content
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
14 changes: 13 additions & 1 deletion Exercises/1-pipe.js
Original file line number Diff line number Diff line change
@@ -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 };
32 changes: 31 additions & 1 deletion Exercises/2-compose.js
Original file line number Diff line number Diff line change
@@ -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 };