chore: simplify tests a little bit

mary.my.id abd3c92f 3a723ea5

verified
Changed files
+23 -20
lib
+23 -20
lib/mod.test.ts
··· 12 12 13 13 // deno-fmt-ignore 14 14 writer.write( 15 - new Uint8Array([ 15 + Uint8Array.from([ 16 16 31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 242, 72, 205, 201, 201, 215, 81, 17 17 40, 207, 47, 202, 73, 81, 4, 0, 0, 0, 255, 255, 3, 0, 230, 198, 230, 18 18 235, 13, 0, 0, 0, ··· 23 23 } 24 24 25 25 { 26 - let buffer = new Uint8Array(0); 27 - for await (const chunk of readable) { 28 - const concat = new Uint8Array(buffer.length + chunk.length); 29 - concat.set(buffer); 30 - concat.set(chunk, buffer.length); 31 - buffer = concat; 32 - } 33 - 34 - const decoded = new TextDecoder().decode(buffer); 26 + const result = await toUint8Array(readable); 27 + const decoded = new TextDecoder().decode(result); 35 28 36 29 assertEquals(decoded, 'Hello, world!'); 37 30 } ··· 43 36 async fn() { 44 37 const { readable, writable } = new MaybeDecompressionStream(); 45 38 46 - const input = new Uint8Array([1, 2, 3, 4, 5]); 39 + const input = Uint8Array.from([1, 2, 3, 4, 5]); 47 40 48 41 { 49 42 const writer = writable.getWriter(); ··· 52 45 } 53 46 54 47 { 55 - let buffer = new Uint8Array(0); 56 - for await (const chunk of readable) { 57 - const concat = new Uint8Array(buffer.length + chunk.length); 58 - concat.set(buffer); 59 - concat.set(chunk, buffer.length); 60 - buffer = concat; 61 - } 62 - 63 - assertEquals(buffer, input); 48 + const result = await toUint8Array(readable); 49 + assertEquals(result, input); 64 50 } 65 51 }, 66 52 }); 53 + 54 + async function toUint8Array(source: AsyncIterable<Uint8Array>): Promise<Uint8Array> { 55 + let buffer: Uint8Array | undefined; 56 + 57 + for await (const value of source) { 58 + if (buffer === undefined) { 59 + buffer = value; 60 + } else { 61 + const concat = new Uint8Array(buffer.length + value.length); 62 + concat.set(buffer); 63 + concat.set(value, buffer.length); 64 + buffer = concat; 65 + } 66 + } 67 + 68 + return buffer ?? new Uint8Array(0); 69 + }