feat(fs): #3243 add encoding option to readTextFile functions (#3244)

* feat(fs): #3243 add encoding option to readFile and readTextFile functions

* feat(fs): add encoding option to ReadFileOptions and update readTextFileLines functions

* add change file
This commit is contained in:
Seiji Okuda
2026-02-09 23:08:26 +09:00
committed by GitHub
parent 05045f9a72
commit e97a4dedab
3 changed files with 13 additions and 5 deletions
+6
View File
@@ -0,0 +1,6 @@
---
"fs": minor
"fs-js": minor
---
Add `encoding` option for `readTextFile` and `readTextFileLines`
File diff suppressed because one or more lines are too long
+6 -4
View File
@@ -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
}
/**
@@ -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';
@@ -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';
@@ -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)
)