Skip to content

Commit 6d0220d

Browse files
committed
Add abstract wrap with before and after callbacks
1 parent 607a788 commit 6d0220d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

JavaScript/2-before-after.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const wrap = (before, after, fn) => (
4+
(...args) => after(fn(...before(...args)))
5+
);
6+
7+
// Usage
8+
9+
const func = (par1, par2) => {
10+
console.dir({ method: { par1, par2 } });
11+
return [par1, par2];
12+
};
13+
14+
const before = (...args) => {
15+
console.log('before');
16+
return args;
17+
};
18+
19+
const after = (...args) => {
20+
console.log('after');
21+
return args;
22+
};
23+
24+
const cloned = wrap(before, after, func);
25+
cloned('Uno', 'Due');

0 commit comments

Comments
 (0)