Skip to content

Commit

Permalink
Restructuring v1.0.0
Browse files Browse the repository at this point in the history
Renamed the main file. Decided to follow @ry's suggestion in
denoland/deno#2101 (comment)
Added two extra examples. Decided not to open and close files for the user,
but instead use a Reader.
  • Loading branch information
johnsonjo4531 committed Apr 13, 2019
1 parent 54b498f commit 993bb52
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 78 deletions.
21 changes: 13 additions & 8 deletions example.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import lines from "./read_lines.ts";
import { linesBytes } from "./lines.ts";
import concatBytes from "./concatBytes.ts";

async function cat(filenames: string[]): Promise<void> {
const newlinebuffer = new TextEncoder().encode("\r\n");
const newlinebuffer = new TextEncoder().encode("\n");
for (let filename of filenames) {
const file_lines: Uint8Array[] = [];
for await (const line of lines(filename)) {
// you could transform the line buffers here
file_lines.push(line);
file_lines.push(newlinebuffer);
const file = await Deno.open(filename);
try {
const file_lines: Uint8Array[] = [];
for await (const line of linesBytes(file)) {
// you could transform the line buffers here
file_lines.push(line);
file_lines.push(newlinebuffer);
}
Deno.stdout.write(concatBytes(...file_lines));
} finally {
file.close();
}
Deno.stdout.write(concatBytes(...file_lines));
}
}

Expand Down
20 changes: 20 additions & 0 deletions example2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { lines } from "./lines.ts";

async function cat(filenames: string[]): Promise<void> {
const newlinebuffer = new TextEncoder().encode("\n");
for (let filename of filenames) {
const file = await Deno.open(filename);
try {
let fileStr = "";
for await (const line of lines(file)) {
// you could transform the line buffers here
fileStr += line + "\n";
}
console.log(fileStr);
} finally {
file.close();
}
}
}

cat(Deno.args.slice(1));
31 changes: 31 additions & 0 deletions example_input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { lines } from "./lines.ts";

/**
* Returns a python like input reader.
*/
function inputReader(r: Deno.Reader) {
const lineReader = lines(r);
/**
* Python like input reader. Returns an array containing at the first index
* the line read and at the second index a boolean indicating whether the eof
* has been reached.
*/
return async function input(output: string) {
if (output) {
Deno.stdout.write(new TextEncoder().encode(output));
}
const { value: line, done: eof } = await lineReader.next();
return [line, eof];
};
}

(async () => {
const input = inputReader(Deno.stdin);
console.log("-- DENO ADDER --");
// get the value and whether it's the eof
const [num1, eof] = await input("Enter a number: ");
console.log(eof);
// just get the value
const num2 = (await input("Enter another number: "))[0];
console.log(`${num1} + ${num2} = ${Number(num1) + Number(num2)}`);
})();
39 changes: 39 additions & 0 deletions lines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { BufReader } from "https://deno.land/x/io/bufio.ts";
import concatBytes from "./concatBytes.ts";

/**
* returns an AsyncIterable of lines from the given file in bytes.
*/
export async function* linesBytes(
reader: Deno.Reader
): AsyncIterableIterator<Uint8Array> {
const bufReader = new BufReader(reader);
let bufState;
let allBytes = [];
while (bufState !== "EOF") {
let bytes, isPrefix;
[bytes, isPrefix, bufState] = await bufReader.readLine();
if (isPrefix) {
allBytes.push(bytes);
continue;
}
if (allBytes.length > 0) {
allBytes.push(bytes);
bytes = concatBytes(...allBytes);
allBytes = [];
}
yield bytes;
}
}

/**
* Reads from a reader and yields each line
*/
export async function* lines(
reader: Deno.Reader
): AsyncIterableIterator<string> {
const decoder = new TextDecoder();
for await (const line of linesBytes(reader)) {
yield decoder.decode(line);
}
}
66 changes: 0 additions & 66 deletions read_lines.ts

This file was deleted.

20 changes: 16 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# read_lines
# lines

Main script is read_lines.ts. See the example.ts for an example to run.
Main script is [lines.ts](./lines.ts).

You can compare this with the cat implementation on deno's examples in the std library. The time spent seems to be pretty about 3x slower on my Macbook and about the same on my PC.
## Example

See the [example.ts](./example.ts) for an example to run. You can compare this with the cat implementation on deno's examples in the std library. This scripts time spent seems to be pretty about 2x slower than deno's cat example on my Macbook Pro's native terminal.

This example:

Expand All @@ -22,8 +24,18 @@ Deno's cat example
time deno -A https://deno.land/std/examples/cat.ts mobydick.txt
```

You can get the [mobydick.txt from project gutenberg](https://www.gutenberg.org/files/2701/2701-0.txt) or curl it:
You can download the [mobydick.txt from project gutenberg](https://www.gutenberg.org/files/2701/2701-0.txt) or curl it (Mac/Linux) from there like so:

```sh
curl https://www.gutenberg.org/files/2701/2701-0.txt -o mobydick.txt
```

## Input example

The `lines` function's async iterator can be used directly like in [`./example_input.ts`](./example_input.ts). The input reader created in that file is somewhat similar in style to pythons `input` function.

Try it out

```sh
deno https://raw.githubusercontent.com/johnsonjo4531/read_lines/master/example_input.ts
```

0 comments on commit 993bb52

Please sign in to comment.