diff --git a/01-parse-a-csv-file/package.json b/01-parse-a-csv-file/package.json index dbc281f..b608a9e 100644 --- a/01-parse-a-csv-file/package.json +++ b/01-parse-a-csv-file/package.json @@ -11,6 +11,7 @@ "license": "BSD", "dependencies": { "csv-spectrum": "^1.0.0", + "deep-equal": "^2.0.5", "neat-csv": "^7.0.0" } } diff --git a/01-parse-a-csv-file/step1.js b/01-parse-a-csv-file/step1.js index 02f9a9f..cb46a9b 100644 --- a/01-parse-a-csv-file/step1.js +++ b/01-parse-a-csv-file/step1.js @@ -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); } }); diff --git a/01-parse-a-csv-file/step2.js b/01-parse-a-csv-file/step2.js new file mode 100644 index 0000000..0e7278d --- /dev/null +++ b/01-parse-a-csv-file/step2.js @@ -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); + } +}); diff --git a/01-parse-a-csv-file/step3.js b/01-parse-a-csv-file/step3.js new file mode 100644 index 0000000..4521775 --- /dev/null +++ b/01-parse-a-csv-file/step3.js @@ -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); + } + } +});