one of those moments of programming eloquence you hope for, discovered to be unneeded immediately upon finishing it
the final code:
handleWordClick = (word, event) => {
let isShiftClick = (this.state.shiftActive);
let [ hasClickFn , hasShiftClickFn ] = [ 'onClick', 'onShiftClick' ].map((p) => (typeof this.props[p] === 'function'));
if(isShiftClick && hasShiftClickFn) {
return this.props.onShiftClick(event, word);
} else if(hasClickFn) {
return this.props.onClick(event, word);
}
}
why it's eloquent:
the straightforward way to write that would be:
handleWordClick = (word, event) => {
let isShiftClick = (this.state.shiftActive);
let [ hasClickFn , hasShiftClickFn ] = [ 'onClick', 'onShiftClick' ].map((p) => (typeof this.props[p] === 'function'));
if(isShiftClick && hasShiftClickFn) {
return this.props.onShiftClick(event, word);
else if(isShiftClick & hasClickFn) {
return this.props.onClick(event, word);
} else if(hasClickFn) {
return this.props.onClick(event, word);
}
}
the eloquence comes in when you notice that the second & third branches are identical, and that the third case is sufficient to capture both scenarios and separate them from the predictably bad scenario (throw when attempting to invoke a method that wasn't provided) given the details of the first test