1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-26 16:09:27 -05:00
denoland-deno/std/io
Yusuke Tanaka 9029003046
build: update dlint to v0.2.10 (#8284)
Update prebuilt "dlint" binary to v0.2.10 and fix diagnostics
for "require-await" rule.

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
2020-11-14 20:27:37 +01:00
..
_iotest.ts chore: add copyright (#7593) 2020-09-21 08:26:41 -04:00
bufio.ts build: update dlint to v0.2.10 (#8284) 2020-11-14 20:27:37 +01:00
bufio_test.ts fix(std/io): remove trivial internal util.ts module (#8032) 2020-10-20 13:51:57 +02:00
ioutil.ts Use dprint for internal formatting (#6682) 2020-07-14 15:24:17 -04:00
ioutil_test.ts test(std/io): use a real tempdir (#8019) 2020-10-18 12:16:26 -04:00
mod.ts chore: add copyright (#7593) 2020-09-21 08:26:41 -04:00
readers.ts Remove unused property of StringReader (#6743) 2020-07-14 14:21:08 -04:00
readers_test.ts chore: add copyright (#7593) 2020-09-21 08:26:41 -04:00
README.md docs(std): version all imports in README (#7442) 2020-10-04 14:18:36 +02:00
streams.ts docs(std): add some missing JSDoc (#7765) 2020-11-06 13:33:59 +11:00
streams_test.ts feat(fmt): Sort named import and export specifiers (#7711) 2020-09-27 12:22:32 +02:00
test.ts chore: add copyright (#7593) 2020-09-21 08:26:41 -04:00
writers.ts fix(std/io): BufWriter/StringWriter bug (#6247) 2020-06-12 15:15:29 -04:00
writers_test.ts chore: add copyright (#7593) 2020-09-21 08:26:41 -04:00

std/io

Bufio

Uses:

readLines

Read reader[like file], line by line:

import { readLines } from "https://deno.land/std@$STD_VERSION/io/mod.ts";
import * as path from "https://deno.land/std@$STD_VERSION/path/mod.ts";

const filename = path.join(Deno.cwd(), "std/io/README.md");
let fileReader = await Deno.open(filename);

for await (let line of readLines(fileReader)) {
  console.log(line);
}

Output:

# std/io

## readLines

```ts
import * as path from "https://deno.land/std@$STD_VERSION/path/mod.ts";

## Rest of the file

readStringDelim

Read reader[like file] chunk by chunk, splitting based on delimiter.

import { readStringDelim } from "https://deno.land/std@$STD_VERSION/io/mod.ts";
import * as path from "https://deno.land/std@$STD_VERSION/path/mod.ts";

const filename = path.join(Deno.cwd(), "std/io/README.md");
let fileReader = await Deno.open(filename);

for await (let line of readStringDelim(fileReader, "\n")) {
  console.log(line);
}

Output:

# std/io

## readLines

```ts
import * as path from "https://deno.land/std@$STD_VERSION/path/mod.ts";

## Rest of the file

Reader

StringReader

Create a Reader object for string.

import { StringReader } from "https://deno.land/std@$STD_VERSION/io/mod.ts";

const data = new Uint8Array(6);
const r = new StringReader("abcdef");
const res0 = await r.read(data);
const res1 = await r.read(new Uint8Array(6));

// Number of bytes read
console.log(res0); // 6
console.log(res1); // null, no byte left to read. EOL

// text

console.log(new TextDecoder().decode(data)); // abcdef

Output:

6
null
abcdef

Writer

StringWriter

Create a Writer object for string.

import {
  copyN,
  StringReader,
  StringWriter,
} from "https://deno.land/std@$STD_VERSION/io/mod.ts";

const w = new StringWriter("base");
const r = new StringReader("0123456789");
await copyN(r, w, 4); // copy 4 bytes

// Number of bytes read
console.log(w.toString()); //base0123

await Deno.copy(r, w); // copy all
console.log(w.toString()); // base0123456789

Output:

base0123
base0123456789

Streams

fromStreamReader

Creates a Reader from a ReadableStreamDefaultReader.

import { fromStreamReader } from "https://deno.land/std@$STD_VERSION/io/mod.ts";
const res = await fetch("https://deno.land");
const file = await Deno.open("./deno.land.html", { create: true, write: true });

const reader = fromStreamReader(res.body!.getReader());
await Deno.copy(reader, file);
file.close();

fromStreamWriter

Creates a Writer from a WritableStreamDefaultWriter.

import { fromStreamWriter } from "https://deno.land/std@$STD_VERSION/io/mod.ts";
const file = await Deno.open("./deno.land.html", { read: true });

const writableStream = new WritableStream({
  write(chunk): void {
    console.log(chunk);
  },
});
const writer = fromStreamWriter(writableStream.getWriter());
await Deno.copy(file, writer);
file.close();