/
csvExporter.js
160 lines (155 loc) · 3.98 KB
/
csvExporter.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
import _ from 'lodash';
import Parser from 'json2csv/lib/JSON2CSVParser';
export default function exportItems(elements) {
const fields = [{
label: 'Name',
value: 'name'
}, {
label: 'Organization',
value: 'organization'
}, {
label: 'Homepage',
value: 'homepage_url'
}, {
label: 'Logo',
value: (row) => window.location.origin + '/logos/' + row.image_data.fileName
}, {
label: 'Twitter',
value: 'twitter'
}, {
label: 'Crunchbase URL',
value: 'crunchbase'
}, {
label: 'Market Cap',
value: 'yahoo_finance_data.market_cap'
}, {
label: 'Ticker',
value: 'yahoo_finance_data.effective_ticker'
}, {
label: 'Funding',
value: 'crunchbaseData.funding'
}, {
label: 'Member',
value: 'member'
}, {
label: 'Relation',
value: 'relation'
}, {
label: 'License',
value: 'license'
}, {
label: 'Headquarters',
value: 'headquarters'
}, {
label: 'Latest Tweet Date',
value: (row) => formatDate(row, 'latestTweetDate.original')
}, {
label: 'Description',
value: 'description'
}, {
label: 'Crunchbase Description',
value: 'crunchbaseData.description'
}, {
label: 'Crunchbase Homepage',
value: 'crunchbaseData.homepage'
}, {
label: 'Crunchbase City',
value: 'crunchbaseData.city'
}, {
label: 'Crunchbase Region',
value: 'crunchbaseData.region'
}, {
label: 'Crunchbase Country',
value: 'crunchbaseData.country'
}, {
label: 'Crunchbase Twitter',
value: 'crunchbaseData.twitter'
}, {
label: 'Crunchbase Linkedin',
value: 'crunchbaseData.linkedin'
}, {
label: 'Crunchbase Ticker',
value: 'crunchbaseData.ticker'
}, {
label: 'Crunchbase Kind',
value: 'crunchbaseData.kind'
}, {
label: 'Crunchbase Min Employees',
value: 'crunchbaseData.numEmployeesMin'
}, {
label: 'Crunchbase Max Employees',
value: 'crunchbaseData.numEmployeesMax'
}, {
label: 'Category',
value: 'category'
}, {
label: 'Subcategory',
value: (row) => row.landscape.split(' / ')[1]
}, {
label: 'OSS',
value: 'oss'
}, {
label: 'Github Repo',
value: 'repo_url'
}, {
label: 'Github Stars',
value: 'github_data.stars'
}, {
label: 'Github Description',
value: 'github_data.description'
}, {
label: 'Github Latest Commit Date',
value: (row) => formatDate(row, 'latest_commit_date')
}, {
label: 'Github Latest Commit Link',
value: (row) => row.github_data ? ('https://github.com' + row.github_data.latest_commit_link) : ''
}, {
label: 'Github Release Date',
value: (row) => formatDate(row, 'github_data.release_date')
}, {
label: 'Github Release Link',
value: 'github_data.release_link'
}, {
label: 'Github Start Commit Date',
value: (row) => formatDate(row, 'github_start_commit_data.start_date')
}, {
label: 'Github Start Commit Link',
value: (row) => row.github_start_commit_data ? ('https://github.com' + row.github_start_commit_data.start_commit_link) : ''
}, {
label: 'Github Contributors Count',
value: 'github_data.contributors_count'
}, {
label: 'Github Contributors Link',
value: 'github_data.contributors_link'
}];
const json2csvParser = new Parser({ fields });
const csv = json2csvParser.parse(elements, { fields });
downloadCSV(csv);
}
function formatDate(row, field) {
const fieldParts = field.split('.');
var value = row[fieldParts[0]];
if (!value) {
return '';
}
if (fieldParts[1]) {
value = value[fieldParts[1]];
}
if (!value) {
return '';
}
return value.substring(0, 10) + ' ' + value.substring(11, 19);
}
function downloadCSV(csv) {
var data, filename, link;
filename = 'interactive_landscape.csv';
window.csv = csv;
if (!csv.match(/^data:text\/csv/i)) {
csv = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv);
}
window.csv = csv;
link = document.createElement('a');
link.setAttribute('href', csv);
link.setAttribute('download', filename);
link.click();
}