Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add writeFileStr and update documentation #340

Merged
merged 3 commits into from
Apr 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions fs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ for (const fileInfo of walk()) {
}
```

### writejson
### writeJson

Writes an object to a JSON file.

Expand All @@ -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
```
2 changes: 2 additions & 0 deletions fs/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 2 additions & 1 deletion fs/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 28 additions & 0 deletions fs/write_file_str.ts
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));
}
38 changes: 38 additions & 0 deletions fs/write_file_str_test.ts
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);
});