summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/tests/unit/text_encoding_test.ts14
-rw-r--r--op_crates/web/08_text_encoding.js1
2 files changed, 15 insertions, 0 deletions
diff --git a/cli/tests/unit/text_encoding_test.ts b/cli/tests/unit/text_encoding_test.ts
index 4c5606a4f..189d391c5 100644
--- a/cli/tests/unit/text_encoding_test.ts
+++ b/cli/tests/unit/text_encoding_test.ts
@@ -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);
+});
diff --git a/op_crates/web/08_text_encoding.js b/op_crates/web/08_text_encoding.js
index d93319758..13e256982 100644
--- a/op_crates/web/08_text_encoding.js
+++ b/op_crates/web/08_text_encoding.js
@@ -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);