-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
|
||
export interface ReadOptions { | ||
encoding?: string; | ||
} | ||
|
||
/** | ||
* Read file synchronously and output it as a string. | ||
* | ||
* @param filename File to read | ||
* @param opts Read options | ||
*/ | ||
export function readFileStrSync( | ||
filename: string, | ||
opts: ReadOptions = {} | ||
): string { | ||
const decoder = new TextDecoder(opts.encoding); | ||
return decoder.decode(Deno.readFileSync(filename)); | ||
} | ||
|
||
/** | ||
* Read file and output it as a string. | ||
* | ||
* @param filename File to read | ||
* @param opts Read options | ||
*/ | ||
export async function readFileStr( | ||
filename: string, | ||
opts: ReadOptions = {} | ||
): Promise<string> { | ||
const decoder = new TextDecoder(opts.encoding); | ||
return decoder.decode(await Deno.readFile(filename)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { test } from "../testing/mod.ts"; | ||
import { assert } from "../testing/asserts.ts"; | ||
import { readFileStrSync, readFileStr } from "./read_file_str.ts"; | ||
import * as path from "./path/mod.ts"; | ||
|
||
const testdataDir = path.resolve("fs", "testdata"); | ||
|
||
test(function testReadFileSync() { | ||
const jsonFile = path.join(testdataDir, "json_valid_obj.json"); | ||
const strFile = readFileStrSync(jsonFile); | ||
assert(typeof strFile === "string"); | ||
assert(strFile.length > 0); | ||
}); | ||
|
||
test(async function testReadFile() { | ||
const jsonFile = path.join(testdataDir, "json_valid_obj.json"); | ||
const strFile = await readFileStr(jsonFile); | ||
assert(typeof strFile === "string"); | ||
assert(strFile.length > 0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters