Skip to content
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

added default (elvis) operator #638

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/jsonata.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ var jsonata = (function() {
case '>=':
result = evaluateComparisonExpression(lhs, rhs, op);
break;
case '?:':
result = evaluateDefault(lhs, rhs, op);
break;
case '&':
result = evaluateStringConcat(lhs, rhs);
break;
Expand Down Expand Up @@ -810,6 +813,16 @@ var jsonata = (function() {
return result;
}

/**
* Evaluate default-value expression
* @param {Object} lhs - LHS value
* @param {Object} rhs - RHS value
* @returns {*} Result
*/
function evaluateDefault(lhs, rhs) {
return lhs ? lhs : rhs;
}

/**
* Inclusion operator - in
*
Expand Down
7 changes: 7 additions & 0 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const parser = (() => {
'<=': 40,
'>=': 40,
'~>': 40,
'?:': 40,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

any input on what the binding power should be on this operator?

'and': 30,
'or': 25,
'in': 40,
Expand Down Expand Up @@ -198,6 +199,11 @@ const parser = (() => {
position += 2;
return create('operator', '~>');
}
if (currentChar === '?' && path.charAt(position + 1) === ':') {
// ?: default / elvis operator
position += 2;
return create('operator', '?:');
}
// test for single char operators
if (Object.prototype.hasOwnProperty.call(operators, currentChar)) {
position++;
Expand Down Expand Up @@ -565,6 +571,7 @@ const parser = (() => {
terminal("in"); //
prefix("-"); // unary numeric negation
infix("~>"); // function application
infix("?:"); // default value

infixr("(error)", 10, function (left) {
this.lhs = left;
Expand Down
7 changes: 7 additions & 0 deletions test/test-suite/groups/default-operator/case000.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "property missing on object uses default string on lhs",
"expr": "order?:'the usual'",
"dataset": "dataset0",
"bindings": {},
"result": "the usual"
}
7 changes: 7 additions & 0 deletions test/test-suite/groups/default-operator/case001.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "property missing on object uses default number on lhs",
"expr": "age?:42",
"dataset": "dataset0",
"bindings": {},
"result": 42
}
7 changes: 7 additions & 0 deletions test/test-suite/groups/default-operator/case003.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "property present on object is used",
"expr": "bar ?: 0",
"dataset": "dataset0",
"bindings": {},
"result": 98
}
7 changes: 7 additions & 0 deletions test/test-suite/groups/default-operator/case004.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"description": "operator can be chained",
"expr": "nope ?: foo.bar ?: 0",
"dataset": "dataset0",
"bindings": {},
"result": 42
}