diff options
author | 2023-10-04 01:28:59 +0800 | |
---|---|---|
committer | 2023-10-03 10:28:59 -0700 | |
commit | 476fa4deda73ce3d63a2fb8175e66678434668d6 (patch) | |
tree | e6a0b7eb969cd899a1ef1d879dedc5fb17e40d92 /test/js | |
parent | 4f1710d2d6aba23613d3a62938efecfb7c10c364 (diff) | |
download | bun-476fa4deda73ce3d63a2fb8175e66678434668d6.tar.gz bun-476fa4deda73ce3d63a2fb8175e66678434668d6.tar.zst bun-476fa4deda73ce3d63a2fb8175e66678434668d6.zip |
feat(encoding): support BOM detection with test passed (#6074)
Diffstat (limited to 'test/js')
-rw-r--r-- | test/js/web/encoding/text-decoder.test.js | 24 | ||||
-rw-r--r-- | test/js/web/encoding/text-encoder.test.js | 2 |
2 files changed, 24 insertions, 2 deletions
diff --git a/test/js/web/encoding/text-decoder.test.js b/test/js/web/encoding/text-decoder.test.js index dabdb0936..3685a5f6d 100644 --- a/test/js/web/encoding/text-decoder.test.js +++ b/test/js/web/encoding/text-decoder.test.js @@ -250,7 +250,7 @@ describe("TextDecoder", () => { it("constructor should set values", () => { const decoder = new TextDecoder("utf-8", { fatal: true, ignoreBOM: false }); expect(decoder.fatal).toBe(true); - // expect(decoder.ignoreBOM).toBe(false); // currently the getter for ignoreBOM doesn't work and always returns undefined + expect(decoder.ignoreBOM).toBe(false); }); it("should throw on invalid input", () => { @@ -265,6 +265,28 @@ describe("TextDecoder", () => { }); }); +describe("TextDecoder ignoreBOM", () => { + it.each([ + { + encoding: "utf-8", + bytes: [0xef, 0xbb, 0xbf, 0x61, 0x62, 0x63], + }, + { + encoding: "utf-16le", + bytes: [0xff, 0xfe, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00], + }, + ])("should ignoreBOM for: %o", ({ encoding, bytes }) => { + const BOM = "\uFEFF"; + const array = new Uint8Array(bytes); + + const decoder_ignore_bom = new TextDecoder(encoding, { ignoreBOM: true }); + expect(decoder_ignore_bom.decode(array)).toStrictEqual(`${BOM}abc`); + + const decoder_not_ignore_bom = new TextDecoder(encoding, { ignoreBOM: false }); + expect(decoder_not_ignore_bom.decode(array)).toStrictEqual("abc"); + }); +}); + it("truncated sequences", () => { const assert_equals = (a, b) => expect(a).toBe(b); diff --git a/test/js/web/encoding/text-encoder.test.js b/test/js/web/encoding/text-encoder.test.js index 1bf2057bc..78940a6eb 100644 --- a/test/js/web/encoding/text-encoder.test.js +++ b/test/js/web/encoding/text-encoder.test.js @@ -111,7 +111,7 @@ describe("TextEncoder", () => { const fixture = new Uint8Array(await Bun.file(import.meta.dir + "/utf8-encoding-fixture.bin").arrayBuffer()); const length = 0x110000; let textEncoder = new TextEncoder(); - let textDecoder = new TextDecoder(); + let textDecoder = new TextDecoder("utf-8", { ignoreBOM: true }); let encodeOut = new Uint8Array(length * 4); let encodeIntoOut = new Uint8Array(length * 4); let encodeIntoBuffer = new Uint8Array(4); |