add bidnings to TextEncoder/TextDecoder#78
Conversation
|
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 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:
|
|
There is also Since the |
TheSpyder
left a comment
There was a problem hiding this comment.
Apologies for not picking up on the other thing that needs changing when I reviewed last week.
| %%private( | ||
| @new external _makeWithOptions: (string, decoderOptions) => t = "TextDecoder" | ||
|
|
||
| @obj | ||
| external makeDecoderOptions: ( | ||
| ~fatal: option<bool>=?, | ||
| ~ignoreBOM: option<bool>=?, | ||
| unit, | ||
| ) => decoderOptions = "" | ||
| ) | ||
|
|
||
| @new external make: unit => t = "TextDecoder" | ||
|
|
||
| let makeWithOptions = (~encoding="utf-8", ~fatal=?, ~ignoreBOM=?, ()) => | ||
| _makeWithOptions(encoding, makeDecoderOptions(~fatal, ~ignoreBOM, ())) |
There was a problem hiding this comment.
While I appreciate the sentiment to make the bindings more usable, we want these bindings to be zero-cost as much as possible. In this case ReScript 10 should have a new feature that makes makeDecoderOptions unnecessary, but it will be easier to switch to the upcoming style if the exported API takes a decoderOptions.
Please remove the private wrappers and export the more difficult to use way for now; here and below.
| %%private( | |
| @new external _makeWithOptions: (string, decoderOptions) => t = "TextDecoder" | |
| @obj | |
| external makeDecoderOptions: ( | |
| ~fatal: option<bool>=?, | |
| ~ignoreBOM: option<bool>=?, | |
| unit, | |
| ) => decoderOptions = "" | |
| ) | |
| @new external make: unit => t = "TextDecoder" | |
| let makeWithOptions = (~encoding="utf-8", ~fatal=?, ~ignoreBOM=?, ()) => | |
| _makeWithOptions(encoding, makeDecoderOptions(~fatal, ~ignoreBOM, ())) | |
| @obj | |
| external makeDecoderOptions: ( | |
| ~fatal: option<bool>=?, | |
| ~ignoreBOM: option<bool>=?, | |
| unit, | |
| ) => decoderOptions = "" | |
| @new external make: unit => t = "TextDecoder" | |
| @new external makeWithOptions: (string, decoderOptions) => t = "TextDecoder" |
There was a problem hiding this comment.
You could still have a name argument for the encoding helps people to know that that parameter is supposed to be:
@new external makeWithOptions: (~encoding: string, decoderOptions) => t = "TextDecoder"
fe0f2e9 to
7f2cf0a
Compare
Both are available on browsers, Node.js, and Deno.
In most cases, it is used without arguments
I added some additional bindings to support the full specification.