mirror of
https://github.com/denoland/deno.git
synced 2025-01-05 05:49:20 -05:00
fix(op_crates/web): make TextEncoder work with forced non-strings (#8206)
Fixes: #8201
This commit is contained in:
parent
d9b8778c45
commit
9397cf508e
2 changed files with 15 additions and 0 deletions
|
@ -210,3 +210,17 @@ unitTest(function toStringShouldBeWebCompatibility(): void {
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
assertEquals(decoder.toString(), "[object 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);
|
||||||
|
});
|
||||||
|
|
|
@ -1061,6 +1061,7 @@
|
||||||
class TextEncoder {
|
class TextEncoder {
|
||||||
encoding = "utf-8";
|
encoding = "utf-8";
|
||||||
encode(input = "") {
|
encode(input = "") {
|
||||||
|
input = String(input);
|
||||||
// Deno.core.encode() provides very efficient utf-8 encoding
|
// Deno.core.encode() provides very efficient utf-8 encoding
|
||||||
if (this.encoding === "utf-8") {
|
if (this.encoding === "utf-8") {
|
||||||
return core.encode(input);
|
return core.encode(input);
|
||||||
|
|
Loading…
Reference in a new issue