-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex4_2.js
More file actions
38 lines (34 loc) · 1.74 KB
/
Copy pathex4_2.js
File metadata and controls
38 lines (34 loc) · 1.74 KB
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
// ## Example 4.2: Using `CSVNormalizer` and `CSVDenormalizer` with `CSVTransformer`
//
// Sometimes it is easier to work with an array of objects than an array of strings in a transformation function.
// `CSVNormalizer` is useful for preparing CSV row data into such an array of objects, in addition to handling
// common CSV mangling by spreadsheet programs.
import { CSVReader, CSVWriter, CSVNormalizer, CSVDenormalizer, CSVTransformer } from '../src/index.js';
/** @import { TransformationFunction } from '../src/index.js'; */
/** @import { CSVNormalizerHeader } from '../src/normalization.js'; */
// A minimal declaration to tell `CSVNormalizer` to trim empty rows and columns but otherwise don't touch the data
/** @type {Array<string|CSVNormalizerHeader>} */
const headers = [
'columnA', // Same as { name: 'columnA' } which is the same as { name: 'columnA', type: 'string' }
{ name: 'columnB' }, // You can mix string and CSVNormalizerHeader in the same array
'columnC',
'sparse1',
'sparse2',
'sparse3'
];
/** @type {TransformationFunction} */
function transform(row) {
let c = 0
for (const field of row) {
field.displayName = `${field.displayName}_new`; // To modify column names
field.value = field.value.toUpperCase(); // To modify field values
if (!field.emptyField) { c++; } // The `emptyField` property is useful for managing sparse or uneven CSV data
}
row.push({ name: 'count', value: c }); // You can also insert new columns in a transformation
return row;
}
await new CSVReader(new URL('./data/ex4_2-in.csv', import.meta.url))
.pipeThrough(new CSVNormalizer(headers))
.pipeThrough(new CSVTransformer(transform))
.pipeThrough(new CSVDenormalizer())
.pipeTo(new CSVWriter(new URL('./data/ex4_2-out.csv', import.meta.url)));