-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathdecoders.ts
413 lines (343 loc) · 9.97 KB
/
decoders.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import { parseDate } from "../deps.ts";
import { parseArray } from "./array_parser.ts";
import type {
Box,
Circle,
Float8,
Line,
LineSegment,
Path,
Point,
Polygon,
TID,
} from "./types.ts";
// Datetime parsing based on:
// https://github.com/bendrucker/postgres-date/blob/master/index.js
// Copyright (c) Ben Drucker <[email protected]> (bendrucker.me). MIT License.
const BACKSLASH_BYTE_VALUE = 92;
const BC_RE = /BC$/;
const DATETIME_RE =
/^(\d{1,})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})(\.\d{1,})?/;
const HEX = 16;
const HEX_PREFIX_REGEX = /^\\x/;
const TIMEZONE_RE = /([Z+-])(\d{2})?:?(\d{2})?:?(\d{2})?/;
export function decodeBigint(value: string): bigint {
return BigInt(value);
}
export function decodeBigintArray(value: string) {
return parseArray(value, decodeBigint);
}
export function decodeBoolean(value: string): boolean {
const v = value.toLowerCase();
return (
v === "t" ||
v === "true" ||
v === "y" ||
v === "yes" ||
v === "on" ||
v === "1"
);
}
export function decodeBooleanArray(value: string) {
return parseArray(value, decodeBoolean);
}
export function decodeBox(value: string): Box {
const points = value.match(/\(.*?\)/g) || [];
if (points.length !== 2) {
throw new Error(
`Invalid Box: "${value}". Box must have only 2 point, ${points.length} given.`,
);
}
const [a, b] = points;
try {
return {
a: decodePoint(a),
b: decodePoint(b),
};
} catch (e) {
throw new Error(`Invalid Box: "${value}" : ${e.message}`);
}
}
export function decodeBoxArray(value: string) {
return parseArray(value, decodeBox, ";");
}
export function decodeBytea(byteaStr: string): Uint8Array {
if (HEX_PREFIX_REGEX.test(byteaStr)) {
return decodeByteaHex(byteaStr);
} else {
return decodeByteaEscape(byteaStr);
}
}
export function decodeByteaArray(value: string) {
return parseArray(value, decodeBytea);
}
function decodeByteaEscape(byteaStr: string): Uint8Array {
const bytes = [];
let i = 0;
let k = 0;
while (i < byteaStr.length) {
if (byteaStr[i] !== "\\") {
bytes.push(byteaStr.charCodeAt(i));
++i;
} else {
if (/[0-7]{3}/.test(byteaStr.substr(i + 1, 3))) {
bytes.push(parseInt(byteaStr.substr(i + 1, 3), 8));
i += 4;
} else {
let backslashes = 1;
while (
i + backslashes < byteaStr.length &&
byteaStr[i + backslashes] === "\\"
) {
backslashes++;
}
for (k = 0; k < Math.floor(backslashes / 2); ++k) {
bytes.push(BACKSLASH_BYTE_VALUE);
}
i += Math.floor(backslashes / 2) * 2;
}
}
}
return new Uint8Array(bytes);
}
function decodeByteaHex(byteaStr: string): Uint8Array {
const bytesStr = byteaStr.slice(2);
const bytes = new Uint8Array(bytesStr.length / 2);
for (let i = 0, j = 0; i < bytesStr.length; i += 2, j++) {
bytes[j] = parseInt(bytesStr[i] + bytesStr[i + 1], HEX);
}
return bytes;
}
export function decodeCircle(value: string): Circle {
const [point, radius] = value
.substring(1, value.length - 1)
.split(/,(?![^(]*\))/) as [string, Float8];
if (Number.isNaN(parseFloat(radius))) {
throw new Error(
`Invalid Circle: "${value}". Circle radius "${radius}" must be a valid number.`,
);
}
try {
return {
point: decodePoint(point),
radius: radius,
};
} catch (e) {
throw new Error(`Invalid Circle: "${value}" : ${e.message}`);
}
}
export function decodeCircleArray(value: string) {
return parseArray(value, decodeCircle);
}
export function decodeDate(dateStr: string): Date | number {
// there are special `infinity` and `-infinity`
// cases representing out-of-range dates
if (dateStr === "infinity") {
return Number(Infinity);
} else if (dateStr === "-infinity") {
return Number(-Infinity);
}
return parseDate(dateStr, "yyyy-MM-dd");
}
export function decodeDateArray(value: string) {
return parseArray(value, decodeDate);
}
export function decodeDatetime(dateStr: string): number | Date {
/**
* Postgres uses ISO 8601 style date output by default:
* 1997-12-17 07:37:16-08
*/
const matches = DATETIME_RE.exec(dateStr);
if (!matches) {
return decodeDate(dateStr);
}
const isBC = BC_RE.test(dateStr);
const year = parseInt(matches[1], 10) * (isBC ? -1 : 1);
// remember JS dates are 0-based
const month = parseInt(matches[2], 10) - 1;
const day = parseInt(matches[3], 10);
const hour = parseInt(matches[4], 10);
const minute = parseInt(matches[5], 10);
const second = parseInt(matches[6], 10);
// ms are written as .007
const msMatch = matches[7];
const ms = msMatch ? 1000 * parseFloat(msMatch) : 0;
let date: Date;
const offset = decodeTimezoneOffset(dateStr);
if (offset === null) {
date = new Date(year, month, day, hour, minute, second, ms);
} else {
// This returns miliseconds from 1 January, 1970, 00:00:00,
// adding decoded timezone offset will construct proper date object.
const utc = Date.UTC(year, month, day, hour, minute, second, ms);
date = new Date(utc + offset);
}
// use `setUTCFullYear` because if date is from first
// century `Date`'s compatibility for millenium bug
// would set it as 19XX
date.setUTCFullYear(year);
return date;
}
export function decodeDatetimeArray(value: string) {
return parseArray(value, decodeDatetime);
}
export function decodeInt(value: string): number {
return parseInt(value, 10);
}
export function decodeIntArray(value: string) {
return parseArray(value, decodeInt);
}
export function decodeFloat(value: string): number {
return parseFloat(value);
}
export function decodeFloatArray(value: string) {
return parseArray(value, decodeFloat);
}
export function decodeJson(value: string): unknown {
return JSON.parse(value);
}
export function decodeJsonArray(value: string): unknown[] {
return parseArray(value, JSON.parse);
}
export function decodeLine(value: string): Line {
const equationConsts = value.substring(1, value.length - 1).split(",") as [
Float8,
Float8,
Float8,
];
if (equationConsts.length !== 3) {
throw new Error(
`Invalid Line: "${value}". Line in linear equation format must have 3 constants, ${equationConsts.length} given.`,
);
}
equationConsts.forEach((c) => {
if (Number.isNaN(parseFloat(c))) {
throw new Error(
`Invalid Line: "${value}". Line constant "${c}" must be a valid number.`,
);
}
});
const [a, b, c] = equationConsts;
return {
a: a,
b: b,
c: c,
};
}
export function decodeLineArray(value: string) {
return parseArray(value, decodeLine);
}
export function decodeLineSegment(value: string): LineSegment {
const points = value.substring(1, value.length - 1).match(/\(.*?\)/g) || [];
if (points.length !== 2) {
throw new Error(
`Invalid Line Segment: "${value}". Line segments must have only 2 point, ${points.length} given.`,
);
}
const [a, b] = points;
try {
return {
a: decodePoint(a),
b: decodePoint(b),
};
} catch (e) {
throw new Error(`Invalid Line Segment: "${value}" : ${e.message}`);
}
}
export function decodeLineSegmentArray(value: string) {
return parseArray(value, decodeLineSegment);
}
export function decodePath(value: string): Path {
// Split on commas that are not inside parantheses
// since encapsulated commas are separators for the point coordinates
const points = value.substring(1, value.length - 1).split(/,(?![^(]*\))/);
return points.map((point) => {
try {
return decodePoint(point);
} catch (e) {
throw new Error(`Invalid Path: "${value}" : ${e.message}`);
}
});
}
export function decodePathArray(value: string) {
return parseArray(value, decodePath);
}
export function decodePoint(value: string): Point {
const coordinates = value
.substring(1, value.length - 1)
.split(",") as Float8[];
if (coordinates.length !== 2) {
throw new Error(
`Invalid Point: "${value}". Points must have only 2 coordinates, ${coordinates.length} given.`,
);
}
const [x, y] = coordinates;
if (Number.isNaN(parseFloat(x)) || Number.isNaN(parseFloat(y))) {
throw new Error(
`Invalid Point: "${value}". Coordinate "${
Number.isNaN(parseFloat(x)) ? x : y
}" must be a valid number.`,
);
}
return {
x: x,
y: y,
};
}
export function decodePointArray(value: string) {
return parseArray(value, decodePoint);
}
export function decodePolygon(value: string): Polygon {
try {
return decodePath(value);
} catch (e) {
throw new Error(`Invalid Polygon: "${value}" : ${e.message}`);
}
}
export function decodePolygonArray(value: string) {
return parseArray(value, decodePolygon);
}
export function decodeStringArray(value: string) {
if (!value) return null;
return parseArray(value, (value) => value);
}
/**
* Decode numerical timezone offset from provided date string.
*
* Matched these kinds:
* - `Z (UTC)`
* - `-05`
* - `+06:30`
* - `+06:30:10`
*
* Returns offset in miliseconds.
*/
function decodeTimezoneOffset(dateStr: string): null | number {
// get rid of date part as TIMEZONE_RE would match '-MM` part
const timeStr = dateStr.split(" ")[1];
const matches = TIMEZONE_RE.exec(timeStr);
if (!matches) {
return null;
}
const type = matches[1];
if (type === "Z") {
// Zulu timezone === UTC === 0
return 0;
}
// in JS timezone offsets are reversed, ie. timezones
// that are "positive" (+01:00) are represented as negative
// offsets and vice-versa
const sign = type === "-" ? 1 : -1;
const hours = parseInt(matches[2], 10);
const minutes = parseInt(matches[3] || "0", 10);
const seconds = parseInt(matches[4] || "0", 10);
const offset = hours * 3600 + minutes * 60 + seconds;
return sign * offset * 1000;
}
export function decodeTid(value: string): TID {
const [x, y] = value.substring(1, value.length - 1).split(",");
return [BigInt(x), BigInt(y)];
}
export function decodeTidArray(value: string) {
return parseArray(value, decodeTid);
}