1
0
Fork 0
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:
Marcos Casagrande 2020-05-14 13:04:07 +02:00 committed by GitHub
parent b60cde4c0a
commit 524b1547b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View file

@ -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));

View file

@ -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");
});