Skip to content

Commit 73d5265

Browse files
committed
Merge branch 'jep-10'
* jep-10: Sync additional test from jmespath.test Implement JEP-10: Slice Projections
2 parents cd4a1e2 + 0d865e6 commit 73d5265

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

jmespath.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,8 @@
530530

531531
nudLbracket: function() {
532532
if (this.lookahead(0) === "Number" || this.lookahead(0) === "Colon") {
533-
return this.parseIndexExpression();
533+
var right = this.parseIndexExpression();
534+
return this.projectIfSlice({type: "Identity"}, right);
534535
} else if (this.lookahead(0) === "Star" &&
535536
this.lookahead(1) === "Rbracket") {
536537
this.advance();
@@ -556,6 +557,18 @@
556557
}
557558
},
558559

560+
projectIfSlice: function(left, right) {
561+
var indexExpr = {type: "IndexExpression", children: [left, right]};
562+
if (right.type === "Slice") {
563+
return {
564+
type: "Projection",
565+
children: [indexExpr, this.parseProjectionRHS(this.bindingPower.Star)]
566+
};
567+
} else {
568+
return indexExpr;
569+
}
570+
},
571+
559572
parseSliceExpression: function() {
560573
// [start:end:step] where each part is optional, as well as the last
561574
// colon.
@@ -653,7 +666,7 @@
653666
var right;
654667
if (token.type === "Number" || token.type === "Colon") {
655668
right = this.parseIndexExpression();
656-
return {type: "IndexExpression", children: [left, right]};
669+
return this.projectIfSlice(left, right);
657670
} else {
658671
this.match("Star");
659672
this.match("Rbracket");

test/compliance/slice.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,57 @@
127127
"error": "syntax"
128128
}
129129
]
130+
}, {
131+
"given": {
132+
"foo": [{"a": 1}, {"a": 2}, {"a": 3}],
133+
"bar": [{"a": {"b": 1}}, {"a": {"b": 2}},
134+
{"a": {"b": 3}}],
135+
"baz": 50
136+
},
137+
"cases": [
138+
{
139+
"expression": "foo[:2].a",
140+
"result": [1, 2]
141+
},
142+
{
143+
"expression": "foo[:2].b",
144+
"result": []
145+
},
146+
{
147+
"expression": "foo[:2].a.b",
148+
"result": []
149+
},
150+
{
151+
"expression": "bar[::-1].a.b",
152+
"result": [3, 2, 1]
153+
},
154+
{
155+
"expression": "bar[:2].a.b",
156+
"result": [1, 2]
157+
},
158+
{
159+
"expression": "baz[:2].a",
160+
"result": null
161+
}
162+
]
163+
}, {
164+
"given": [{"a": 1}, {"a": 2}, {"a": 3}],
165+
"cases": [
166+
{
167+
"expression": "[:]",
168+
"result": [{"a": 1}, {"a": 2}, {"a": 3}]
169+
},
170+
{
171+
"expression": "[:2].a",
172+
"result": [1, 2]
173+
},
174+
{
175+
"expression": "[::-1].a",
176+
"result": [3, 2, 1]
177+
},
178+
{
179+
"expression": "[:2].b",
180+
"result": []
181+
}
182+
]
130183
}]

0 commit comments

Comments
 (0)