-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
executable file
·398 lines (385 loc) · 14.1 KB
/
index.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/usr/bin/env node
'use strict';
const promisify = require('js-promisify');
const fs = require('fs');
const csv = require('csv');
const through = require('through');
const firstline = require('firstline');
require('colors');
exports.load = function load (path, usrOpts) {
const opts = {
delimiter: ',',
encoding: 'utf8',
log: true,
objName: undefined,
parse: true,
stream: false
};
Object.assign(opts, usrOpts);
const parseOpts = opts._parseOpts || { // "_parseOpts" is a private option, used for the "check()" method
cast: opts.parse,
columns: true,
delimiter: opts.delimiter,
objname: opts.objName,
skip_empty_lines: true
};
const log = opts.log;
if (opts.delimiter.length > 1) throw new Error('The delimiter can only be one character'.red);
log && console.log(`\nReading data from ${path}\n`);
if (opts.stream) {
return fs.createReadStream(path, {encoding: opts.encoding}).pipe(csv.parse(parseOpts));
} else {
return promisify(fs.readFile, [path, {encoding: opts.encoding}])
.then(data => {
if (data) {
log && console.log('Parsing data...\n'.yellow);
return promisify(csv.parse, [data, parseOpts])
.then(data => {
log && console.log('Data parsed\n'.green);
return data;
});
} else {
log && console.log('File appears to be empty!\n'.yellow);
}
});
}
};
exports.write = function write (path, data, usrOpts) {
const opts = {
append: false,
delimiter: ',',
encoding: 'utf8',
empty: false,
header: '',
log: true
};
Object.assign(opts, usrOpts);
const delimiter = opts.delimiter;
const log = opts.log;
let header = opts.header;
let hlen;
return new Promise((resolve, reject) => {
if (delimiter.length > 1) throw new Error('The delimiter can only be one character'.red);
const ws = fs.createWriteStream(path, {encoding: opts.encoding, flags: opts.append ? 'a' : 'w'});
ws
.on('finish', () => resolve())
.on('error', err => reject(err));
log && console.log((`\nWriting data to ${path}\n`));
if (header) {
if (typeof header === 'string') {
if (!opts.append) ws.write(header + '\n');
header = header.split(delimiter);
hlen = header.length;
header.forEach(item => {
if (item.length === 0) throw new Error('Header column titles can not be empty'.red);
});
} else {
throw new Error('The header argument must be a string'.red);
}
}
if (typeof data === 'string' || Array.isArray(data)) {
if (typeof data === 'string') {
data = data.split('\n');
for (let i = 0; i < data.length; i++) {
data[i] = data[i].split(delimiter);
}
}
// It's an array (of arrays or of objects?)
if (Array.isArray(data[0])) {
// It's an array of arrays
hlen = hlen || data[0].length;
for (let i = 0; i < data.length; i++) {
let arr = data[i];
if (Array.isArray(arr)) {
if (arr.length === hlen) {
if (opts.empty) {
for (let i = 0; i < arr.length; i++) {
let value = arr[i];
if (value === undefined || value === null || value === '') {
throw new Error(('Empty value "' + header[i] + '" at line' + (i + 1) + ':\n').red +
JSON.stringify(arr) + '\n');
}
}
}
ws.write(arr.join(delimiter) + '\n');
} else {
if (arr[0] !== '' || arr.length !== 1) {
throw new Error(('Number of values different from first line of CSV\n').red +
'First line length: ' + hlen + '\n' +
'Entry (length ' + arr.length + '): ' + JSON.stringify(arr) + '\n');
}
}
} else {
throw new Error(('Wrong input in array at index ' + i + '\n').red +
'This item is not an array:\n' + JSON.stringify(arr) + '\n');
}
}
} else if (typeof data[0] === 'object') {
// It's an array of objects
if (header) {
for (let i = 0; i < data.length; i++) {
let obj = data[i];
if (typeof obj === 'object') {
let entry = [];
for (let i = 0; i < header.length; i++) {
let key = header[i];
if (obj.hasOwnProperty(key)) {
let value = obj[key];
if (opts.empty) {
if (value === undefined || value === null || value === '') {
throw new Error(('Empty value "' + key + '"" in object:\n').red +
JSON.stringify(obj) + '\n');
}
}
entry.push(value);
} else {
throw new Error('Object properties do not conform to the format specified in header\n'.red +
'Header: ' + JSON.stringify(header) + '\n' +
'Object: ' + JSON.stringify(obj) + '\n');
}
}
ws.write(entry.join(delimiter) + '\n');
} else {
throw new Error(('Wrong input in array at index ' + i + '\n').red +
'This item is not an object:\n' + JSON.stringify(obj) + '\n');
}
}
} else {
throw new Error('When data comes from an object, the header argument must be provided\n'.red);
}
}
} else if (typeof data === 'object') {
// It's an object (containing objects?)
if (header) {
const objIndex = Object.keys(data);
for (let i = 0; i < objIndex.length; i++) {
let obj = data[objIndex[i]];
let entry = [];
for (let i = 0; i < header.length; i++) {
let key = header[i];
if (obj.hasOwnProperty(key)) {
let value = obj[key];
if (opts.empty) {
if (value === undefined || value === null || value === '') {
throw new Error(('Empty value "' + key + '"" in object:\n').red +
JSON.stringify(obj) + '\n');
}
}
entry.push(value);
} else {
throw new Error('Object properties do not conform to the format specified in header\n'.red +
'Header: ' + JSON.stringify(header) + '\n' +
'Object: ' + JSON.stringify(obj) + '\n');
}
}
ws.write(entry.join(delimiter) + '\n');
}
} else {
throw new Error('When data comes from an object, the header argument must be provided\n'.red);
}
} else {
throw new Error('Wrong input!'.red + ' Data can be accepted only in these formats:\n' +
'- String\n' +
'- Array of arrays\n' +
'- Array of objects\n' +
'- Object containing objects\n' +
'(see documentation for further details)\n');
}
ws.end();
log && console.log('Data written!\n'.green);
});
};
exports.check = function check (path, usrOpts) {
const opts = {
delimiter: ',',
duplicates: false,
emptyLines: false,
emptyValues: true,
encoding: 'utf8',
limit: false,
log: true
};
Object.assign(opts, usrOpts);
let limit;
const log = opts.log;
return firstline(path, {encoding: opts.encoding})
.then(line => {
let cols = line.split(opts.delimiter);
if (cols.length === 1 && cols[0] === '') {
log && console.log(`\nReading data from ${path}\n\nFile appears to be empty!\n`.yellow);
return false;
}
cols.forEach(col => {
if (col === '') {
let err = 'The CSV header contains empty values\n'.red;
log && !module.parent && console.error(err);
throw new Error(err);
}
});
let hlen = cols.length;
if (opts.limit) {
limit = [];
opts.limit.split(',').forEach(col => {
let i = cols.indexOf(col);
if (i === -1) {
let err = (`The column value "${col}" does not correpond to CSV headers\n`).red +
'Please provide valid column names (string format, comma separated)\n';
log && !module.parent && console.error(err);
throw new Error(err);
}
limit.push(i);
});
}
return new Promise((resolve, reject) => {
const rs = exports.load(path, {
encoding: opts.encoding,
log: opts.log,
stream: true,
_parseOpts: {
relax_column_count: true,
delimiter: opts.delimiter
}
});
let result = true;
let count = 1;
let missing = [];
let emptyLines = opts.emptyLines === true ? [] : false;
let emptyValues = opts.emptyValues === true ? [] : false;
let duplicates;
if (opts.duplicates === true) {
// Duplicate checking is done through an array that for each column
// contains an array with two objects: "memo" and "map".
// "memo" is to check if the value already exists, and
// "map" is where duplicates coordinates are actually stored.
duplicates = [];
if (limit) {
limit.forEach(col => {
duplicates[col] = [{},{}];
});
} else {
for (let i = 0; i < hlen; i++) {
duplicates[i] = [{},{}];
}
}
}
function checkEmptyValues (line, col, item) {
if (line.length !== 1 && item === '') {
result = false;
log && emptyValues.push([count,col]);
}
}
function checkDuplicates (col, item) {
if (item !== '' && item !== undefined) {
let memo = duplicates[col][0][item];
if (memo === undefined) {
duplicates[col][0][item] = count;
} else {
result = false;
if (log) {
let map = duplicates[col][1][item];
map ? map.push(count) : (duplicates[col][1][item] = [memo, count]);
}
}
}
}
function check (line) {
// Check missing values and empty lines
if (line.length !== hlen) {
if (line.length === 1 && line[0] === '') {
if (emptyLines) {
result = false;
log && emptyLines.push(count);
}
} else {
result = false;
log && missing.push(count);
}
}
// Check empty or duplicate values
if (emptyValues || duplicates) {
if (limit) {
for (let i = 0; i < limit.length; i++) {
let col = limit[i];
let item = line[col];
emptyValues && checkEmptyValues(line, col, item);
duplicates && checkDuplicates(col, item);
}
} else {
for (let col = 0; col < hlen; col++) {
let item = line[col];
emptyValues && checkEmptyValues(line, col, item);
duplicates && checkDuplicates(col, item);
}
}
}
count++;
}
rs
.pipe(through(check))
.on('end', () => {
if (result) {
log && console.log('\nFile looks ok.\n'.green);
} else {
if (log) {
missing && missing[0] !== undefined && console.log(`\nMissing value on line ${missing.join(', ')}\n`);
emptyLines && emptyLines[0] !== undefined && console.log(`\nEmpty line on line ${emptyLines.join(', ')}\n`);
if (emptyValues && emptyValues[0] !== undefined) {
console.log('\nEmpty value on line:');
for (let i = 0; i < emptyValues.length; i++) {
let item = emptyValues[i];
console.log(`${item[0]} (${cols[item[1]]})`);
}
}
if (duplicates) {
for (let col = 0; col < hlen; col++) {
if (duplicates[col]) {
let map = duplicates[col][1];
let mapKeys = Object.keys(map);
if (mapKeys.length !== 0) {
console.log(`\nDuplicate values for "${cols[col]}":`);
for (let i = 0; i < mapKeys.length; i++) {
let key = mapKeys[i];
console.log(`"${key}" on line: ${map[key].join(', ')}`);
}
}
}
}
}
}
log && console.error('\nFile has problems!\n'.red);
}
resolve(result);
})
.on('error', err => {
log && !module.parent && console.error(err);
reject(err);
});
});
});
};
// Code for command line usage of the "check" method
if (!module.parent) {
const program = require('commander');
program
.usage('[options] <file ...>')
.option('-c, --check <filepath>', 'Check data integrity for the CSV file indicated at <filepath> (must be provided)')
.option('-d, --duplicates', 'Check for duplicate values within columns')
.option('-e, --empty-values', 'Accept empty values')
.option('-l, --limit <comma separated column names>', 'Limit CSV checking to specific column(s)')
.option('-s, --log', 'Avoid logging and speed up')
.option('-x, --empty-lines', 'Check for empty lines');
program.parse();
const opts = program.opts();
if (typeof opts.check === 'string' && opts.check[0] !== '-' && opts.check.length !== 0) {
const optsObj = {
duplicates: opts.duplicates,
emptyValues: !opts.emptyValues,
emptyLines: opts.emptyLines,
limit: opts.limit,
log: !opts.log
};
exports.check(opts.check, optsObj);
} else {
program.help();
}
}