mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-30 17:48:50 +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 {
|
interface ReadFileOptions {
|
||||||
/** Base directory for `path` */
|
/** Base directory for `path` */
|
||||||
baseDir?: BaseDirectory
|
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
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
|
* import { readTextFile, BaseDirectory } from '@tauri-apps/plugin-fs';
|
||||||
@@ -777,11 +779,11 @@ async function readTextFile(
|
|||||||
|
|
||||||
const bytes = arr instanceof ArrayBuffer ? arr : Uint8Array.from(arr)
|
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
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* import { readTextFileLines, BaseDirectory } from '@tauri-apps/plugin-fs';
|
* import { readTextFileLines, BaseDirectory } from '@tauri-apps/plugin-fs';
|
||||||
@@ -838,7 +840,7 @@ async function readTextFileLines(
|
|||||||
return { value: null, done }
|
return { value: null, done }
|
||||||
}
|
}
|
||||||
|
|
||||||
const line = new TextDecoder().decode(
|
const line = new TextDecoder(options?.encoding ?? 'utf-8').decode(
|
||||||
bytes.slice(0, bytes.byteLength - 1)
|
bytes.slice(0, bytes.byteLength - 1)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user