Skip to content

Commit c474ed8

Browse files
authored
chore: simplify postgresInterval code (#679)
1 parent 56c6380 commit c474ed8

4 files changed

Lines changed: 196 additions & 38 deletions

File tree

packages/graphile-build-pg/src/postgresInterval.js

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,48 +30,30 @@ export type Interval = {
3030
seconds: number,
3131
};
3232

33-
// All intervals will have exactly these properties:
34-
const BASE: Interval = Object.freeze({
35-
years: 0,
36-
months: 0,
37-
days: 0,
38-
hours: 0,
39-
minutes: 0,
40-
seconds: 0.0,
41-
});
42-
4333
export function parseInterval(interval: string): Interval {
44-
const result = { ...BASE };
45-
4634
if (!interval) {
47-
return result;
35+
return {
36+
years: 0,
37+
months: 0,
38+
days: 0,
39+
hours: 0,
40+
minutes: 0,
41+
seconds: 0.0,
42+
};
4843
}
4944

50-
const matches = INTERVAL.exec(interval);
51-
if (!matches) {
52-
throw new Error(`Failed to parse interval '${interval}' from PostgreSQL`);
53-
}
54-
55-
const [
56-
,
57-
years,
58-
months,
59-
days,
60-
plusMinusTime,
61-
hours,
62-
minutes,
63-
seconds,
64-
] = matches;
45+
const [, years, months, days, plusMinusTime, hours, minutes, seconds] =
46+
INTERVAL.exec(interval) || [];
6547

6648
const timeMultiplier = plusMinusTime === "-" ? -1 : 1;
6749

68-
if (years) result.years = parseInt(years, 10);
69-
if (months) result.months = parseInt(months, 10);
70-
if (days) result.days = parseInt(days, 10);
71-
if (hours) result.hours = timeMultiplier * parseInt(hours, 10);
72-
if (minutes) result.minutes = timeMultiplier * parseInt(minutes, 10);
73-
// Seconds can be decimal; all other values are integer
74-
if (seconds) result.seconds = timeMultiplier * parseFloat(seconds);
75-
76-
return result;
50+
return {
51+
years: years ? parseInt(years, 10) : 0,
52+
months: months ? parseInt(months, 10) : 0,
53+
days: days ? parseInt(days, 10) : 0,
54+
hours: hours ? timeMultiplier * parseInt(hours, 10) : 0,
55+
minutes: minutes ? timeMultiplier * parseInt(minutes, 10) : 0,
56+
// Seconds can be decimal; all other values are integer
57+
seconds: seconds ? timeMultiplier * parseFloat(seconds) : 0,
58+
};
7759
}

packages/postgraphile-core/__tests__/integration/__snapshots__/mutations.test.js.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,22 @@ Object {
22342234
"seconds": 1,
22352235
"years": 1,
22362236
},
2237+
Object {
2238+
"days": 0,
2239+
"hours": 0,
2240+
"minutes": 0,
2241+
"months": 0,
2242+
"seconds": 0,
2243+
"years": 200,
2244+
},
2245+
Object {
2246+
"days": 0,
2247+
"hours": 0,
2248+
"minutes": 3,
2249+
"months": 0,
2250+
"seconds": 20.2,
2251+
"years": 0,
2252+
},
22372253
],
22382254
"json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}",
22392255
"jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}",
@@ -2532,6 +2548,22 @@ Object {
25322548
"seconds": 1,
25332549
"years": 1,
25342550
},
2551+
Object {
2552+
"days": 0,
2553+
"hours": 0,
2554+
"minutes": 0,
2555+
"months": 0,
2556+
"seconds": 0,
2557+
"years": 200,
2558+
},
2559+
Object {
2560+
"days": 0,
2561+
"hours": 0,
2562+
"minutes": 3,
2563+
"months": 0,
2564+
"seconds": 20.2,
2565+
"years": 0,
2566+
},
25352567
],
25362568
"json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}",
25372569
"jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}",

packages/postgraphile-core/__tests__/integration/__snapshots__/queries.test.js.snap

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8940,6 +8940,22 @@ Object {
89408940
"seconds": 1,
89418941
"years": 1,
89428942
},
8943+
Object {
8944+
"days": 0,
8945+
"hours": 0,
8946+
"minutes": 0,
8947+
"months": 0,
8948+
"seconds": 0,
8949+
"years": 200,
8950+
},
8951+
Object {
8952+
"days": 0,
8953+
"hours": 0,
8954+
"minutes": 3,
8955+
"months": 0,
8956+
"seconds": 20.2,
8957+
"years": 0,
8958+
},
89438959
],
89448960
"json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}",
89458961
"jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}",
@@ -9237,6 +9253,22 @@ Object {
92379253
"seconds": 1,
92389254
"years": 1,
92399255
},
9256+
Object {
9257+
"days": 0,
9258+
"hours": 0,
9259+
"minutes": 0,
9260+
"months": 0,
9261+
"seconds": 0,
9262+
"years": 200,
9263+
},
9264+
Object {
9265+
"days": 0,
9266+
"hours": 0,
9267+
"minutes": 3,
9268+
"months": 0,
9269+
"seconds": 20.2,
9270+
"years": 0,
9271+
},
92409272
],
92419273
"json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}",
92429274
"jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}",
@@ -9541,6 +9573,22 @@ Object {
95419573
"seconds": 1,
95429574
"years": 1,
95439575
},
9576+
Object {
9577+
"days": 0,
9578+
"hours": 0,
9579+
"minutes": 0,
9580+
"months": 0,
9581+
"seconds": 0,
9582+
"years": 200,
9583+
},
9584+
Object {
9585+
"days": 0,
9586+
"hours": 0,
9587+
"minutes": 3,
9588+
"months": 0,
9589+
"seconds": 20.2,
9590+
"years": 0,
9591+
},
95449592
],
95459593
"json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}",
95469594
"jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}",
@@ -10134,6 +10182,22 @@ Object {
1013410182
"seconds": 1,
1013510183
"years": 1,
1013610184
},
10185+
Object {
10186+
"days": 0,
10187+
"hours": 0,
10188+
"minutes": 0,
10189+
"months": 0,
10190+
"seconds": 0,
10191+
"years": 200,
10192+
},
10193+
Object {
10194+
"days": 0,
10195+
"hours": 0,
10196+
"minutes": 3,
10197+
"months": 0,
10198+
"seconds": 20.2,
10199+
"years": 0,
10200+
},
1013710201
],
1013810202
"json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}",
1013910203
"jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}",
@@ -10431,6 +10495,22 @@ Object {
1043110495
"seconds": 1,
1043210496
"years": 1,
1043310497
},
10498+
Object {
10499+
"days": 0,
10500+
"hours": 0,
10501+
"minutes": 0,
10502+
"months": 0,
10503+
"seconds": 0,
10504+
"years": 200,
10505+
},
10506+
Object {
10507+
"days": 0,
10508+
"hours": 0,
10509+
"minutes": 3,
10510+
"months": 0,
10511+
"seconds": 20.2,
10512+
"years": 0,
10513+
},
1043410514
],
1043510515
"json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}",
1043610516
"jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}",
@@ -10735,6 +10815,22 @@ Object {
1073510815
"seconds": 1,
1073610816
"years": 1,
1073710817
},
10818+
Object {
10819+
"days": 0,
10820+
"hours": 0,
10821+
"minutes": 0,
10822+
"months": 0,
10823+
"seconds": 0,
10824+
"years": 200,
10825+
},
10826+
Object {
10827+
"days": 0,
10828+
"hours": 0,
10829+
"minutes": 3,
10830+
"months": 0,
10831+
"seconds": 20.2,
10832+
"years": 0,
10833+
},
1073810834
],
1073910835
"json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}",
1074010836
"jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}",
@@ -11635,6 +11731,22 @@ Object {
1163511731
"seconds": 1,
1163611732
"years": 1,
1163711733
},
11734+
Object {
11735+
"days": 0,
11736+
"hours": 0,
11737+
"minutes": 0,
11738+
"months": 0,
11739+
"seconds": 0,
11740+
"years": 200,
11741+
},
11742+
Object {
11743+
"days": 0,
11744+
"hours": 0,
11745+
"minutes": 3,
11746+
"months": 0,
11747+
"seconds": 20.2,
11748+
"years": 0,
11749+
},
1163811750
],
1163911751
"json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}",
1164011752
"jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}",
@@ -11932,6 +12044,22 @@ Object {
1193212044
"seconds": 1,
1193312045
"years": 1,
1193412046
},
12047+
Object {
12048+
"days": 0,
12049+
"hours": 0,
12050+
"minutes": 0,
12051+
"months": 0,
12052+
"seconds": 0,
12053+
"years": 200,
12054+
},
12055+
Object {
12056+
"days": 0,
12057+
"hours": 0,
12058+
"minutes": 3,
12059+
"months": 0,
12060+
"seconds": 20.2,
12061+
"years": 0,
12062+
},
1193512063
],
1193612064
"json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}",
1193712065
"jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}",
@@ -12236,6 +12364,22 @@ Object {
1223612364
"seconds": 1,
1223712365
"years": 1,
1223812366
},
12367+
Object {
12368+
"days": 0,
12369+
"hours": 0,
12370+
"minutes": 0,
12371+
"months": 0,
12372+
"seconds": 0,
12373+
"years": 200,
12374+
},
12375+
Object {
12376+
"days": 0,
12377+
"hours": 0,
12378+
"minutes": 3,
12379+
"months": 0,
12380+
"seconds": 20.2,
12381+
"years": 0,
12382+
},
1223912383
],
1224012384
"json": "{\\"a\\":1,\\"b\\":2,\\"c\\":3,\\"d\\":{\\"e\\":4,\\"f\\":5,\\"g\\":[6,7,8,\\"x\\",false,null]}}",
1224112385
"jsonb": "{\\"1\\":\\"a\\",\\"2\\":\\"b\\",\\"3\\":\\"c\\",\\"4\\":{\\"5\\":\\"d\\",\\"6\\":\\"e\\",\\"7\\":[\\"f\\",\\"g\\",\\"h\\",42,true,null]}}",

packages/postgraphile-core/__tests__/kitchen-sink-data.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ insert into b.types values (
133133
'04:05:06',
134134
'04:05:06 -8:00',
135135
'1 year 2 months 3 days 4 hours 5 minutes 6.789123456 seconds',
136-
ARRAY['1 year 2 months 3 days 4 hours 5 minutes 6.789123456 seconds', '1 year 1 months 1 days 1 hours 1 minutes 1 seconds']::interval[],
136+
ARRAY['1 year 2 months 3 days 4 hours 5 minutes 6.789123456 seconds', '1 year 1 months 1 days 1 hours 1 minutes 1 seconds', '200 years', '200.200 seconds']::interval[],
137137
'9876543.21',
138138
(null, null, null, null, null, null, null, null),
139139
((3, '456', 'red', 'aed18400-2a92-46df-8204-b70c728b3520', 'BAR_FOO', 'one', interval '6 hours', 93), (null, null, null, null, null, null, null, null), 7),

0 commit comments

Comments
 (0)