This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
58 lines (51 loc) · 1.6 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
const commandLineArgs = require('command-line-args')
const JSONStream = require('JSONStream')
const fs = require('fs')
const es = require('event-stream')
const optionDefinitions = [
{ name: 'type', type: String, defaultOption: 'companies' },
{ name: 'file', type: String },
{ name: 'id', type: String }
]
const options = commandLineArgs(optionDefinitions)
if (!options.file) {
console.log('No file provided')
process.exit(1)
}
if (options.id) {
options.id = parseInt(options.id)
}
let stream = fs.createReadStream(options.file, {encoding: 'utf8'})
let parser = JSONStream.parse('items.*')
let totalRows = 0
stream
.pipe(parser)
.pipe(es.mapSync(function (data) {
switch (options.type) {
case 'companies':
case 'investors':
if (options.id) {
if (options.id === parseInt(data.id)) {
console.log(JSON.stringify(data))
}
} else {
console.log('['+data.id+'] '+data.name)
}
break;
case 'rounds':
if (options.id) {
if (options.id === parseInt(data.id)) {
console.log(JSON.stringify(data))
}
} else {
console.log('['+data.id+'] '+data.round+' ('+data.company_name+')')
}
break;
default:
throw new Error('Wrong type provided')
}
totalRows++
}))
stream.on('end', function() {
console.log('Total rows: '+totalRows)
})