You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add default/elvis and coalescing operator (#748)
* Merge default operator (?:) feature from markmelville/jsonata
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]>
* feat: add default and coalescing operators
* docs
* add more testcases
* add more tests for elvis
* use existing logic for new operators
* fix $boolean for functions
---------
Co-authored-by: Mark Melville <[email protected]>
Copy file name to clipboardExpand all lines: docs/other-operators.md
+30Lines changed: 30 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,36 @@ __Example__
25
25
26
26
`Price < 50 ? "Cheap" : "Expensive"`
27
27
28
+
## `?:` (Default/Elvis)
29
+
30
+
The default (or "elvis") operator returns the left-hand side if it has an effective Boolean value of `true`, otherwise it returns the right-hand side. This is useful for providing fallback values when an expression may evaluate to a value with an effective Boolean value of `false` (such as `null`, `false`, `0`, `''`, or `undefined`).
31
+
32
+
__Syntax__
33
+
34
+
`<expr1> ?: <expr2>`
35
+
36
+
__Example__
37
+
38
+
`foo.bar ?: 'default'` => `'default'` (if `foo.bar` is evaluates to Boolean `false`)
39
+
40
+
## `??` (Coalescing)
41
+
42
+
The coalescing operator returns the left-hand side if it is defined (not `undefined`), otherwise it returns the right-hand side. This is useful for providing fallback values only when the left-hand side is missing or not present (empty sequence), but not for other values with an effective Boolean value of `false` like `0`, `false`, or `''`.
43
+
44
+
__Syntax__
45
+
46
+
`<expr1> ?? <expr2>`
47
+
48
+
__Example__
49
+
50
+
`foo.bar ?? 42` => `42` (if `foo.bar` is undefined)
51
+
52
+
`foo.bar ?? 'default'` => `'default'` (if `foo.bar` is undefined)
53
+
54
+
`0 ?? 1` => `0`
55
+
56
+
`'' ?? 'fallback'` => `''`
57
+
28
58
## `:=` (Variable binding)
29
59
30
60
The variable binding operator is used to bind the value of the RHS to the variable name defined on the LHS. The variable binding is scoped to the current block and any nested blocks. It is an error if the LHS is not a `$` followed by a valid variable name.
Copy file name to clipboardExpand all lines: docs/programming.md
+80Lines changed: 80 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,6 +40,8 @@ Produces [this](http://try.jsonata.org/ryYn78Q0m), if you're interested!
40
40
41
41
## Conditional logic
42
42
43
+
### Ternary operator (`? :`)
44
+
43
45
If/then/else constructs can be written using the ternary operator "? :".
44
46
45
47
`predicate ? expr1 : expr2`
@@ -68,6 +70,84 @@ __Examples__
68
70
]</div>
69
71
</div>
70
72
73
+
### Elvis/Default operator (`?:`)
74
+
75
+
The default (or "elvis") operator is syntactic sugar for a common pattern using the ternary operator. It returns the left-hand side if it has an effective Boolean value of `true`, otherwise it returns the right-hand side.
76
+
77
+
`expr1 ?: expr2`
78
+
79
+
This is equivalent to:
80
+
81
+
`expr1 ? expr1 : expr2`
82
+
83
+
The elvis operator is useful for providing fallback values when an expression may evaluate to a value with an effective Boolean value of `false`, without having to repeat the expression twice as you would with the ternary operator.
84
+
85
+
__Examples__
86
+
87
+
<divclass="jsonata-ex">
88
+
<div>Account.Order.Product.{
89
+
`Product Name`: $.'Product Name',
90
+
`Category`: $.Category ?: "Uncategorized"
91
+
}</div>
92
+
<div>[
93
+
{
94
+
"Product Name": "Bowler Hat",
95
+
"Category": "Uncategorized"
96
+
},
97
+
{
98
+
"Product Name": "Trilby hat",
99
+
"Category": "Uncategorized"
100
+
},
101
+
{
102
+
"Product Name": "Bowler Hat",
103
+
"Category": "Uncategorized"
104
+
},
105
+
{
106
+
"Product Name": "Cloak",
107
+
"Category": "Uncategorized"
108
+
}
109
+
]</div>
110
+
</div>
111
+
112
+
### Coalescing operator (`??`)
113
+
114
+
The coalescing operator is syntactic sugar for a common pattern using the ternary operator with the `$exists` function. It returns the left-hand side if it is defined (not `undefined`), otherwise it returns the right-hand side.
115
+
116
+
`expr1 ?? expr2`
117
+
118
+
This is equivalent to:
119
+
120
+
`$exists(expr1) ? expr1 : expr2`
121
+
122
+
The coalescing operator is useful for providing fallback values only when the left-hand side is missing or not present (empty sequence), but not for other values with an effective Boolean value of `false` like `0`, `false`, or `''`. It avoids having to evaluate the expression twice and explicitly use the `$exists` function as you would with the ternary operator.
Any name that starts with a dollar '$' is a variable. A variable is a named reference to a value. The value can be one of any type in the language's [type system](processing#the-jsonata-type-system).
0 commit comments