Last active
September 5, 2020 19:31
-
-
Save NullDev/5b7515558ed044f35eb3e4f6515a2026 to your computer and use it in GitHub Desktop.
A JavaScript implementation to allow expressions such as 3 > 2 > 1
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
/** | |
* This basically allows expressions like 3 > 2 > 1 | |
* which is usually written as 3 > 2 && 2 > 1 | |
* | |
* I basically did this because I wanted to see how easy it is | |
* to implement such "new syntax" in JS | |
* | |
* Please don't use this. It's hacky. | |
*/ | |
let lastRightHandOperator = null; | |
Number.prototype[">"] = Boolean.prototype[">"] = function(x){ | |
if (isNaN(parseFloat(this))){ | |
let cache = lastRightHandOperator; | |
lastRightHandOperator = this; | |
return (this && cache > x); | |
} | |
lastRightHandOperator = this; | |
return this > x; | |
}; | |
console.log( | |
8 [">"] (5) [">"] (3) | |
); | |
// true | |
console.log( | |
3 [">"] (5) [">"] (3) | |
); | |
// false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment