MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1function assert(condition, message) {
2 if (!condition) throw new Error(message);
3}
4
5const bytes = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]);
6
7assert(bytes.toHex() === "48656c6c6f20576f726c64", "Uint8Array.prototype.toHex");
8assert(bytes.toBase64() === "SGVsbG8gV29ybGQ=", "Uint8Array.prototype.toBase64");
9
10const fromHex = Uint8Array.fromHex("48656c6c6f20576f726c64");
11assert(fromHex.length === bytes.length, "Uint8Array.fromHex length");
12assert(bytes.every((value, index, array) => array === bytes && value === fromHex[index]), "Uint8Array.fromHex bytes");
13
14const fromBase64 = Uint8Array.fromBase64("SGVsbG8gV29ybGQ=");
15assert(fromBase64.length === bytes.length, "Uint8Array.fromBase64 length");
16assert(bytes.every((value, index) => value === fromBase64[index]), "Uint8Array.fromBase64 bytes");
17
18const hexTarget = new Uint8Array(16);
19const hexResult = hexTarget.setFromHex("48656c6c6f20576f726c64");
20assert(hexResult.read === 22, "Uint8Array.setFromHex read count");
21assert(hexResult.written === 11, "Uint8Array.setFromHex written count");
22assert(bytes.every((value, index) => value === hexTarget[index]), "Uint8Array.setFromHex bytes");
23
24const base64Target = new Uint8Array(16);
25const base64Result = base64Target.setFromBase64("SGVsbG8gV29ybGQ=");
26assert(base64Result.read === 16, "Uint8Array.setFromBase64 read count");
27assert(base64Result.written === 11, "Uint8Array.setFromBase64 written count");
28assert(bytes.every((value, index) => value === base64Target[index]), "Uint8Array.setFromBase64 bytes");
29
30console.log("Uint8Array base64/hex tests completed!");