-
Notifications
You must be signed in to change notification settings - Fork 92
/
encode.ts
94 lines (71 loc) · 2.37 KB
/
encode.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
const { test } = Deno;
import { assertEquals } from "../test_deps.ts";
import { encode } from "../encode.ts";
// internally `encode` uses `getTimezoneOffset` to encode Date
// so for testing purposes we'll be overriding it
const _getTimezoneOffset = Date.prototype.getTimezoneOffset;
function resetTimezoneOffset() {
Date.prototype.getTimezoneOffset = _getTimezoneOffset;
}
function overrideTimezoneOffset(offset: number) {
Date.prototype.getTimezoneOffset = function () {
return offset;
};
}
test("encodeDatetime", function () {
// GMT
overrideTimezoneOffset(0);
const gmtDate = new Date(2019, 1, 10, 20, 30, 40, 5);
const gmtEncoded = encode(gmtDate);
assertEquals(gmtEncoded, "2019-02-10T20:30:40.005+00:00");
resetTimezoneOffset();
// GMT+02:30
overrideTimezoneOffset(-150);
const date = new Date(2019, 1, 10, 20, 30, 40, 5);
const encoded = encode(date);
assertEquals(encoded, "2019-02-10T20:30:40.005+02:30");
resetTimezoneOffset();
});
test("encodeUndefined", function () {
assertEquals(encode(undefined), null);
});
test("encodeNull", function () {
assertEquals(encode(null), null);
});
test("encodeBoolean", function () {
assertEquals(encode(true), "true");
assertEquals(encode(false), "false");
});
test("encodeNumber", function () {
assertEquals(encode(1), "1");
assertEquals(encode(1.2345), "1.2345");
});
test("encodeString", function () {
assertEquals(encode("deno-postgres"), "deno-postgres");
});
test("encodeObject", function () {
assertEquals(encode({ x: 1 }), '{"x":1}');
});
test("encodeUint8Array", function () {
const buf1 = new Uint8Array([1, 2, 3]);
const buf2 = new Uint8Array([2, 10, 500]);
assertEquals("\\x010203", encode(buf1));
assertEquals("\\x02af4", encode(buf2));
});
test("encodeArray", function () {
const array = [null, "postgres", 1, ["foo", "bar"]];
const encodedArray = encode(array);
assertEquals(encodedArray, '{NULL,"postgres","1",{"foo","bar"}}');
});
test("encodeObjectArray", function () {
const array = [{ x: 1 }, { y: 2 }];
const encodedArray = encode(array);
assertEquals(encodedArray, '{"{\\"x\\":1}","{\\"y\\":2}"}');
});
test("encodeDateArray", function () {
overrideTimezoneOffset(0);
const array = [new Date(2019, 1, 10, 20, 30, 40, 5)];
const encodedArray = encode(array);
assertEquals(encodedArray, '{"2019-02-10T20:30:40.005+00:00"}');
resetTimezoneOffset();
});