File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 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 ) ) ;
You can’t perform that action at this time.
0 commit comments