Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/change-pr-3244.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"fs": minor
"fs-js": minor
---

Add `encoding` option for `readTextFile` and `readTextFileLines`
2 changes: 1 addition & 1 deletion plugins/fs/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions plugins/fs/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ async function readDir(
interface ReadFileOptions {
/** Base directory for `path` */
baseDir?: BaseDirectory
/** Text encoding to use when reading a text file. Defaults to 'utf-8'. */
encoding?: string
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing documentation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amrbashir
Thanks for the review!
Added documentation comments.

}

/**
Expand Down Expand Up @@ -753,7 +755,7 @@ async function readFile(
}

/**
* Reads and returns the entire contents of a file as UTF-8 string.
* Reads and returns the entire contents of a file as a string using the specified encoding (default: UTF-8).
* @example
* ```typescript
* import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
Expand All @@ -777,11 +779,11 @@ async function readTextFile(

const bytes = arr instanceof ArrayBuffer ? arr : Uint8Array.from(arr)

return new TextDecoder().decode(bytes)
return new TextDecoder(options?.encoding ?? 'utf-8').decode(bytes)
}

/**
* Returns an async {@linkcode AsyncIterableIterator} over the lines of a file as UTF-8 string.
* Returns an async {@linkcode AsyncIterableIterator} over the lines of a file, decoded using the specified encoding (default: UTF-8).
* @example
* ```typescript
* import { readTextFileLines, BaseDirectory } from '@tauri-apps/plugin-fs';
Expand Down Expand Up @@ -838,7 +840,7 @@ async function readTextFileLines(
return { value: null, done }
}

const line = new TextDecoder().decode(
const line = new TextDecoder(options?.encoding ?? 'utf-8').decode(
bytes.slice(0, bytes.byteLength - 1)
)

Expand Down
Loading