-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·130 lines (103 loc) · 3.26 KB
/
index.js
File metadata and controls
executable file
·130 lines (103 loc) · 3.26 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { Command } = require('commander');
const parser = require('csv-parser');
const program = new Command();
program.version('0.0.1', '-v, --version');
program
.requiredOption('-i, --input <file>', 'input CSV file')
.option('-d, --delimiter <delimiter>', 'delimiter used in the CSV', ',')
.option(
'-c, --columns <columns>',
'columns to extract, comma-separated\nyou can get unique values per unique values from another column by\nchaining them together using `::`\n(default: all columns)'
)
.option(
'-o, --output <directory>',
'output destination, will create a file per column'
)
.option('--no-sort', "don't sort unique values");
program.parse(process.argv);
const input = fs.createReadStream(path.resolve(program.opts().input));
class SetWithToJSONSupport extends Set {
toJSON() {
const asArray = Array.from(this);
return program.opts().sort
? asArray.sort((a, b) => a.localeCompare(b, 'en', { numeric: true }))
: asArray;
}
}
const uniqueValues = {};
input
.on('error', (error) => {
console.error('Error while reading input file', error);
})
.pipe(parser({ separator: program.opts().delimiter }))
.on('data', (row) => {
const columns = program.opts().columns
? program.opts().columns.split(',')
: Object.keys(row);
columns.forEach((column) => {
const groups = column.split('::');
if (groups.length === 1) {
const value = row[column];
if (typeof value === 'undefined') {
return;
}
if (!uniqueValues[column]) {
uniqueValues[column] = new SetWithToJSONSupport();
}
uniqueValues[column].add(value);
return;
}
if (!uniqueValues[column]) {
uniqueValues[column] = {};
}
let target = uniqueValues[column];
groups.forEach((group, i) => {
const value = row[group];
if (typeof value === 'undefined') {
return;
}
if (i === groups.length - 1) {
target.add(value);
} else if (i === groups.length - 2) {
if (!target[value]) {
target[value] = new SetWithToJSONSupport();
}
target = target[value];
} else {
if (!target[value]) {
target[value] = {};
}
target = target[value];
}
});
});
})
.on('end', async () => {
if (!program.opts().output) {
console.log(JSON.stringify(uniqueValues, null, 2));
return;
}
const parsedOutputPath = path.normalize(program.opts().output);
await (async () => {
return new Promise((resolve) => {
fs.mkdir(parsedOutputPath, { recursive: true }, (err) => {
if (err && err.code !== 'EEXIST') {
console.error('Error creating output directory', parsedOutputPath);
console.error(err);
} else {
resolve();
}
});
});
})();
Object.entries(uniqueValues).forEach(([column, values]) => {
const json = JSON.stringify(values, null, 2);
const output = fs.createWriteStream(
path.resolve(parsedOutputPath, `${column.replace(/::/gi, '-')}.json`)
);
output.write(json);
});
});