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
|
import { expect, it, describe } from "bun:test";
describe("TextEncoder", () => {
it("should encode latin1 text", () => {
const text = "Hello World!";
const encoder = new TextEncoder();
const encoded = encoder.encode(text);
expect(encoded instanceof Uint8Array).toBe(true);
expect(encoded.length).toBe(text.length);
const result = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33];
for (let i = 0; i < result.length; i++) {
expect(encoded[i]).toBe(result[i]);
}
});
it("should encode latin1 rope text", () => {
var text = "Hello";
text += " ";
text += "World!";
const encoder = new TextEncoder();
const encoded = encoder.encode(text);
expect(encoded instanceof Uint8Array).toBe(true);
const result = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33];
for (let i = 0; i < result.length; i++) {
expect(encoded[i]).toBe(result[i]);
}
expect(result.length).toBe(encoded.length);
});
it("should encode utf-16 text", () => {
const text = `❤️ Red Heart
✨ Sparkles
🔥 Fire
`;
const encoder = new TextEncoder();
const encoded = encoder.encode(text);
expect(encoded instanceof Uint8Array).toBe(true);
const result = [
226, 157, 164, 239, 184, 143, 32, 82, 101, 100, 32, 72, 101, 97, 114, 116,
10, 32, 32, 32, 32, 226, 156, 168, 32, 83, 112, 97, 114, 107, 108, 101,
115, 10, 32, 32, 32, 32, 240, 159, 148, 165, 32, 70, 105, 114, 101, 10,
32, 32, 32, 32,
];
expect(encoded.length).toBe(result.length);
for (let i = 0; i < result.length; i++) {
expect(encoded[i]).toBe(result[i]);
}
});
it("should encode utf-16 rope text", () => {
var textReal = `❤️ Red Heart
✨ Sparkles
🔥 Fire
`;
var a = textReal.split("");
var text = "";
for (let j of a) {
text += j;
}
const text2 = `❤️ Red Heart
✨ Sparkles
🔥 Fire
`;
// expect(text2).toBe(text);
// console.log(text2 === text);
const encoder = new TextEncoder();
const encoded = encoder.encode(text);
console.log(text);
console.log(textReal);
expect(encoded instanceof Uint8Array).toBe(true);
const result = [
226, 157, 164, 239, 184, 143, 32, 82, 101, 100, 32, 72, 101, 97, 114, 116,
10, 32, 32, 32, 32, 226, 156, 168, 32, 83, 112, 97, 114, 107, 108, 101,
115, 10, 32, 32, 32, 32, 240, 159, 148, 165, 32, 70, 105, 114, 101, 10,
32, 32, 32, 32,
];
const len = Math.min(result.length, encoded.length);
for (let i = 0; i < len; i++) {
expect(encoded[i]).toBe(result[i]);
}
expect(encoded.length).toBe(result.length);
});
// it("should use a unicode replacement character for invalid surrogate pairs", () => {
// var bad = [
// {
// input: "\uD800",
// expected: "\uFFFD",
// name: "lone surrogate lead",
// },
// {
// input: "\uDC00",
// expected: "\uFFFD",
// name: "lone surrogate trail",
// },
// {
// input: "\uD800\u0000",
// expected: "\uFFFD\u0000",
// name: "unmatched surrogate lead",
// },
// {
// input: "\uDC00\u0000",
// expected: "\uFFFD\u0000",
// name: "unmatched surrogate trail",
// },
// {
// input: "\uDC00\uD800",
// expected: "\uFFFD\uFFFD",
// name: "swapped surrogate pair",
// },
// {
// input: "\uD834\uDD1E",
// expected: "\uD834\uDD1E",
// name: "properly encoded MUSICAL SYMBOL G CLEF (U+1D11E)",
// },
// ];
// const encoder = new TextEncoder();
// for (var i = 0; i < bad.length; i++) {
// const input = encoder.encode(bad[i].input);
// const output = encoder.encode(bad[i].expected);
// for (let j = 0; j < input.length; j++) {
// expect(input[j]).toBe(output[j]);
// console.log(input[j], output[j]);
// }
// }
// });
});
|