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.
parent
406ef1c5f8
commit
bb6e9b0e38
@ -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…
Reference in new issue