-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for reading/writing with multiple encodings #441
Comments
This may be a duplicate of #269 now. I'm biased, but I'd vouch for the 2 positional parameters and an optional third "options" parameter that contains the For write, I feel like we have the option of "auto-detecting" the encoding. I.e. if a string is passed, we assume UTF8. So, personally I'd be in favor of these types: fs.read(path: FilePath): Promise<Uint8Array>
fs.read(path: FilePath, options: { encoding: "utf8" }): Promise<string>
fs.write(path: FilePath, bytes: Uint8Array)
fs.write(path: FilePath, text: string, options: { encoding: "utf8" }) |
Oh whoops, forgot about that issue. fs.write(path: FilePath, bytes: Uint8Array)
fs.write(path: FilePath, text: string, options: { encoding: "utf8" }) That works too yeah, I have no strong preference.
Yes exactly, that's what I was trying to achieve as well. I assumed it'd be easier to do in Typescript with my syntax, rather than yours, but I could be wrong. But yeah, your syntax is more familiar. |
You are correct about that version being harder, but it's still possible. Here's how node.js defines the Essentially it'd boil down to function type overloading. Something like this in our case: export function read(path: FilePath): Promise<Uint8Array>;
export function read(path: FilePath, options: { encoding: "utf8" }): Promise<string>;
export function read(path: FilePath, options?: { encoding?: string }): Promise<Uint8Array | string> {
// impl
} |
Oh you can do function overloading in Typescript?! How did I not know this 😲 |
With Webnative 0.35 you can only read and write
Uint8Array
s.There should be support for other encoding, such as UTF8 strings.
@matheus23 suggested the
encoding
option from https://nodejs.org/api/fs.html#filehandlereadfileoptionsWe could do something like:
The text was updated successfully, but these errors were encountered: