diff options
author | Bartek Iwańczuk <biwanczuk@gmail.com> | 2020-03-15 15:31:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-15 15:31:55 +0100 |
commit | dc6e0c3591709d6f8887bb672af1de54dfc8a974 (patch) | |
tree | 0239d62e419b840f1c4e5cd631a7b87689ed2a3b /core/encode_decode_test.js | |
parent | ec3f44581bf4312cbbe36b71daca7f0474177cf3 (diff) |
feat: Deno.core.{encode,decode}; standalone UTF-8 encoding/decoding (#4349)
This commits add two new methods to "Deno.core" namespace: "encode" and "decode".
Those methods are bound in Rust to provide a) fast b) generally available of encoding and decoding UTF-8 strings.
Both methods are now used in "cli/js/dispatch_json.ts".
Diffstat (limited to 'core/encode_decode_test.js')
-rw-r--r-- | core/encode_decode_test.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/core/encode_decode_test.js b/core/encode_decode_test.js new file mode 100644 index 000000000..8a366dd66 --- /dev/null +++ b/core/encode_decode_test.js @@ -0,0 +1,40 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. + +function assertArrayEquals(a1, a2) { + if (a1.length !== a2.length) throw Error("assert"); + + for (const index in a1) { + if (a1[index] !== a2[index]) { + throw Error("assert"); + } + } +} + +function main() { + // prettier-ignore + const fixture1 = [ + 0xf0, 0x9d, 0x93, 0xbd, + 0xf0, 0x9d, 0x93, 0xae, + 0xf0, 0x9d, 0x94, 0x81, + 0xf0, 0x9d, 0x93, 0xbd + ]; + // prettier-ignore + const fixture2 = [ + 72, 101, 108, 108, + 111, 32, 239, 191, + 189, 239, 191, 189, + 32, 87, 111, 114, + 108, 100 + ]; + + assertArrayEquals(Array.from(Deno.core.encode("𝓽𝓮𝔁𝓽")), fixture1); + assertArrayEquals( + Array.from(Deno.core.encode("Hello \udc12\ud834 World")), + fixture2 + ); + + assert(Deno.core.decode(new Uint8Array(fixture1)) === "𝓽𝓮𝔁𝓽"); + assert(Deno.core.decode(new Uint8Array(fixture2)) === "Hello �� World"); +} + +main(); |