-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
54b498f
commit 993bb52
Showing
6 changed files
with
119 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters