One more step in the process to learn about converting callbacks with promises.

master
Zed A. Shaw 2 years ago
parent bb6e9b0e38
commit cfadabb5ff
  1. 2
      01-parse-a-csv-file/step3.js
  2. 32
      01-parse-a-csv-file/step4.js
  3. 36
      01-parse-a-csv-file/step5.js

@ -20,7 +20,7 @@ spectrum(async (err, samples) => {
const ours = simple_parse(raw_csv);
if(!equal(ours, good)) {
console.error("EXPECTED", good, "\nGOT", ours);
console.error("FAIL EXPECTED", good, "\nGOT", ours);
process.exit(1);
}
}

@ -13,25 +13,21 @@ const simple_parse = (raw_csv) => {
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 = [];
test('failing spectrum samples match', async t => {
spectrum(async (err, samples) => {
for(let sample of samples) {
const raw_csv = sample.csv.toString();
const ours = simple_parse(raw_csv);
const count = await new Promise((res, rej) => {
parseString(raw_csv)
.on('error', rej())
.on('error', t.fail())
.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);
}
.on('end', rowCount => {
if(!equal(ours, good)) {
console.error("FAIL EXPECTED", good, "\nGOT", ours);
process.exit(1);
}
});
}
});
})

@ -0,0 +1,36 @@
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', err => rej(err))
.on('data', row => good.push(row))
.on('end', rowCount => res(rowCount));
});
const ours = simple_parse(raw_csv);
t.is(ours, good);
}
})
Loading…
Cancel
Save