Skip to content

Commit 511d3d1

Browse files
committed
chore: refinements in the Test Suite
1 parent 8ddc791 commit 511d3d1

File tree

4 files changed

+101
-51
lines changed

4 files changed

+101
-51
lines changed

src/compile.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ describe('error handling', () => {
8383
])
8484
})
8585

86+
test('should do nothing when sorting objects without a getter', () => {
87+
const data = [{ a: 1 }, { c: 3 }, { b: 2 }]
88+
expect(go(data, ['sort'])).toEqual(data)
89+
})
90+
8691
test('should not crash when sorting a list with nested arrays', () => {
8792
expect(go([[3], [7], [4]], ['sort'])).toEqual([[3], [4], [7]])
8893
expect(go([[], [], []], ['sort'])).toEqual([[], [], []])

src/functions.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ export const functions: FunctionBuildersMap = {
146146

147147
for (const item of data) {
148148
const value = getter(item) as string
149-
res[value] = res[value] ?? item
149+
if (!(value in res)) {
150+
res[value] = item
151+
}
150152
}
151153

152154
return res
@@ -162,7 +164,7 @@ export const functions: FunctionBuildersMap = {
162164
uniqBy:
163165
<T>(path: JSONQueryProperty) =>
164166
(data: T[]): T[] =>
165-
Object.values(functions.groupBy(path)(data)).map((groups) => groups[0]),
167+
Object.values(functions.keyBy(path)(data)),
166168

167169
limit:
168170
(count: number) =>
@@ -188,31 +190,12 @@ export const functions: FunctionBuildersMap = {
188190

189191
max: () => (data: number[]) => Math.max(...data),
190192

191-
in: (path: string, values: JSONQuery) => {
192-
const getter = compile(path)
193-
const _values = compile(values)
194-
195-
return (data: unknown) => (_values(data) as string[]).includes(getter(data) as string)
196-
},
197-
198-
'not in': (path: string, values: JSONQuery) => {
199-
const _in = functions.in(path, values)
200-
201-
return (data: unknown) => !_in(data)
202-
},
203-
204-
regex: (path: JSONQuery, expression: string, options?: string) => {
205-
const regex = new RegExp(expression, options)
206-
const getter = compile(path)
207-
208-
return (data: unknown) => regex.test(getter(data) as string)
209-
},
210-
211193
and: buildFunction((a, b) => !!(a && b)),
212194
or: buildFunction((a, b) => !!(a || b)),
213195
not: buildFunction((a: unknown) => !a),
214-
exists: (path: JSONQueryFunction) => {
215-
const parentPath = path.slice(1)
196+
197+
exists: (queryGet: JSONQueryFunction) => {
198+
const parentPath = queryGet.slice(1)
216199
const key = parentPath.pop()
217200
const getter = functions.get(...parentPath)
218201

@@ -228,6 +211,23 @@ export const functions: FunctionBuildersMap = {
228211

229212
return (data: unknown) => (_condition(data) ? _valueIfTrue(data) : _valueIfFalse(data))
230213
},
214+
in: (path: string, values: JSONQuery) => {
215+
const getter = compile(path)
216+
const _values = compile(values)
217+
218+
return (data: unknown) => (_values(data) as string[]).includes(getter(data) as string)
219+
},
220+
'not in': (path: string, values: JSONQuery) => {
221+
const _in = functions.in(path, values)
222+
223+
return (data: unknown) => !_in(data)
224+
},
225+
regex: (path: JSONQuery, expression: string, options?: string) => {
226+
const regex = new RegExp(expression, options)
227+
const getter = compile(path)
228+
229+
return (data: unknown) => regex.test(getter(data) as string)
230+
},
231231

232232
eq: buildFunction((a, b) => a === b),
233233
gt: buildFunction((a, b) => a > b),

test-suite/compile.test.json

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
11
{
22
"source": "https://github.com/jsonquerylang/jsonquery/tree/main/test-suite/compile.test.json",
3-
"updated": "2024-11-12T09:00:00Z",
3+
"updated": "2024-11-17T11:00:00Z",
44
"tests": [
5+
{
6+
"category": "value",
7+
"description": "should get a string",
8+
"input": null,
9+
"query": "Hello",
10+
"output": "Hello"
11+
},
12+
{
13+
"category": "value",
14+
"description": "should get a number",
15+
"input": null,
16+
"query": 2.4,
17+
"output": 2.4
18+
},
19+
{
20+
"category": "value",
21+
"description": "should get a boolean (true)",
22+
"input": null,
23+
"query": true,
24+
"output": true
25+
},
26+
{
27+
"category": "value",
28+
"description": "should get a boolean (false)",
29+
"input": null,
30+
"query": false,
31+
"output": false
32+
},
33+
{
34+
"category": "value",
35+
"description": "should get null",
36+
"input": null,
37+
"query": null,
38+
"output": null
39+
},
540
{
641
"category": "pipe",
742
"description": "should execute a pipe (1)",
843
"input": [{ "user": { "name": "Joe" } }],
9-
"query": ["pipe", ["get", "0"], ["get", "user"], ["get", "name"]],
44+
"query": ["pipe", ["get", 0], ["get", "user"], ["get", "name"]],
1045
"output": "Joe"
1146
},
1247
{
@@ -98,11 +133,18 @@
98133
},
99134
{
100135
"category": "get",
101-
"description": "should return null in case of a non existing path",
136+
"description": "should return null in case of a non-existing path (1)",
102137
"input": {},
103138
"query": ["get", "foo", "bar"],
104139
"output": null
105140
},
141+
{
142+
"category": "get",
143+
"description": "should return null in case of a non-existing path (2)",
144+
"input": [1, 2, 3],
145+
"query": ["get", 5],
146+
"output": null
147+
},
106148
{
107149
"category": "get",
108150
"description": "should get a path using function get",
@@ -147,21 +189,21 @@
147189
},
148190
{
149191
"category": "get",
150-
"description": "should get in item from an array (1)",
192+
"description": "should get an item from an array (1)",
151193
"input": ["A", "B", "C"],
152194
"query": ["get", 1],
153195
"output": "B"
154196
},
155197
{
156198
"category": "get",
157-
"description": "should get in item from an array (2)",
199+
"description": "should get an item from an array (2)",
158200
"input": { "arr": ["A", "B", "C"] },
159201
"query": ["get", "arr", 1],
160202
"output": "B"
161203
},
162204
{
163205
"category": "get",
164-
"description": "should get in item from an array (3)",
206+
"description": "should get an item from an array (3)",
165207
"input": [{ "text": "A" }, { "text": "B" }, { "text": "C" }],
166208
"query": ["get", 1, "text"],
167209
"output": "B"
@@ -229,13 +271,6 @@
229271
"query": ["sort", ["get", "score"], "desc"],
230272
"output": [{ "score": 5 }, { "score": 3 }, { "score": -2 }]
231273
},
232-
{
233-
"category": "sort",
234-
"description": "should do nothing when sorting objects without a getter",
235-
"input": [{ "a": 1 }, { "c": 3 }, { "b": 2 }],
236-
"query": ["sort"],
237-
"output": [{ "a": 1 }, { "c": 3 }, { "b": 2 }]
238-
},
239274

240275
{
241276
"category": "pick",
@@ -842,56 +877,56 @@
842877
},
843878
{
844879
"category": "exists",
845-
"description": "should calculate exists (1)",
880+
"description": "should calculate exists (2)",
846881
"input": { "a": null },
847882
"query": ["exists", ["get", "a"]],
848883
"output": true
849884
},
850885
{
851886
"category": "exists",
852-
"description": "should calculate exists (1)",
887+
"description": "should calculate exists (3)",
853888
"input": { "a": 2 },
854889
"query": ["exists", ["get", "a"]],
855890
"output": true
856891
},
857892
{
858893
"category": "exists",
859-
"description": "should calculate exists (1)",
894+
"description": "should calculate exists (4)",
860895
"input": { "a": 0 },
861896
"query": ["exists", ["get", "a"]],
862897
"output": true
863898
},
864899
{
865900
"category": "exists",
866-
"description": "should calculate exists (1)",
901+
"description": "should calculate exists (5)",
867902
"input": { "a": "" },
868903
"query": ["exists", ["get", "a"]],
869904
"output": true
870905
},
871906
{
872907
"category": "exists",
873-
"description": "should calculate exists (1)",
908+
"description": "should calculate exists (6)",
874909
"input": { "nested": { "a": 2 } },
875910
"query": ["exists", ["get", "nested", "a"]],
876911
"output": true
877912
},
878913
{
879914
"category": "exists",
880-
"description": "should calculate exists (1)",
915+
"description": "should calculate exists (7)",
881916
"input": {},
882917
"query": ["exists", ["get", "a"]],
883918
"output": false
884919
},
885920
{
886921
"category": "exists",
887-
"description": "should calculate exists (1)",
922+
"description": "should calculate exists (8)",
888923
"input": {},
889924
"query": ["exists", ["get", "nested", "a"]],
890925
"output": false
891926
},
892927
{
893928
"category": "exists",
894-
"description": "should calculate exists (1)",
929+
"description": "should calculate exists (9)",
895930
"input": {},
896931
"query": ["exists", ["get", "sort"]],
897932
"output": false
@@ -950,6 +985,13 @@
950985
"query": ["if", "", true, false],
951986
"output": false
952987
},
988+
{
989+
"category": "if",
990+
"description": "should calculate if (8)",
991+
"input": null,
992+
"query": ["if", null, true, false],
993+
"output": false
994+
},
953995

954996
{
955997
"category": "in",
@@ -1153,22 +1195,22 @@
11531195
{
11541196
"category": "round",
11551197
"description": "should round a property to two digits",
1156-
"input": { "a": 23.1345 },
1198+
"input": { "a": 23.1348 },
11571199
"query": ["round", ["get", "a"], 2],
11581200
"output": 23.13
11591201
},
11601202
{
11611203
"category": "round",
11621204
"description": "should round a property to three digits",
1163-
"input": { "a": 23.1345 },
1205+
"input": { "a": 23.1348 },
11641206
"query": ["round", ["get", "a"], 3],
11651207
"output": 23.135
11661208
},
11671209
{
11681210
"category": "round",
1169-
"description": "should round a number to three digits",
1211+
"description": "should round a number to two digits",
11701212
"input": null,
1171-
"query": ["round", 23.1345, 2],
1213+
"query": ["round", 23.1348, 2],
11721214
"output": 23.13
11731215
},
11741216

test-suite/stringify.test.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"source": "https://github.com/jsonquerylang/jsonquery/tree/main/test-suite/stringify.test.json",
3-
"updated": "2024-11-11T09:00:00Z",
3+
"updated": "2024-11-12T09:00:00Z",
44
"groups": [
55
{
66
"category": "property",
@@ -10,7 +10,10 @@
1010
{ "input": ["get", "age"], "output": ".age" },
1111
{ "input": ["get", "address", "city"], "output": ".address.city" },
1212
{ "input": ["get", "with space"], "output": ".\"with space\"" },
13-
{ "input": ["get", "with special !"], "output": ".\"with special !\"" }
13+
{ "input": ["get", "with special !"], "output": ".\"with special !\"" },
14+
{ "input": ["get", 2, "name"], "output": ".2.name" },
15+
{ "input": ["get", 0, "name"], "output": ".0.name" },
16+
{ "input": ["get", "AaZz09_$"], "output": ".AaZz09_$" }
1417
]
1518
},
1619
{

0 commit comments

Comments
 (0)