I am using this code:
class CsvParser {
constructor(csv, field_separator = ",") {
this.field_separator = field_separator;
this.csv = csv;
this.index = 0;
this.first = true;
}
get_next_record(record) {
record = this.csv.split(',');
if(this.first) {
this.first = false;
} else {
return false;
}
return true;
}
}
function process_csv(contents) {
const parser = new CsvParser(contents);
let record = [];
while(parser.get_next_record(record)) {
console.log(record);
}
console.log("exiting process_csv");
}
process_csv("1,2,3");
Run via this web page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CsvParser demo</title>
<script src="csvparser.js"></script>
</head>
<body>
</body>
</html>
If I run this javascript, the console output is:
[]
exiting process_csv
Why does record after the calling of the get_next_record member function not populate the record?
This code is of course not complete. My idea is that get_next_record would be repeatedly called until the end of the contents was reached in which case the get_next_record function would return false.
I thought that if I try to simply return the record then how to signify that we have reached the end of the contents?
This presumably is not the way things are done in javascript. What is the idiomatic way to design the class/function in javascript? Is call by reference not supported?