Setup the steps to produce a better CSV parser from the naive one.

master
Zed A. Shaw 2 years ago
parent afd39a1a3c
commit 406ef1c5f8
  1. 1
      01-parse-a-csv-file/package.json
  2. 11
      01-parse-a-csv-file/step1.js
  3. 28
      01-parse-a-csv-file/step2.js
  4. 27
      01-parse-a-csv-file/step3.js

@ -11,6 +11,7 @@
"license": "BSD",
"dependencies": {
"csv-spectrum": "^1.0.0",
"deep-equal": "^2.0.5",
"neat-csv": "^7.0.0"
}
}

@ -1,8 +1,13 @@
import spectrum from "csv-spectrum";
import csv from "neat-csv";
spectrum(async (err, data) => {
for(let sample of data) {
console.log(await csv(sample.csv.toString()));
spectrum(async (err, samples) => {
for(let sample of samples) {
const raw_csv = sample.csv.toString();
console.log("RAW CSV:\n", raw_csv);
const parsed = await csv(raw_csv);
console.log("PARSED:\n", parsed);
}
});

@ -0,0 +1,28 @@
import spectrum from "csv-spectrum";
/*
* A naive CVS parser to start comparing with the existing neat-csv
* parser. This wrong for many CSV inputs, but you'll take this and
* work to make it behave as well as the neat-csv parser.
*/
const simple_parse = (raw_csv) => {
const rows = raw_csv.split("\n");
const result = [];
for(let row of rows) {
result.push(row.split(','));
}
return result;
}
spectrum(async (err, samples) => {
for(let sample of samples) {
const raw_csv = sample.csv.toString();
console.log("RAW CSV:\n", raw_csv);
const parsed = await simple_parse(raw_csv);
console.log("PARSED:\n", parsed);
}
});

@ -0,0 +1,27 @@
import spectrum from "csv-spectrum";
import csv from "neat-csv";
import equal from "deep-equal";
const simple_parse = (raw_csv) => {
const rows = raw_csv.split("\n");
const result = [];
for(let row of rows) {
result.push(row.split(','));
}
return result;
}
spectrum(async (err, samples) => {
for(let sample of samples) {
const raw_csv = sample.csv.toString();
const good = await csv(raw_csv);
const ours = await simple_parse(raw_csv);
if(!equal(ours, good)) {
console.error("EXPECTED", good, "\nGOT", ours);
process.exit(1);
}
}
});
Loading…
Cancel
Save