Skip to content

Commit 6d5a8c3

Browse files
committed
Merge pull request #3 from jmespath/jep-12
Add support for JEP-12
2 parents e68a9f3 + 4e6ab6e commit 6d5a8c3

File tree

5 files changed

+151
-96
lines changed

5 files changed

+151
-96
lines changed

jmespath.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@
214214
tokens.push({type: "QuotedIdentifier",
215215
value: identifier,
216216
start: start});
217+
} else if (stream[this.current] === "'") {
218+
start = this.current;
219+
identifier = this.consumeRawStringLiteral(stream);
220+
tokens.push({type: "Literal",
221+
value: identifier,
222+
start: start});
217223
} else if (stream[this.current] === "`") {
218224
start = this.current;
219225
var literal = this.consumeLiteral(stream);
@@ -271,6 +277,25 @@
271277
return JSON.parse(stream.slice(start, this.current));
272278
},
273279

280+
consumeRawStringLiteral: function(stream) {
281+
var start = this.current;
282+
this.current++;
283+
var maxLength = stream.length;
284+
while (stream[this.current] !== "'" && this.current < maxLength) {
285+
// You can escape a single quote and you can escape an escape.
286+
var current = this.current;
287+
if (stream[current] === "\\" && (stream[current + 1] === "\\" ||
288+
stream[current + 1] === "'")) {
289+
current += 2;
290+
} else {
291+
current++;
292+
}
293+
this.current = current;
294+
}
295+
this.current++;
296+
return stream.slice(start + 1, this.current - 1);
297+
},
298+
274299
consumeNumber: function(stream) {
275300
var start = this.current;
276301
this.current++;

test/compliance/filters.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cases": [
55
{
66
"comment": "Matching a literal",
7-
"expression": "foo[?name == `a`]",
7+
"expression": "foo[?name == 'a']",
88
"result": [{"name": "a"}]
99
}
1010
]
@@ -86,7 +86,7 @@
8686
"cases": [
8787
{
8888
"comment": "Filter with subexpression",
89-
"expression": "foo[?top.name == `a`]",
89+
"expression": "foo[?top.name == 'a']",
9090
"result": [{"top": {"name": "a"}}]
9191
}
9292
]

test/compliance/functions.json

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"error": "invalid-type"
6969
},
7070
{
71-
"expression": "avg(`abc`)",
71+
"expression": "avg('abc')",
7272
"error": "invalid-type"
7373
},
7474
{
@@ -100,23 +100,23 @@
100100
"result": -1
101101
},
102102
{
103-
"expression": "ceil(`string`)",
103+
"expression": "ceil('string')",
104104
"error": "invalid-type"
105105
},
106106
{
107-
"expression": "contains(`abc`, `a`)",
107+
"expression": "contains('abc', 'a')",
108108
"result": true
109109
},
110110
{
111-
"expression": "contains(`abc`, `d`)",
111+
"expression": "contains('abc', 'd')",
112112
"result": false
113113
},
114114
{
115-
"expression": "contains(`false`, `d`)",
115+
"expression": "contains(`false`, 'd')",
116116
"error": "invalid-type"
117117
},
118118
{
119-
"expression": "contains(strings, `a`)",
119+
"expression": "contains(strings, 'a')",
120120
"result": true
121121
},
122122
{
@@ -128,23 +128,23 @@
128128
"result": false
129129
},
130130
{
131-
"expression": "ends_with(str, `r`)",
131+
"expression": "ends_with(str, 'r')",
132132
"result": true
133133
},
134134
{
135-
"expression": "ends_with(str, `tr`)",
135+
"expression": "ends_with(str, 'tr')",
136136
"result": true
137137
},
138138
{
139-
"expression": "ends_with(str, `Str`)",
139+
"expression": "ends_with(str, 'Str')",
140140
"result": true
141141
},
142142
{
143-
"expression": "ends_with(str, `SStr`)",
143+
"expression": "ends_with(str, 'SStr')",
144144
"result": false
145145
},
146146
{
147-
"expression": "ends_with(str, `foo`)",
147+
"expression": "ends_with(str, 'foo')",
148148
"result": false
149149
},
150150
{
@@ -156,7 +156,7 @@
156156
"result": 1
157157
},
158158
{
159-
"expression": "floor(`string`)",
159+
"expression": "floor('string')",
160160
"error": "invalid-type"
161161
},
162162
{
@@ -172,11 +172,11 @@
172172
"error": "invalid-type"
173173
},
174174
{
175-
"expression": "length(`abc`)",
175+
"expression": "length('abc')",
176176
"result": 3
177177
},
178178
{
179-
"expression": "length(`\"\"`)",
179+
"expression": "length('')",
180180
"result": 0
181181
},
182182
{
@@ -288,7 +288,7 @@
288288
"result": "a"
289289
},
290290
{
291-
"expression": "type(`abc`)",
291+
"expression": "type('abc')",
292292
"result": "string"
293293
},
294294
{
@@ -352,43 +352,43 @@
352352
"error": "invalid-type"
353353
},
354354
{
355-
"expression": "join(`\", \"`, strings)",
355+
"expression": "join(', ', strings)",
356356
"result": "a, b, c"
357357
},
358358
{
359-
"expression": "join(`, `, strings)",
359+
"expression": "join(', ', strings)",
360360
"result": "a, b, c"
361361
},
362362
{
363-
"expression": "join(`,`, `[\"a\", \"b\"]`)",
363+
"expression": "join(',', `[\"a\", \"b\"]`)",
364364
"result": "a,b"
365365
},
366366
{
367-
"expression": "join(`,`, `[\"a\", 0]`)",
367+
"expression": "join(',', `[\"a\", 0]`)",
368368
"error": "invalid-type"
369369
},
370370
{
371-
"expression": "join(`, `, str)",
371+
"expression": "join(', ', str)",
372372
"error": "invalid-type"
373373
},
374374
{
375-
"expression": "join(`|`, strings)",
375+
"expression": "join('|', strings)",
376376
"result": "a|b|c"
377377
},
378378
{
379379
"expression": "join(`2`, strings)",
380380
"error": "invalid-type"
381381
},
382382
{
383-
"expression": "join(`|`, decimals)",
383+
"expression": "join('|', decimals)",
384384
"error": "invalid-type"
385385
},
386386
{
387-
"expression": "join(`|`, decimals[].to_string(@))",
387+
"expression": "join('|', decimals[].to_string(@))",
388388
"result": "1.01|1.2|-1.5"
389389
},
390390
{
391-
"expression": "join(`|`, empty_list)",
391+
"expression": "join('|', empty_list)",
392392
"result": ""
393393
},
394394
{
@@ -404,27 +404,27 @@
404404
"result": []
405405
},
406406
{
407-
"expression": "reverse(``)",
407+
"expression": "reverse('')",
408408
"result": ""
409409
},
410410
{
411-
"expression": "reverse(`hello world`)",
411+
"expression": "reverse('hello world')",
412412
"result": "dlrow olleh"
413413
},
414414
{
415-
"expression": "starts_with(str, `S`)",
415+
"expression": "starts_with(str, 'S')",
416416
"result": true
417417
},
418418
{
419-
"expression": "starts_with(str, `St`)",
419+
"expression": "starts_with(str, 'St')",
420420
"result": true
421421
},
422422
{
423-
"expression": "starts_with(str, `Str`)",
423+
"expression": "starts_with(str, 'Str')",
424424
"result": true
425425
},
426426
{
427-
"expression": "starts_with(str, `String`)",
427+
"expression": "starts_with(str, 'String')",
428428
"result": false
429429
},
430430
{
@@ -452,7 +452,7 @@
452452
"result": 0
453453
},
454454
{
455-
"expression": "to_array(`foo`)",
455+
"expression": "to_array('foo')",
456456
"result": ["foo"]
457457
},
458458
{
@@ -472,7 +472,7 @@
472472
"result": [false]
473473
},
474474
{
475-
"expression": "to_string(`foo`)",
475+
"expression": "to_string('foo')",
476476
"result": "foo"
477477
},
478478
{
@@ -484,19 +484,19 @@
484484
"result": "[0,1]"
485485
},
486486
{
487-
"expression": "to_number(`\"1.0\"`)",
487+
"expression": "to_number('1.0')",
488488
"result": 1.0
489489
},
490490
{
491-
"expression": "to_number(`\"1.1\"`)",
491+
"expression": "to_number('1.1')",
492492
"result": 1.1
493493
},
494494
{
495-
"expression": "to_number(`\"4\"`)",
495+
"expression": "to_number('4')",
496496
"result": 4
497497
},
498498
{
499-
"expression": "to_number(`\"notanumber\"`)",
499+
"expression": "to_number('notanumber')",
500500
"result": null
501501
},
502502
{

0 commit comments

Comments
 (0)