Created
November 19, 2024 15:31
-
-
Save wschutzer/b2e147a9c1a1e8ff355ff7c73ebae1ef to your computer and use it in GitHub Desktop.
Simulation reader
This file contains hidden or 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
async function readLargeFile(url) { | |
const response = await fetch(url); | |
const reader = response.body.getReader(); | |
const decoder = new TextDecoder(); | |
let remainingData = ""; | |
while (true) { | |
const { done, value } = await reader.read(); | |
if (done) break; | |
remainingData += decoder.decode(value, { stream: true }); | |
let lines = remainingData.split("\n"); | |
// Process all lines except the last (incomplete) one | |
for (let i = 0; i < lines.length - 1; i++) { | |
let parsedData = parseLine(lines[i]); | |
console.log("Parsed data:", parsedData); | |
} | |
// Save the incomplete line for the next chunk | |
remainingData = lines[lines.length - 1]; | |
} | |
// Process the last line if there is remaining data | |
if (remainingData) { | |
let parsedData = parseLine(remainingData); | |
console.log("Parsed data:", parsedData); | |
} | |
} | |
function parseLine(line) { | |
// Split the line by whitespace, comma, or other delimiters | |
let numbers = line | |
.trim() | |
.split(/[\s,]+/) // Splits by spaces, tabs, or commas | |
.map(parseFloat); // Convert strings to floating-point numbers | |
// Group numbers into pairs | |
let pairs = []; | |
for (let i = 0; i < numbers.length; i += 2) { | |
if (i + 1 < numbers.length) { | |
pairs.push([numbers[i], numbers[i + 1]]); | |
} | |
} | |
return pairs; | |
} | |
// Call the function with the URL of the file | |
readLargeFile("https://example.com/largefile.txt"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment