Public
Edited
Nov 29, 2022
Insert cell
Insert cell
viewof file = Inputs.file()
Insert cell
url = file.url()
Insert cell
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#processing_a_text_file_line_by_line

async function* makeTextFileLineIterator(fileURL) {
const utf8Decoder = new TextDecoder('utf-8');
const response = await fetch(fileURL);
const reader = response.body.getReader();
let { value: chunk, done: readerDone } = await reader.read();
chunk = chunk ? utf8Decoder.decode(chunk) : '';

const re = /\r\n|\n|\r/gm; // this regex was cr or lf or crlf, but that cause blank lines so I changed the order to crlf or cr or lf
let startIndex = 0;
let result;

while (true) {
let result = re.exec(chunk);
if (!result) {
if (readerDone) break;
let remainder = chunk.substr(startIndex);
({ value: chunk, done: readerDone } = await reader.read());
chunk = remainder + (chunk ? utf8Decoder.decode(chunk) : '');
startIndex = re.lastIndex = 0;
continue;
}
yield chunk.substring(startIndex, result.index);
startIndex = re.lastIndex;
}

if (startIndex < chunk.length) {
// Last line didn't end in a newline char
yield chunk.substr(startIndex);
}
}
Insert cell
process = {
let c = 0 // a counter of the lines to process
for await (const line of makeTextFileLineIterator(url)) {
c++
// console.log ('line ', d3.tsvParse(line)) // display the line after converting it json
}
return c
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more