-
-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support ternary operator #240
Comments
I like this! Internally, we use MathJs, which does support ternary operators. Happy for PRs 🙂 |
I've been on quite a roll with my own project and this would become very handy for me, so let's see if I will make PR. 🤔 |
Awesome! Ourt of interest, are you able to provide any real world uses for the ternary? I can think of things like this: // Roll a d6 where greater than or equal to 5 is success
// If success, then return 2, otherwise return 1
1d6>=5 ? 2 : 1 Or perhaps even: 1d6>=5 ? 2d10 : 2d3 Maybe even something like this (Although it could conflict with some modifiers): (2d10 * 3) > 250 ? 300 : 100 |
I can imagine a lot of benefits with this, checking for success before performing another roll is a very common action in a lot of tabletop rpgs. Something like checking if an attack hits in dnd 5e with Unfortunately, with how comparisons are implemented, something like I went ahead and made the quick change necessary and put up a pull request. |
Is your feature request related to a problem? Please describe.
In the tool I'm writing currently I use mustache for user side templating and this lib to "evaluate" the values. For some expression it would be easier and cleaner to write and use them if this lib would support ternary operations.
Describe the solution you'd like
Support writing
a ? b : c
which returnsb
ifa
is non-zero andc
ifa
is zero. With this the comparison operators should also work with just naked numbers so that the following expression would also work1>0 ? 100 : 0
.Describe alternatives you've considered
I can always write my own parser on top of this lib and I might have to do that at some point anyways. 😅
The text was updated successfully, but these errors were encountered: