Skip to content

Commit 1f1579b

Browse files
soxboxjamesls
authored andcommitted
Implement jep-9: Improved filters
Implements: * map function * paren-expression * and-expression Working with all tests but 2 filter and 1 literal. Squashed commit of the following: commit 3cca211 Author: soxbox <[email protected]> Date: Fri Oct 23 13:44:35 2015 -0700 implemented the map function commit 1439e7a Author: soxbox <[email protected]> Date: Fri Oct 23 13:33:10 2015 -0700 implemented the map function commit 26bf9b1 Merge: 5e49020 058102b Author: Timothy DeDecker <[email protected]> Date: Fri Oct 23 11:30:52 2015 -0700 Merge pull request #1 from soxbox/lparen Lparen commit 058102b Author: soxbox <[email protected]> Date: Fri Oct 23 11:29:19 2015 -0700 paren-expression commit a6280c1 Author: soxbox <[email protected]> Date: Fri Oct 23 11:18:44 2015 -0700 wip lperen commit 5e49020 Author: Timothy DeDecker <[email protected]> Date: Wed Oct 21 22:55:02 2015 -0700 implemented the Not expression commit a3d3017 Author: Timothy DeDecker <[email protected]> Date: Wed Oct 21 22:04:06 2015 -0700 Implement AND from the updated spec. commit fdd6d12 Author: Timothy DeDecker <[email protected]> Date: Wed Oct 21 22:03:15 2015 -0700 Updated the specs from the master spec branch.
1 parent dbbf2b4 commit 1f1579b

17 files changed

+675
-69
lines changed

artifacts/jmespath.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jmespath.js

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@
119119
}
120120

121121

122-
// The "[", "<", ">" tokens
122+
// The "&", "[", "<", ">" tokens
123123
// are not in basicToken because
124124
// there are two token variants
125-
// ("[?", "<=", ">="). This is specially handled
125+
// ("&&", "[?", "<=", ">="). This is specially handled
126126
// below.
127127

128128
var basicTokens = {
@@ -135,8 +135,7 @@
135135
"]": "Rbracket",
136136
"(": "Lparen",
137137
")": "Rparen",
138-
"@": "Current",
139-
"&": "Expref"
138+
"@": "Current"
140139
};
141140

142141
var identifierStart = {
@@ -231,6 +230,15 @@
231230
} else if (skipChars[stream[this.current]] !== undefined) {
232231
// Ignore whitespace.
233232
this.current++;
233+
} else if (stream[this.current] === "&") {
234+
start = this.current;
235+
this.current++;
236+
if (stream[this.current] === "&") {
237+
this.current++;
238+
tokens.push({type: "And", value: "&&", start: start});
239+
} else {
240+
tokens.push({type: "Expref", value: "&", start: start});
241+
}
234242
} else if (stream[this.current] === "|") {
235243
start = this.current;
236244
this.current++;
@@ -329,6 +337,8 @@
329337
if (stream[this.current] === "=") {
330338
this.current++;
331339
return {type: "NE", value: "!=", start: start};
340+
} else {
341+
return {type: "Not", value: "!", start: start};
332342
}
333343
} else if (startingChar === "<") {
334344
if (stream[this.current] === "=") {
@@ -419,17 +429,19 @@
419429
"Current": 0,
420430
"Expref": 0,
421431
"Pipe": 1,
422-
"EQ": 2,
423-
"GT": 2,
424-
"LT": 2,
425-
"GTE": 2,
426-
"LTE": 2,
427-
"NE": 2,
428-
"Or": 5,
429-
"Flatten": 6,
432+
"Or": 2,
433+
"And": 3,
434+
"EQ": 5,
435+
"GT": 5,
436+
"LT": 5,
437+
"GTE": 5,
438+
"LTE": 5,
439+
"NE": 5,
440+
"Flatten": 9,
430441
"Star": 20,
431442
"Filter": 20,
432443
"Dot": 40,
444+
"Not": 45,
433445
"Lbrace": 50,
434446
"Lbracket": 55,
435447
"Lparen": 60
@@ -530,11 +542,21 @@
530542
}
531543
},
532544

545+
nudNot: function() {
546+
var right = this.expression(this.bindingPower.Not);
547+
return {type: "NotExpression", children: [right]};
548+
},
549+
533550
ledOr: function(left) {
534551
var right = this.expression(this.bindingPower.Or);
535552
return {type: "OrExpression", children: [left, right]};
536553
},
537554

555+
ledAnd: function(left) {
556+
var right = this.expression(this.bindingPower.And);
557+
return {type: "AndExpression", children: [left, right]};
558+
},
559+
538560
ledPipe: function(left) {
539561
var right = this.expression(this.bindingPower.Pipe);
540562
return {type: "Pipe", children: [left, right]};
@@ -717,6 +739,22 @@
717739
return {type: "Projection", children: [leftNode, rightNode]};
718740
},
719741

742+
nudLparen: function() {
743+
var args = [];
744+
var expression;
745+
while (this.lookahead(0) !== "Rparen") {
746+
if (this.lookahead(0) === "Current") {
747+
expression = {type: "Current"};
748+
this.advance();
749+
} else {
750+
expression = this.expression(0);
751+
}
752+
args.push(expression);
753+
}
754+
this.match("Rparen");
755+
return args[0];
756+
},
757+
720758
ledLparen: function(left) {
721759
var name = left.name;
722760
var args = [];
@@ -1085,6 +1123,20 @@
10851123
return matched;
10861124
},
10871125

1126+
visitAndExpression: function(node, value) {
1127+
var first = this.visit(node.children[0], value);
1128+
1129+
if (isFalse(first) === true) {
1130+
return first;
1131+
}
1132+
return this.visit(node.children[1], value);
1133+
},
1134+
1135+
visitNotExpression: function(node, value) {
1136+
var first = this.visit(node.children[0], value);
1137+
return isFalse(first);
1138+
},
1139+
10881140
visitLiteral: function(node) {
10891141
return node.value;
10901142
},
@@ -1144,6 +1196,9 @@
11441196
length: {
11451197
func: this.functionLength,
11461198
signature: [{types: ["string", "array", "object"]}]},
1199+
map: {
1200+
func: this.functionMap,
1201+
signature: [{types: ["expref"]}, {types: ["array"]}]},
11471202
max: {
11481203
func: this.functionMax,
11491204
signature: [{types: ["array-number", "array-string"]}]},
@@ -1359,6 +1414,17 @@
13591414
}
13601415
},
13611416

1417+
functionMap: function(resolvedArgs) {
1418+
var mapped = [];
1419+
var interpreter = this.interpreter;
1420+
var exprefNode = resolvedArgs[0];
1421+
var elements = resolvedArgs[1];
1422+
for (var i = 0; i < elements.length; i++) {
1423+
mapped.push(interpreter.visit(exprefNode, elements[i]));
1424+
}
1425+
return mapped;
1426+
},
1427+
13621428
functionMerge: function(resolvedArgs) {
13631429
var merged = {};
13641430
for (var i = 0; i < resolvedArgs.length; i++) {

test/compliance/basic.json

100644100755
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
"expression": "foo.bar.baz",
1515
"result": "correct"
1616
},
17+
{
18+
"expression": "foo\n.\nbar\n.baz",
19+
"result": "correct"
20+
},
1721
{
1822
"expression": "foo.bar.baz.bad",
1923
"result": null

0 commit comments

Comments
 (0)