mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 00:21:05 -05:00
std/node fs.readFile should take string as option (#5316)
This commit is contained in:
parent
b60cde4c0a
commit
524b1547b7
2 changed files with 22 additions and 2 deletions
|
@ -24,7 +24,7 @@ function maybeDecode(
|
|||
|
||||
export function readFile(
|
||||
path: string | URL,
|
||||
optOrCallback: ReadFileCallback | FileOptions,
|
||||
optOrCallback: ReadFileCallback | FileOptions | string,
|
||||
callback?: ReadFileCallback
|
||||
): void {
|
||||
path = path instanceof URL ? fromFileUrl(path) : path;
|
||||
|
@ -47,7 +47,7 @@ export function readFile(
|
|||
|
||||
export function readFileSync(
|
||||
path: string | URL,
|
||||
opt?: FileOptions
|
||||
opt?: FileOptions | string
|
||||
): string | Uint8Array {
|
||||
path = path instanceof URL ? fromFileUrl(path) : path;
|
||||
return maybeDecode(denoReadFileSync(path), getEncoding(opt));
|
||||
|
|
|
@ -35,6 +35,20 @@ test("readFileEncodeUtf8Success", async function () {
|
|||
assertEquals(data as string, "hello world");
|
||||
});
|
||||
|
||||
test("readFileEncodingAsString", async function () {
|
||||
const data = await new Promise((res, rej) => {
|
||||
readFile(testData, "utf8", (err, data) => {
|
||||
if (err) {
|
||||
rej(err);
|
||||
}
|
||||
res(data);
|
||||
});
|
||||
});
|
||||
|
||||
assertEquals(typeof data, "string");
|
||||
assertEquals(data as string, "hello world");
|
||||
});
|
||||
|
||||
test("readFileSyncSuccess", function () {
|
||||
const data = readFileSync(testData);
|
||||
assert(data instanceof Uint8Array);
|
||||
|
@ -46,3 +60,9 @@ test("readFileEncodeUtf8Success", function () {
|
|||
assertEquals(typeof data, "string");
|
||||
assertEquals(data as string, "hello world");
|
||||
});
|
||||
|
||||
test("readFileEncodeAsString", function () {
|
||||
const data = readFileSync(testData, "utf8");
|
||||
assertEquals(typeof data, "string");
|
||||
assertEquals(data as string, "hello world");
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue