Skip to content

Commit

Permalink
docs: fs example
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jan 9, 2024
1 parent 1a12e8f commit 93260e5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/csv/samples/.gitignore
@@ -0,0 +1,2 @@
example.fs.input.csv
example.fs.output.csv
24 changes: 24 additions & 0 deletions packages/csv/samples/example.fs.js
@@ -0,0 +1,24 @@
import fs from "node:fs";
import assert from "node:assert";
import { finished } from "node:stream/promises";
import { parse, transform, stringify } from "csv";

const __dirname = new URL(".", import.meta.url).pathname;
await fs.promises.writeFile(
`${__dirname}/example.fs.input.csv`,
"a,b,c\n1,2,3"
);

await finished(
fs
.createReadStream(`${__dirname}/example.fs.input.csv`)
.pipe(parse())
.pipe(transform((record) => record.reverse()))
.pipe(stringify())
.pipe(fs.createWriteStream(`${__dirname}/example.fs.output.csv`))
);

assert.equal(
await fs.promises.readFile(`${__dirname}/example.fs.output.csv`, "utf8"),
"c,b,a\n3,2,1\n"
);

0 comments on commit 93260e5

Please sign in to comment.