Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
Created July 21, 2020 19:13
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 mlhaufe/61f76f1f8b0bddaf341b18f3b7ef03e8 to your computer and use it in GitHub Desktop.
Save mlhaufe/61f76f1f8b0bddaf341b18f3b7ef03e8 to your computer and use it in GitHub Desktop.
Boolean control structures
interface Boolean {
ifTrue<T>(fn: () => T): T | undefined
ifFalse<T>(fn: () => T): T | undefined
}
Object.assign(Boolean.prototype, {
ifTrue(fn: Function) {
return this ? fn.apply(this) : undefined
},
ifFalse(fn: Function) {
return this ? undefined : fn.apply(this)
}
});
let a = 4, b = 3;
console.log(
(a > b).ifTrue(() => a - b)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment