diff --git a/CHANGELOG.md b/CHANGELOG.md index 0085574e..333ceb52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ * `DataTransfer` bindings (#51) * `WorkerGlobalScope`, `WindowOrWorkerGlobalScope`, `WorkerNavigator`, and `WorkerLocation` bindings (#57) * `Response` constructors to `Fetch` bindings (#64) +* `TextEncoder` and `TextDecoder` bindings (#78) ### Fixed * `ofElement` was incorrectly returning `Dom.htmlElement` type instead of the enclosing element type (#60) diff --git a/lib/js/tests/Webapi/Webapi__TextDecoder__test.js b/lib/js/tests/Webapi/Webapi__TextDecoder__test.js new file mode 100644 index 00000000..6e91f297 --- /dev/null +++ b/lib/js/tests/Webapi/Webapi__TextDecoder__test.js @@ -0,0 +1,24 @@ +'use strict'; + +var Webapi__TextDecoder = require("../../src/Webapi/Webapi__TextDecoder.js"); + +var decoder1 = new TextDecoder(); + +var decoder2 = Webapi__TextDecoder.makeWithOptions(undefined, true, true, undefined); + +var message = new Uint8Array([ + 72, + 101, + 108, + 108, + 111 + ]); + +console.log(decoder1.decode(message)); + +console.log(Webapi__TextDecoder.decodeStream(decoder1, message)); + +exports.decoder1 = decoder1; +exports.decoder2 = decoder2; +exports.message = message; +/* decoder1 Not a pure module */ diff --git a/lib/js/tests/Webapi/Webapi__TextEncoder__test.js b/lib/js/tests/Webapi/Webapi__TextEncoder__test.js new file mode 100644 index 00000000..03c90d75 --- /dev/null +++ b/lib/js/tests/Webapi/Webapi__TextEncoder__test.js @@ -0,0 +1,19 @@ +'use strict'; + + +var encoder = new TextEncoder(); + +console.log(encoder.encoding); + +console.log(encoder.encode("Hello")); + +var buffer = new Uint8Array([]); + +var result = encoder.encodeInto("Hello", buffer); + +console.log(result.read, result.written); + +exports.encoder = encoder; +exports.buffer = buffer; +exports.result = result; +/* encoder Not a pure module */ diff --git a/src/Webapi.res b/src/Webapi.res index f79b3f44..7a0aa9fb 100644 --- a/src/Webapi.res +++ b/src/Webapi.res @@ -6,6 +6,8 @@ module File = Webapi__File module FileList = Webapi__FileList module Fetch = Webapi__Fetch module FormData = Webapi__FormData +module TextEncoder = Webapi__TextEncoder +module TextDecoder = Webapi__TextDecoder module Navigator = Webapi__Navigator diff --git a/src/Webapi/Webapi__TextDecoder.res b/src/Webapi/Webapi__TextDecoder.res new file mode 100644 index 00000000..9ab2d5e2 --- /dev/null +++ b/src/Webapi/Webapi__TextDecoder.res @@ -0,0 +1,35 @@ +type t = { + encoding: string, + fatal: bool, + ignoreBOM: bool, +} + +type decoderOptions + +type decodeOptions + +%%private( + @new external _makeWithOptions: (string, decoderOptions) => t = "TextDecoder" + + @obj + external makeDecoderOptions: ( + ~fatal: option=?, + ~ignoreBOM: option=?, + unit, + ) => decoderOptions = "" +) + +@new external make: unit => t = "TextDecoder" + +let makeWithOptions = (~encoding="utf-8", ~fatal=?, ~ignoreBOM=?, ()) => + _makeWithOptions(encoding, makeDecoderOptions(~fatal, ~ignoreBOM, ())) + +%%private( + @obj external makeDecodeOptions: (~stream: bool) => decodeOptions = "" + + @send external _decodeWithOptions: (t, Js.TypedArray2.Uint8Array.t, decodeOptions) => t = "decode" +) + +@send external decode: (t, Js.TypedArray2.Uint8Array.t) => string = "decode" + +let decodeStream = (t, array) => _decodeWithOptions(t, array, makeDecodeOptions(~stream=true)) diff --git a/src/Webapi/Webapi__TextEncoder.res b/src/Webapi/Webapi__TextEncoder.res new file mode 100644 index 00000000..950dc876 --- /dev/null +++ b/src/Webapi/Webapi__TextEncoder.res @@ -0,0 +1,16 @@ +type t = { + // This always returns "utf-8" + encoding: string, +} + +@new external make: unit => t = "TextEncoder" + +@send external encode: (t, string) => Js.TypedArray2.Uint8Array.t = "encode" + +type encodeIntoResult = { + read: int, + written: int, +} + +@send +external encodeInto: (t, string, Js.TypedArray2.Uint8Array.t) => encodeIntoResult = "encodeInto" diff --git a/tests/Webapi/Webapi__TextDecoder__test.res b/tests/Webapi/Webapi__TextDecoder__test.res new file mode 100644 index 00000000..86a41aa0 --- /dev/null +++ b/tests/Webapi/Webapi__TextDecoder__test.res @@ -0,0 +1,9 @@ +open Js.TypedArray2 +open Webapi.TextDecoder + +let decoder1 = make() +let decoder2 = makeWithOptions(~fatal=true, ~ignoreBOM=true, ()) + +let message = Uint8Array.make([72, 101, 108, 108, 111]) +decoder1->decode(message)->Js.log +decoder1->decodeStream(message)->Js.log diff --git a/tests/Webapi/Webapi__TextEncoder__test.res b/tests/Webapi/Webapi__TextEncoder__test.res new file mode 100644 index 00000000..a607a829 --- /dev/null +++ b/tests/Webapi/Webapi__TextEncoder__test.res @@ -0,0 +1,12 @@ +open Js.TypedArray2 +open Webapi.TextEncoder + +let encoder = make() + +Js.log(encoder.encoding) + +encoder->encode("Hello")->Js.log + +let buffer = Uint8Array.make([]) +let result = encoder->encodeInto("Hello", buffer) +Js.log2(result.read, result.written)