-
Notifications
You must be signed in to change notification settings - Fork 228
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
Proposal: Add a 'default' operator #370
Comments
This would be a wonderful addition to Jsonata. To expound on the OPs example of how verbose workaround can get, if you have a large expression, you don't want to have to repeat it in a ternary:
the most terse way to do it now is with statements:
with a new operator it could be:
Another workaround is a method, but an operator would be better to short-circuit the potentially expensive work of resolving the second operand:
|
Just want to clarify desired behaviour here. The expression: Alternatively, you might want to default to a fallback value only if the |
That's a good question. The behavior of JavaScript's
That is a cool trick that I didn't know. |
I think the sequence flattening would confuse ppl not familiar with that pattern. I'd prefer the |
bump |
Lmao how does this still not exist |
My take is that one of the project owners showed some ways to achieve this, and though it's not what we might want, it does get the job done. Our options are to use it, or submit a pull request. |
Look what you made me do. I created a pull request. In the past I tried adding an operator and everything was blowing up. Perhaps it's different ever since the 2.0 change from generators to async. |
As a work around I use reduce function that accumulates first !null from a list. But it doesn’t short-circuit, so it isn’t great but offer better readability than nested ternary operators. |
In one use case, I was returned a Concatenating to an empty string got me the desired result, that is:
It's definitely not going to win any beauty contests. |
I came across the solution I think. But it’s undocumented. The weirdest thing is, using half of a ternary operator works. So something, ‘$maybe ? “default value”’ would work. Found this when kept chasing the latter part of ternary. |
There is another way to default a value:
The problem with ternary is if you have syntax occurring before it - it gets interpreted as part of the condition whereas |
In JavaScript, it is possible to write statements such as:
value = input || 0;
where a variable or result can be assigned the result of another variable if it is assigned and otherwise it is assigned a default value. The closest thing in JSONata that can do this is:
$value := input ? input : 0;
which is significantly more verbose. Ideally, it would be nice if there was a way to do this in JSONata. One possibilty is to add a new operator (e.g.
||
) or is to tweak the existingor
operator.The text was updated successfully, but these errors were encountered: