rescript-webapi
rescript-webapi copied to clipboard
add bidnings to TextEncoder/TextDecoder
Both are available on browsers, Node.js, and Deno.
In most cases, it is used without arguments
// UTF-8 encoding/decoding
let e = TextEncoder.make()
e->TextEncoder.encoding // "utf-8"
let bin = e->TextEncoder.encode("Hello, World!")
let d = TextDecoder.make()
d->TextDecoder.decode(bin) // "Hello, World!"
I added some additional bindings to support the full specification.
Looks like a good start! Since this is creating a new type rather than reusing something from ReScript, feel free to use the records-as-objects capability of ReScript instead of @get methods:
type t = pri {
encoding: string,
fatal: bool,
ignoreBOM: bool
}
Then your code can look like this:
let e = TextEncoder.make()
e.encoding // "utf-8"
Thanks, @TheSpyder
TIL a binding pattern!
Haha yeah it's not obvious but a really nice ergonomic way to do bindings.
So, two more things before we can merge:
- Add something to the test folder. These files aren't tests we run, they just validate that the correct JS is generated (the JS is checked in to help with this).
- Changelog entry
There is also TextDecoderStream API in the web, which depends on ReadableStream and WritableStream interface.
Since the WritableStream binding is still empty, I leave it to do.