test_prelude.js (603B)
1 const enc = new TextEncoder(); 2 const dec = new TextDecoder(); 3 4 const buf = enc.encode("Hello, World"); 5 6 console.log("byteOffset", buf.byteOffset); 7 console.log("byteLength", buf.byteLength); 8 9 const str = dec.decode(buf); 10 11 if (str != "Hello, World") { 12 throw Error("test 1 failed"); 13 } 14 15 // Put 4 bytes before and after! 16 const buf2 = new Uint8Array(buf.byteLength + 8); 17 18 buf2.set(buf, 4); 19 20 buf2[0] = 42; 21 22 console.log(JSON.stringify(dec.decode(buf2.slice(3, buf2.byteLength - 4)))) 23 24 const str2 = dec.decode(buf2.slice(4, buf2.byteLength - 4)) 25 if (str2 != "Hello, World") { 26 throw Error("test 2 failed"); 27 }