-
Notifications
You must be signed in to change notification settings - Fork 0
/
papa-unparse.html
206 lines (194 loc) · 4.97 KB
/
papa-unparse.html
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
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../polymer/lib/utils/debounce.html">
<link rel="import" href="./papaparse.html">
<!--
`papa-unparse` is a Polymer 2.0 element to convert JSON arrays or objects into
CSV strings with [Papa parse](http://papaparse.com/).
### Basic usage
For array of objects, e.g. [{col1: 1, col2: 2}, {col1: 3, col2: 4}]
```html
<papa-unparse header json-array="[[rows]]"></papa-unparse>
```
For array of arrays, e.g. [[1,2], [3,4]]
```html
<papa-unparse json-array="[[rows]]" fields="[[fields]]"></papa-unparse>
```
### Download as CSV file
```html
<papa-unparse header json-array="[[rows]]"></papa-unparse>
<button onclick="javascript:download">Download</button>
```
```javascript
function download() {
document.querySelector('papa-unparse').downloadCsv('somefile.csv');
}
```
### Other options
```html
<papa-unparse
header
quotes
quoteChar="'"
delimiter="|"
newline="\n"
json-array="[[rows]]"></papa-unparse>
```
@customElement
@polymer
@demo demo/unparse.html
-->
<dom-module id="papa-unparse">
<script>
class PapaUnparse extends Polymer.Element {
static get is() {
return 'papa-unparse';
}
static get properties() {
return {
/**
* whether to enclose all the fields with `quoteChar`.
* @type {Boolean}
*/
quotes: {
type: Boolean,
value: false
},
/**
* character used to enclose all the fields with.
* @type {String}
*/
quoteChar: {
type: String,
value: '"'
},
/**
* delimiter used to delimit the fields.
* @type {String}
*/
delimiter: {
type: String,
value: ','
},
/**
* whether to include the header.
* @type {Boolean}
*/
header: {
type: Boolean,
value: false
},
/**
* The newline sequence.
* @type {String}
*/
newline: {
type: String,
value: '\r\n'
},
/**
* The data to transform. `json-array` can be one of the following:
* - An array of arrays: e.g. [['col1', 'col2'],[1, 2], [3, 4]]
* - An array of objects: e.g. [{col1:1, col2:2}, {col1:3, col2:4}]
*
* @type {Array}
*/
jsonArray: {
type: Array
},
/**
* The header to insert if not already inside `json-array`. Only
* applicable when `json-array` is an array of arrays, where header
* is not included. e.g. [[1, 2], [3, 4]]] instead of
* [['col1', 'col2'],[1, 2], [3, 4]]
* @type {String[]}
*/
fields: {
type: Array,
value: null
},
/**
* The CSV string produced from the JSON array.
* @type {String}
*/
csvStr: {
type: String,
notify: true
},
/**
* @type {Polymer.Debouncer}
*/
_debounceJob: {
type: Object
},
_config: {
type: Object,
value: function() {
return {};
}
}
};
}
static get observers() {
return [
'_unparse(jsonArray, fields, quotes, quoteChar, delimiter, header, newline)'
];
}
/**
* Wrapper around `Papa.unparse`.
*
* @param {Array|Object} data
* @param {{quotes:Boolean, quoteChar:String, delimiter:String, header:Boolean, newline:String}} config
* @return {String}
*/
unparse(data, config) {
if (!data) return;
return Papa.unparse(data, config);
}
/**
* Unparse and download the resultant `csv-str` as a csv file.
* @param {String} filename
* @param {Array|Object} data
* @param {{quotes:Boolean, quoteChar:String, delimiter:String, header:Boolean, newline:String}} config
*/
downloadCsv(filename, data, config) {
var csvStr =
arguments.length >= 2 ? this.unparse(data, config) : this.csvStr;
filename = filename || 'data.csv';
var link = document.createElement('a');
link.setAttribute('href', 'data:text/csv,' + encodeURI(csvStr));
link.setAttribute('target', '_blank');
link.setAttribute('download', filename);
link.dispatchEvent(new MouseEvent('click'));
}
_unparse() {
this._debounceJob = Polymer.Debouncer.debounce(
this._debounceJob,
Polymer.Async.microTask,
() => {
if (!Array.isArray(this.jsonArray) || this.jsonArray.length === 0)
return;
let config = {
quotes: this.quotes,
quoteChar: this.quoteChar,
delimiter: this.delimiter,
header: this.header,
newline: this.newline
};
if (Array.isArray(this.fields) && Array.isArray(this.jsonArray[0])) {
this.csvStr = this.unparse(
{
fields: this.fields,
data: this.jsonArray
},
config
);
} else {
this.csvStr = this.unparse(this.jsonArray, config);
}
}
);
}
}
window.customElements.define(PapaUnparse.is, PapaUnparse);
</script>
</dom-module>