Skip to content
Permalink
Browse files

add writeFileStr and update documentation (#340)

  • Loading branch information...
axetroy authored and ry committed Apr 14, 2019
1 parent 95ab4e2 commit 191e53a78bfc96d32df6d01affa90f938b92e6e5
Showing with 101 additions and 4 deletions.
  1. +31 −3 fs/README.md
  2. +2 −0 fs/mod.ts
  3. +2 −1 fs/test.ts
  4. +28 −0 fs/write_file_str.ts
  5. +38 −0 fs/write_file_str_test.ts
@@ -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
```
@@ -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";
@@ -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";
@@ -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));
}
@@ -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);
});

0 comments on commit 191e53a

Please sign in to comment.
You can’t perform that action at this time.