Skip to content

Commit

Permalink
fix(op_crates/web): make TextEncoder work with forced non-strings (de…
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamingr authored and jannes committed Dec 1, 2020
1 parent 6e1a2eb commit 8fe643e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cli/tests/unit/text_encoding_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
1 change: 1 addition & 0 deletions op_crates/web/08_text_encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8fe643e

Please sign in to comment.