Implement step4 as a demo of how to deal with APIs that don't understand async using a promise. Remove useless await on simple_parse.

master
Zed A. Shaw 2 years ago
parent 406ef1c5f8
commit bb6e9b0e38
  1. 2
      01-parse-a-csv-file/package.json
  2. 1
      01-parse-a-csv-file/step1.js
  3. 4
      01-parse-a-csv-file/step2.js
  4. 2
      01-parse-a-csv-file/step3.js
  5. 37
      01-parse-a-csv-file/step4.js

@ -10,8 +10,10 @@
"author": "",
"license": "BSD",
"dependencies": {
"ava": "^4.3.1",
"csv-spectrum": "^1.0.0",
"deep-equal": "^2.0.5",
"fast-csv": "^4.3.6",
"neat-csv": "^7.0.0"
}
}

@ -2,7 +2,6 @@ import spectrum from "csv-spectrum";
import csv from "neat-csv";
spectrum(async (err, samples) => {
for(let sample of samples) {
const raw_csv = sample.csv.toString();
console.log("RAW CSV:\n", raw_csv);

@ -17,12 +17,12 @@ const simple_parse = (raw_csv) => {
}
spectrum(async (err, samples) => {
spectrum((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);
const parsed = simple_parse(raw_csv);
console.log("PARSED:\n", parsed);
}
});

@ -17,7 +17,7 @@ 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);
const ours = simple_parse(raw_csv);
if(!equal(ours, good)) {
console.error("EXPECTED", good, "\nGOT", ours);

@ -0,0 +1,37 @@
import spectrum from "csv-spectrum";
import { parseString } from "fast-csv";
import test from "ava";
const simple_parse = (raw_csv) => {
const rows = raw_csv.split("\n");
const result = [];
for(let row of rows) {
result.push(row.split(','));
}
return result;
}
test('spectrum samples match', async t => {
const samples = await new Promise((res, rej) => {
spectrum((err, samples) => res(samples));
});
for(let sample of samples) {
const raw_csv = sample.csv.toString();
const good = [];
const count = await new Promise((res, rej) => {
parseString(raw_csv)
.on('error', rej())
.on('data', row => good.push(row))
.on('end', rowCount => res(rowCount));
});
const ours = simple_parse(raw_csv);
t.is(ours.length, count);
t.is(ours, good);
}
})
Loading…
Cancel
Save