Created
March 15, 2023 17:20
-
-
Save gflarity/12e7b9774705e704d7c55799881e4f50 to your computer and use it in GitHub Desktop.
Some Deno code for reading a file a line by line asychronously
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
// function that returns reads a file line by line asynchronously | |
export async function* readFileLineByLine(file: string) : AsyncGenerator<string> { | |
const textDecoder = new TextDecoder(); | |
let stringBuffer: string = ""; | |
const fileHandle = await Deno.open(file); | |
try { | |
let intBuffer = new Uint8Array(1000); | |
let numberOfBytesRead = await fileHandle.read(intBuffer); | |
while (numberOfBytesRead !== null) { | |
const asString = textDecoder.decode(intBuffer); | |
stringBuffer += asString; | |
while (stringBuffer.includes("\n")) { | |
const indexOfNewLine = stringBuffer.indexOf("\n"); | |
const newLine = stringBuffer.slice(0, indexOfNewLine); | |
yield newLine; | |
// replace the stringBuffer with the remaining string and keep processing | |
stringBuffer = stringBuffer.slice(indexOfNewLine + 1); | |
} | |
intBuffer = new Uint8Array(1000); | |
numberOfBytesRead = await fileHandle.read(intBuffer); | |
} | |
} finally { | |
fileHandle.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment