rescript-webapi icon indicating copy to clipboard operation
rescript-webapi copied to clipboard

add bidnings to TextEncoder/TextDecoder

Open cometkim opened this issue 3 years ago • 4 comments

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.

cometkim avatar Dec 31 '21 00:12 cometkim

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"

TheSpyder avatar Dec 31 '21 02:12 TheSpyder

Thanks, @TheSpyder

TIL a binding pattern!

cometkim avatar Jan 01 '22 14:01 cometkim

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

TheSpyder avatar Jan 03 '22 11:01 TheSpyder

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.

cometkim avatar Jan 08 '22 23:01 cometkim