1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 12:58:54 -05:00

fix(op_crates/web): make TextEncoder work with forced non-strings (#8206)

Fixes: #8201
This commit is contained in:
Benjamin Gruenbaum 2020-11-02 01:57:18 +02:00 committed by GitHub
parent d9b8778c45
commit 9397cf508e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View file

@ -210,3 +210,17 @@ unitTest(function toStringShouldBeWebCompatibility(): void {
const decoder = new TextDecoder();
assertEquals(decoder.toString(), "[object TextDecoder]");
});
unitTest(function textEncoderShouldCoerceToString(): void {
const encoder = new TextEncoder();
const fixutreText = "text";
const fixture = {
toString() {
return fixutreText;
},
};
const bytes = encoder.encode(fixture as unknown as string);
const decoder = new TextDecoder();
const decoded = decoder.decode(bytes);
assertEquals(decoded, fixutreText);
});

View file

@ -1061,6 +1061,7 @@
class TextEncoder {
encoding = "utf-8";
encode(input = "") {
input = String(input);
// Deno.core.encode() provides very efficient utf-8 encoding
if (this.encoding === "utf-8") {
return core.encode(input);