Skip to content

Conversation

@zirkelc
Copy link
Contributor

@zirkelc zirkelc commented Jul 1, 2025

This PR continues the work of @markmelville in PR #638 for the elvis/default operator A ?: B and adds the coalescing operator A ?? B as described by @andrew-coleman

Coalescing operator A ?? B which will evaluate to B iff A does not exist (empty sequence). Equivalent to [A, B](0) (from XPath).
#638 (comment)

I followed the logic of $exists() when evaluating the LHS which only checks for undefined, that means other falsy values like null are evaluated as true. This could be changed to also include null to match the behavior of JavaScript's ?? operator.

zirkelc and others added 4 commits July 1, 2025 14:33
Add support for the default (Elvis) operator (?:) which provides a
default value when the left-hand side expression is falsy.

Examples:
- order ?: 'default value'
- age ?: 42
- missing ?: other ?: 0  (chainable)

Co-authored-by: Mark Melville <[email protected]>
@markmelville
Copy link
Contributor

Thanks for picking this up. When the feedback on my PR was that the ?? was probably the more important to prioritize, I admittedly lost my steam. My feedback here is that the elvis operator should get some more test cases to show that that rhs would be selected for defined, falsy values such as 0 or "" (where as the null coalescing would not).

@zirkelc
Copy link
Contributor Author

zirkelc commented Jul 2, 2025

Thanks for your quick feedback! I added a few more tests for the elvis operator

@markmelville
Copy link
Contributor

I added a few more tests for the elvis operator

Looks good to me!

@andrew-coleman, I'm curious if features get backported to the v1 release. If so, is there anything that needs to be done here for that?

Copy link
Member

@andrew-coleman andrew-coleman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for working on this. I've left a few comments, but basically I think we can do this purely in the tokeniser / parser since these new operators are syntactic sugar for the existing ternary operator.

@andrew-coleman
Copy link
Member

I'm curious if features get backported to the v1 release. If so, is there anything that needs to be done here for that?

I haven't been maintaining a separate v1 branch.

@zirkelc
Copy link
Contributor Author

zirkelc commented Jul 6, 2025

@andrew-coleman thanks for your feedback! I incorporated the changes and added the two new operators to the conditionals section with examples. Please feel free to suggest other changes if I missed something!

I also added a few more tests to the default operator following the logic how values are cast to Boolean (https://docs.jsonata.org/boolean-functions).

However, this test should evaluate the LHS to false and therefore return 42:

https://github.com/zirkelc/jsonata/blob/8ccf176a5aa73a6e904258f6ace789c4ce3eb768/test/test-suite/groups/default-operator/case011.json#L1-L7

But it fails and returns the LHS. Is something wrong here?

@zirkelc zirkelc requested a review from andrew-coleman July 6, 2025 04:27
@andrew-coleman
Copy link
Member

But it fails and returns the LHS. Is something wrong here?

Looks like you've found a bug in $boolean() 😆
If you change this line to:

} else if (arg !== null && typeof arg === 'object' && !isFunction(arg)) {

then that should fix it.

Thanks :)

@zirkelc
Copy link
Contributor Author

zirkelc commented Jul 7, 2025

Great, I was first doubting myself 😄

Let me know if anything else should be changed in the docs

Copy link
Member

@andrew-coleman andrew-coleman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thanks!

@andrew-coleman andrew-coleman merged commit 9e1523d into jsonata-js:master Jul 8, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants