Skip to content

Commit a5a5e27

Browse files
authored
Merge pull request #1 from jmespath-community/feature/function-let
JEP-11 Lexical Scoping - Function `let()`
2 parents 0be0a71 + 0f6ff5f commit a5a5e27

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed

tests/function_let.json

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
[
2+
{
3+
"given": {
4+
"search_for": "foo",
5+
"people": [
6+
{
7+
"name": "a"
8+
},
9+
{
10+
"name": "b"
11+
},
12+
{
13+
"name": "c"
14+
},
15+
{
16+
"name": "foo"
17+
},
18+
{
19+
"name": "bar"
20+
},
21+
{
22+
"name": "baz"
23+
},
24+
{
25+
"name": "qux"
26+
},
27+
{
28+
"name": "x"
29+
},
30+
{
31+
"name": "y"
32+
},
33+
{
34+
"name": "z"
35+
}
36+
]
37+
},
38+
"cases": [
39+
{
40+
"description": "Let function with filters",
41+
"expression": "let({search_for: search_for}, &people[?name==search_for].name | [0])",
42+
"result": "foo"
43+
}
44+
]
45+
},
46+
{
47+
"given": {
48+
"a": {
49+
"mylist": [
50+
{
51+
"l1": "1",
52+
"result": "foo"
53+
},
54+
{
55+
"l2": "2",
56+
"result": "bar"
57+
},
58+
{
59+
"l1": "8",
60+
"l2": "9"
61+
},
62+
{
63+
"l1": "8",
64+
"l2": "9"
65+
}
66+
],
67+
"level2": "2"
68+
},
69+
"level1": "1",
70+
"nested": {
71+
"a": {
72+
"b": {
73+
"c": {
74+
"fourth": "fourth"
75+
},
76+
"third": "third"
77+
},
78+
"second": "second"
79+
},
80+
"first": "first"
81+
},
82+
"precedence": {
83+
"a": {
84+
"b": {
85+
"c": {
86+
"variable": "fourth"
87+
},
88+
"variable": "third",
89+
"other": "y"
90+
},
91+
"variable": "second",
92+
"other": "x"
93+
},
94+
"variable": "first",
95+
"other": "w"
96+
}
97+
},
98+
"cases": [
99+
{
100+
"description": "Basic let from scope",
101+
"expression": "let({level1: level1}, &a.[level2, level1])",
102+
"result": [
103+
"2",
104+
"1"
105+
]
106+
},
107+
{
108+
"description": "Current object has precedence",
109+
"expression": "let({level1: `\"other\"`}, &level1)",
110+
"result": "1"
111+
},
112+
{
113+
"description": "No scope specified using literal hash",
114+
"expression": "let(`{}`, &a.level2)",
115+
"result": "2"
116+
},
117+
{
118+
"description": "Arbitrary variable added",
119+
"expression": "let({foo: `\"anything\"`}, &[level1, foo])",
120+
"result": [
121+
"1",
122+
"anything"
123+
]
124+
},
125+
{
126+
"description": "Basic let from current object",
127+
"expression": "let({other: level1}, &level1)",
128+
"result": "1"
129+
},
130+
{
131+
"description": "Nested let function with filters",
132+
"expression": "let({level1: level1}, &a.[mylist[?l1==level1].result, let({level2: level2}, &mylist[?l2==level2].result)])[]",
133+
"result": [
134+
"foo",
135+
"bar"
136+
]
137+
},
138+
{
139+
"description": "Nested let function with filters with literal scope binding",
140+
"expression": "let(`{\"level1\": \"1\"}`, &a.[mylist[?l1==level1].result, let({level2: level2}, &mylist[?l2==level2].result)])[]",
141+
"result": [
142+
"foo",
143+
"bar"
144+
]
145+
},
146+
{
147+
"description": "Nested let functions",
148+
"expression": "nested.let({level1: first}, &a.let({level2: second}, &b.let({level3: third}, &c.{first: level1, second: level2, third: level3, fourth: fourth})))",
149+
"result": {
150+
"first": "first",
151+
"second": "second",
152+
"third": "third",
153+
"fourth": "fourth"
154+
}
155+
},
156+
{
157+
"description": "Precedence of lexical vars from scope object",
158+
"expression": "precedence.let({other: other}, &a.let({other: other}, &b.let({other: other}, &c.{other: other})))",
159+
"result": {
160+
"other": "y"
161+
}
162+
},
163+
{
164+
"description": "Precedence of lexical vars from current object",
165+
"expression": "precedence.let({variable: variable}, &a.let({variable: variable}, &b.let({variable: variable}, &c.let({variable: `\"override\"`}, &variable))))",
166+
"result": "fourth"
167+
}
168+
]
169+
}
170+
]

0 commit comments

Comments
 (0)