Skip to content

Commit e97a4de

Browse files
authored
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
1 parent 05045f9 commit e97a4de

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

.changes/change-pr-3244.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"fs": minor
3+
"fs-js": minor
4+
---
5+
6+
Add `encoding` option for `readTextFile` and `readTextFileLines`

plugins/fs/api-iife.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/fs/guest-js/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,8 @@ async function readDir(
723723
interface ReadFileOptions {
724724
/** Base directory for `path` */
725725
baseDir?: BaseDirectory
726+
/** Text encoding to use when reading a text file. Defaults to 'utf-8'. */
727+
encoding?: string
726728
}
727729

728730
/**
@@ -753,7 +755,7 @@ async function readFile(
753755
}
754756

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

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

780-
return new TextDecoder().decode(bytes)
782+
return new TextDecoder(options?.encoding ?? 'utf-8').decode(bytes)
781783
}
782784

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

841-
const line = new TextDecoder().decode(
843+
const line = new TextDecoder(options?.encoding ?? 'utf-8').decode(
842844
bytes.slice(0, bytes.byteLength - 1)
843845
)
844846

0 commit comments

Comments
 (0)