I have a large amount of small textfiles where the first 4 or less lines contain metadata; following is an example
Lorem Ipsum
Tag1 Tag2 Tag3
Text
4204
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
- Lorem Ipsum; would be the title
- Tag1, Tag2, and Tag3; would be an array of tags
- Text; would be the type
- 4204; would be the ID.
- Lorem ipsum [...]; would be the actual content
I need to load the metadata without loading the actual content. Im working in node.js. I made following code:
function readMeta (path, callback) {
const meta = {};
const lineReader = require("readline").createInterface({input: require("fs").createReadStream(path)});
let lineCount = 0;
let interpretedMeta;
lineReader.on("line", line => {
interpretedMeta = interpretMeta(line, lineCount)
switch (lineCount) {
case 0:
meta.name = interpretedMeta;
break;
case 1:
meta.tags = interpretedMeta.split(" ");
break;
case 2:
meta.type = interpretedMeta;
break;
case 3:
meta.id = interpretedMeta;
}
++lineCount;
if (/^\s*$/.test(line)) {
lineReader.close();
}
});
lineReader.on("close", () => {
callback(meta);
process.exit(0);
});
}
where interpretMeta()
is a function that formats the string given based on a linenumber. I will integrate this into readMeta()
later since it's somewhat redundant.
Problem
This code works with one file, but bugs if it runs multiple times in a short amount of time. It reaches the second line but then starts over each time the function runs.
Im not 100% sure why this happens, but I assume something like lineReader.on()
's callback doesn't make copies of the variables it gets from readMeta
happens. I can't figure out how to debug or solve though.
Fix
I have no experience whatsoever working with asynchronous functions, so apologies if i use the wrong terms onwards: I believe a way around my problem that i would be comfortable working with, is a synchronous readline()
function that reads the next line in a stream. I can't figure out how to do this though, so my question is how do i:
A: fix the code
B: make a synchronous ´readline` function
Thanks