Skip to content
This repository was archived by the owner on Jun 28, 2021. It is now read-only.

Commit fb07f2b

Browse files
committed
samples: new file recipie
1 parent 5fd0c83 commit fb07f2b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
* promise: new API module
1111
* errors: finish normalisation of all errors
1212

13+
## Trunk
14+
15+
Project management:
16+
* samples: new file recipie
17+
1318
## Version 4.9.1
1419

1520
Minor improvements:

samples/recipies.file.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
const os = require('os');
3+
const fs = require('fs').promises;
4+
const parse = require('../lib/sync');
5+
6+
(async function(){
7+
// Prepare the dataset
8+
await fs.writeFile(`${os.tmpdir()}/input.csv`, [
9+
'\ufeff', // BOM
10+
'a,1\n', // First record
11+
'b,2\n' // Second record
12+
].join(''), {encoding: 'utf8'})
13+
// Read the content
14+
const content = await fs.readFile(`${os.tmpdir()}/input.csv`)
15+
// Parse the CSV content
16+
const records = parse(content)
17+
// Print records to the console
18+
// records.map( record => console.log(record) )
19+
// Write a file with one JSON per line for each record
20+
json = records.map( JSON.stringify ).join('\n')
21+
fs.writeFile(`${os.tmpdir()}/output.csv`, json)
22+
})()

0 commit comments

Comments
 (0)