/
csv.js
38 lines (36 loc) · 946 Bytes
/
csv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import * as csv from 'csv/browser/esm';
window.onload = () => {
const runEl = document.getElementById('run');
const outputEl = document.getElementById('output');
runEl.onclick = (e) => {
// Run the pipeline
csv
// Generate 20 records
.generate({
delimiter: '|',
length: 10
})
// Transform CSV data into records
.pipe(csv.parse({
delimiter: '|'
}))
// Transform each value into uppercase
.pipe(csv.transform( (record) => {
record.push(record.shift());
return record;
}))
// Convert objects into a stream
.pipe(csv.stringify({
quoted: true
}))
// Print the CSV stream to stdout
.on('readable', function () {
let chunck; while(chunck = this.read()){
outputEl.innerHTML = outputEl.innerHTML + chunck.toString()
}
})
.on('error', function (err) {
outputEl.innerHTML = 'Error: ' + err.message;
});
};
};