Created
July 3, 2018 14:17
-
-
Save dsheiko/ab16afb39b516e81dc9292633d331086 to your computer and use it in GitHub Desktop.
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
/** | |
* Get round nested ternary operator | |
* | |
* <header>{ | |
* accept() | |
* .when( a == 1, <h1>Case 1</h1>) | |
* .when( a == 2, <h1>Case 2</h1>) | |
* .otherwise( <h1>Case 3</h1>) | |
* .render() | |
* }</header> | |
*/ | |
class Accept { | |
constructor() { | |
this.node = null; | |
} | |
/** | |
* Render reactNode if expression in `predicate` resolves in true | |
* @param {Boolean} predicate | |
* @param {React.ReactNode} reactNode | |
* @returns {Accept} | |
*/ | |
when( predicate, reactNode ) { | |
if ( predicate ) { | |
this.node = reactNode; | |
} | |
return this; | |
} | |
/** | |
* Render reactNode when not matches were found so far | |
* @param {React.ReactNode} reactNode | |
* @returns {Accept} | |
*/ | |
otherwise( reactNode ) { | |
if ( !this.node ) { | |
this.node = reactNode; | |
} | |
return this; | |
} | |
/** | |
* Resolve chain | |
* @returns {React.ReactNode} | |
*/ | |
render() { | |
return this.node; | |
} | |
} | |
export default function accept() { | |
return new Accept(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment