forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add writeFileStr and update documentation (denoland/std#340)
Original: denoland/std@191e53a
- Loading branch information
Showing
5 changed files
with
101 additions
and
4 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
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
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
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,28 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** | ||
* Write the string to file synchronously. | ||
* | ||
* @param filename File to write | ||
* @param content The content write to file | ||
* @returns void | ||
*/ | ||
export function writeFileStrSync(filename: string, content: string): void { | ||
const encoder = new TextEncoder(); | ||
Deno.writeFileSync(filename, encoder.encode(content)); | ||
} | ||
|
||
/** | ||
* Write the string to file. | ||
* | ||
* @param filename File to write | ||
* @param content The content write to file | ||
* @returns Promise<void> | ||
*/ | ||
export async function writeFileStr( | ||
filename: string, | ||
content: string | ||
): Promise<void> { | ||
const encoder = new TextEncoder(); | ||
await Deno.writeFile(filename, encoder.encode(content)); | ||
} |
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,38 @@ | ||
import { test } from "../testing/mod.ts"; | ||
import { assertEquals } from "../testing/asserts.ts"; | ||
import { writeFileStr, writeFileStrSync } from "./write_file_str.ts"; | ||
import * as path from "./path/mod.ts"; | ||
|
||
const testdataDir = path.resolve("fs", "testdata"); | ||
|
||
test(function testReadFileSync() { | ||
const jsonFile = path.join(testdataDir, "write_file_1.json"); | ||
const content = "write_file_str_test"; | ||
writeFileStrSync(jsonFile, content); | ||
|
||
// make sure file have been create. | ||
Deno.statSync(jsonFile); | ||
|
||
const result = new TextDecoder().decode(Deno.readFileSync(jsonFile)); | ||
|
||
// remove test file | ||
Deno.removeSync(jsonFile); | ||
|
||
assertEquals(content, result); | ||
}); | ||
|
||
test(async function testReadFile() { | ||
const jsonFile = path.join(testdataDir, "write_file_2.json"); | ||
const content = "write_file_str_test"; | ||
await writeFileStr(jsonFile, content); | ||
|
||
// make sure file have been create. | ||
await Deno.stat(jsonFile); | ||
|
||
const result = new TextDecoder().decode(await Deno.readFile(jsonFile)); | ||
|
||
// remove test file | ||
await Deno.remove(jsonFile); | ||
|
||
assertEquals(content, result); | ||
}); |