Created
January 5, 2017 18:59
-
-
Save dmsnell/c0403125f9e32e79b5c4d6396fb2550f to your computer and use it in GitHub Desktop.
Building independent reducers of inter-related data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const height = ( state = 8, { type, height } ) => | |
'SET_HEIGHT' === type || 'RESET_BOARD' === type | |
? height | |
: state; | |
const width = ( state = 8, { type, width } ) => | |
'SET_WIDTH' === type || 'RESET_BOARD' === type | |
? width | |
: state; | |
const grid = ( state = [], { type, height, width } ) => { | |
if ( 'SET_HEIGHT' === type ) { | |
return state.slice( 0, height ); | |
} | |
if ( 'SET_WIDTH' === type ) { | |
return state.map( row => row.slice( 0, width ) ); | |
} | |
if ( 'RESET_BOARD' === type ) { | |
return ( new Array( height ) ).map( row => new Array( width ) ); | |
} | |
return state; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment