1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-26 16:09:27 -05:00

add writeFileStr and update documentation (denoland/deno_std#340)

Original: 191e53a78b
This commit is contained in:
Axetroy 2019-04-15 04:14:57 +08:00 committed by Ryan Dahl
parent d2d5b6ac8e
commit 7336800658
5 changed files with 101 additions and 4 deletions

View file

@ -134,7 +134,7 @@ for (const fileInfo of walk()) {
}
```
### writejson
### writeJson
Writes an object to a JSON file.
@ -147,6 +147,34 @@ Writes an object to a JSON file.
import { writeJson, writeJsonSync } from "https://deno.land/std/fs/mod.ts";
writeJson("./target.dat", { foo: "bar" }, { spaces: 2 }); // returns a promise
writeJsonSync("./target.dat", { foo: "bar" }, { replacer: ["foo"] });
// void
writeJsonSync("./target.dat", { foo: "bar" }, { replacer: ["foo"] }); // void
```
### readFileStr
Read file and output it as a string.
**ReadOptions**
- encoding : The encoding to read file. lowercased.
```ts
import { readFileStr, readFileStrSync } from "https://deno.land/std/fs/mod.ts";
readFileStr("./target.dat", { encoding: "utf8" }); // returns a promise
readFileStrSync("./target.dat", { encoding: "utf8" }); // void
```
### writeFileStr
Write the string to file.
```ts
import {
writeFileStr,
writeFileStrSync
} from "https://deno.land/std/fs/mod.ts";
writeFileStr("./target.dat", "file content"); // returns a promise
writeFileStrSync("./target.dat", "file content"); // void
```

View file

@ -6,6 +6,8 @@ export * from "./exists.ts";
export * from "./glob.ts";
export * from "./globrex.ts";
export * from "./move.ts";
export * from "./read_file_str.ts";
export * from "./write_file_str.ts";
export * from "./read_json.ts";
export * from "./write_json.ts";
export * from "./walk.ts";

View file

@ -10,6 +10,7 @@ import "./ensure_dir_test.ts";
import "./ensure_file_test.ts";
import "./move_test.ts";
import "./read_json_test.ts";
import "./read_file_str_test.ts";
import "./write_json_test.ts";
import "./read_file_str_test.ts";
import "./write_file_str_test.ts";
import "./utils_test.ts";

28
fs/write_file_str.ts Normal file
View file

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

38
fs/write_file_str_test.ts Normal file
View file

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