Skip to content

Commit b8ed7c4

Browse files
committed
Add recursive composition
1 parent 6263462 commit b8ed7c4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

JavaScript/6-recursive.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const compose = (...fns) => (...args) => {
4+
if (fns.length === 0) return args[0];
5+
const fn = fns.shift();
6+
const res = fn(...args);
7+
if (fns.length === 0) return res;
8+
return compose(...fns)(res);
9+
};
10+
11+
// Usage
12+
13+
const upperCapital = s => s.replace(
14+
/\w+/g,
15+
word => word.charAt(0).toUpperCase() + word.substr(1)
16+
);
17+
18+
const lower = s => (typeof s === 'string' ? s.toLowerCase() : '');
19+
20+
const trim = s => s.trim();
21+
22+
const s = ' MARCUS AURELIUS ';
23+
console.log(s);
24+
console.log('lower(' + s + ') = ' + lower(s));
25+
console.log('upperCapital(' + s + ') = ' + upperCapital(s));
26+
27+
const capitalize = compose(trim, lower, upperCapital);
28+
console.log('capitalize(' + s + ') = ' + capitalize(s));

0 commit comments

Comments
 (0)