Skip to content

Instantly share code, notes, and snippets.

@rainstormza
Forked from dsheiko/accept.js
Created September 19, 2018 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rainstormza/9bc463ee310409e5121492230018c497 to your computer and use it in GitHub Desktop.
Save rainstormza/9bc463ee310409e5121492230018c497 to your computer and use it in GitHub Desktop.
/**
* 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