mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-21 11:26:15 +02:00
* 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:
@@ -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
@@ -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)
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user