Simple README.md explaining the exercise code.

master
Zed A. Shaw 2 years ago
parent cfadabb5ff
commit 54e0a00fe9
  1. 12
      01-parse-a-csv-file/README.md
  2. 37
      01-parse-a-csv-file/step6.js

@ -0,0 +1,12 @@
# Exercise 01: CSV is Easy...Right?
This is exercise 01 of `JavaScript Level 2` in [Learn JavaScript the Hard Way](https://learnjsthehardway.com/). It is meant to be done as a challenge with _incorrect_ starting code that you are expected to fix and improve. I repeat, this code is _incorrect_ on _purpose_ so you can fix it and attempt to do better.
* `step1.js` -- Use two projects to get CSV samples and try to write a simple parser that can parse all of the samples.
* `step2.js` -- This is a starter in case you are stuck, but it still fails and would fail on many CSV files. CSV is more complex than its name lets on.
* `step3.js` -- Take your parser and use the [deep-equal](https://www.npmjs.com/package/deep-equal) project to compare your results to the [csv-spectrum](https://github.com/maxogden/csv-spectrum) project's tests.
* `step4.js` -- Let's up the game and make this a unit test with [ava](https://github.com/avajs/ava), except it doesn't work because of promises/async/await not working with callbacks and events.
* `step5.js` -- First solution to `step4.js` that uses `Promise` directly to wrap callbacks and events.
* `step6.js` -- Second solution that uses [util.promisify](https://nodejs.org/api/util.html#utilpromisifyoriginal) to convert the `spectrum` callback to a `Promise` automatically.
If you attempt this challenge then point me at your solution at [@lzsthw](https://twitter.com/lzsthw) and I'll take a look.

@ -0,0 +1,37 @@
import spectrum from "csv-spectrum";
import { parseString } from "fast-csv";
import { promisify } from "util";
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;
}
const spectrum_samples = promisify(spectrum);
test('spectrum samples match', async t => {
const samples = await spectrum_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