Created
July 21, 2020 19:13
-
-
Save mlhaufe/61f76f1f8b0bddaf341b18f3b7ef03e8 to your computer and use it in GitHub Desktop.
Boolean control structures
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
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