File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- const each = ( arr , cb ) => arr . forEach ( ( v , i ) => cb ( v , i ) ) ;
1+ const each = ( arr , cb ) => {
2+ for ( let i = 0 ; i < arr . length ; i ++ ) {
3+ cb ( arr [ i ] , i ) ;
4+ }
5+ } ;
26
37const map = ( arr , cb ) => {
48 const output = [ ] ;
@@ -23,10 +27,7 @@ const filter = (arr, cb) => {
2327 return output ;
2428} ;
2529
26- const flatten = arr => arr . reduce ( ( a , c ) => {
27- if ( Array . isArray ( c ) ) return a . concat ( flatten ( c ) ) ;
28- return a . concat ( c ) ;
29- } , [ ] ) ;
30+ const flatten = arr => arr . reduce ( ( a , c ) => ( Array . isArray ( c ) ? a . concat ( flatten ( c ) ) : a . concat ( c ) ) , [ ] ) ;
3031
3132/* eslint-enable no-unused-vars, max-len */
3233
Original file line number Diff line number Diff line change @@ -13,20 +13,12 @@ const counterFactory = () => {
1313
1414const limitFunctionCallCount = ( cb , n ) => {
1515 let count = 0 ;
16- return ( ...args ) => {
17- if ( count === n ) return null ;
18- count ++ ;
19- return cb ( ...args ) ;
20- } ;
16+ return ( ...args ) => ( count === n ? null : ++ count && cb ( ...args ) ) ;
2117} ;
2218
2319const cacheFunction = ( cb ) => {
2420 const cache = { } ;
25- return ( arg ) => {
26- if ( arg in cache ) return cache [ arg ] ;
27- cache [ arg ] = cb ( arg ) ;
28- return cache [ arg ] ;
29- } ;
21+ return arg => ( arg in cache ? cache [ arg ] : cache [ arg ] = cb ( arg ) ) ;
3022} ;
3123
3224/* eslint-enable no-unused-vars */
Original file line number Diff line number Diff line change @@ -2,30 +2,26 @@ const keys = obj => Object.keys(obj);
22
33const values = obj => Object . values ( obj ) ;
44
5- const mapObject = ( obj , cb ) => {
6- return Object . keys ( obj ) . reduce ( ( a , c ) => {
7- a [ c ] = cb ( obj [ c ] ) ;
8- return a ;
9- } , { } ) ;
10- } ;
5+ const mapObject = ( obj , cb ) => Object . keys ( obj ) . reduce ( ( a , c ) => {
6+ a [ c ] = cb ( obj [ c ] ) ;
7+ return a ;
8+ } , { } ) ;
119
1210const pairs = obj => Object . entries ( obj ) ;
1311
14- const invert = ( obj ) => {
15- return Object . entries ( obj ) . reduce ( ( a , c ) => {
16- const [ k , v ] = c ;
17- a [ v ] = k ;
18- return a ;
19- } , { } ) ;
20- } ;
12+ const invert = obj => Object . entries ( obj ) . reduce ( ( a , c ) => {
13+ const [ k , v ] = c ;
14+ a [ v ] = k ;
15+ return a ;
16+ } , { } ) ;
17+
2118
2219const defaults = ( obj , defaultProps ) => {
2320 const x = Object . entries ( obj ) ;
2421 const y = Object . entries ( defaultProps ) ;
2522 return [ ...x , ...y ] . reduce ( ( a , c ) => {
2623 const [ k , v ] = c ;
27- if ( k in a ) return a ;
28- a [ k ] = v ;
24+ if ( ! ( k in a ) ) a [ k ] = v ;
2925 return a ;
3026 } , { } ) ;
3127} ;
You can’t perform that action at this time.
0 commit comments