1
1
<template >
2
2
<div :id =" idName" @click =" generate" >
3
- <slot >Download {{name}}</slot >
3
+ <slot >Download {{ name }}</slot >
4
4
</div >
5
5
</template >
6
6
7
- <script >
7
+ <script lang="ts">
8
+ import {defineComponent } from ' vue'
8
9
import mapKeys from " lodash.mapkeys" ;
9
10
import pickBy from " lodash.pickby" ;
10
11
import pick from " lodash.pick" ;
11
12
12
- import { saveAs } from " file-saver" ;
13
- import { unparse } from " papaparse" ;
13
+ import {saveAs } from " file-saver" ;
14
+ import {unparse } from " papaparse" ;
14
15
15
- export const isType = (value , type ) => typeof value === type;
16
+ export const isType = (value : any , type : string ) => typeof value === type ;
16
17
17
- export default {
18
+ export default defineComponent ( {
18
19
name: " JsonCSV" ,
19
20
props: {
20
21
/**
@@ -30,6 +31,7 @@ export default {
30
31
* Can either be an array or a function
31
32
*/
32
33
fields: {
34
+ type: [Array , Function ],
33
35
required: false
34
36
},
35
37
/**
@@ -68,14 +70,16 @@ export default {
68
70
*/
69
71
advancedOptions: {
70
72
type: Object ,
71
- default : () => {}
73
+ default : () => {
74
+ }
72
75
},
73
76
/**
74
77
* Labels for columns
75
78
*
76
79
* Object or function
77
80
*/
78
81
labels: {
82
+ type: [Object , Function ],
79
83
required: false
80
84
},
81
85
/**
@@ -102,64 +106,67 @@ export default {
102
106
}
103
107
},
104
108
methods: {
105
- labelsFunctionGenerator () {
109
+
110
+ labelsFunctionGenerator(): (item : any ) => any {
111
+ let labels: any = this .labels ;
106
112
if (
107
- ! isType (this . labels , " undefined" ) &&
108
- ! isType (this . labels , " function" ) &&
109
- ! isType (this . labels , " object" )
113
+ ! isType (labels , " undefined" ) &&
114
+ ! isType (labels , " function" ) &&
115
+ ! isType (labels , " object" )
110
116
) {
111
117
throw new Error (" Labels needs to be a function(value,key) or object." );
112
118
}
113
119
114
- if (isType (this . labels , " function" )) {
115
- return item => {
116
- let _mapKeys = mapKeys (item, this . labels );
120
+ if (isType (labels , " function" )) {
121
+ return ( item : any ) => {
122
+ let _mapKeys = mapKeys (item , labels );
117
123
return _mapKeys ;
118
124
};
119
125
}
120
126
121
- if (isType (this . labels , " object" )) {
127
+ if (isType (labels , " object" )) {
122
128
return item => {
123
129
return mapKeys (item , (item , key ) => {
124
- return this . labels [key] || key;
130
+ return labels [key ] || key ;
125
131
});
126
132
};
127
133
}
128
134
129
135
return item => item ;
130
136
},
131
137
132
- fieldsFunctionGenerator () {
138
+ fieldsFunctionGenerator(): (item : any ) => any {
139
+ let fields: any = this .fields ;
133
140
if (
134
- ! isType (this . fields , " undefined" ) &&
135
- ! isType (this . fields , " function" ) &&
136
- ! isType (this . fields , " object" ) &&
137
- ! Array .isArray (this . fields )
141
+ ! isType (fields , " undefined" ) &&
142
+ ! isType (fields , " function" ) &&
143
+ ! isType (fields , " object" ) &&
144
+ ! Array .isArray (fields )
138
145
) {
139
146
throw new Error (" Fields needs to be a function(value,key) or array." );
140
147
}
141
148
142
149
if (
143
- isType (this . fields , " function" ) ||
144
- (isType (this . fields , " object" ) && ! Array .isArray (this . fields ))
150
+ isType (fields , " function" ) ||
151
+ (isType (fields , " object" ) && ! Array .isArray (fields ))
145
152
) {
146
153
return item => {
147
- return pickBy (item, this . fields );
154
+ return pickBy (item , fields );
148
155
};
149
156
}
150
157
151
- if (Array .isArray (this . fields )) {
158
+ if (Array .isArray (fields )) {
152
159
return item => {
153
- return pick (item, this . fields );
160
+ return pick (item , fields );
154
161
};
155
162
}
156
163
return item => item ;
157
164
},
158
165
159
166
cleaningData() {
160
167
if (
161
- isType (this .fields , " undefined" ) &&
162
- isType (this .labels , " undefined" )
168
+ isType (this .fields , " undefined" ) &&
169
+ isType (this .labels , " undefined" )
163
170
) {
164
171
return this .data ;
165
172
}
@@ -180,14 +187,14 @@ export default {
180
187
}
181
188
182
189
let csv = unparse (
183
- dataExport,
184
- Object .assign (
185
- {
186
- delimiter: this .delimiter ,
187
- encoding: this .encoding
188
- },
189
- this .advancedOptions
190
- )
190
+ dataExport ,
191
+ Object .assign (
192
+ {
193
+ delimiter: this .delimiter ,
194
+ encoding: this .encoding
195
+ },
196
+ this .advancedOptions
197
+ )
191
198
);
192
199
if (this .separatorExcel ) {
193
200
csv = " SEP=" + this .delimiter + " \r\n " + csv ;
@@ -205,7 +212,7 @@ export default {
205
212
}
206
213
}
207
214
}
208
- };
215
+ }) ;
209
216
</script >
210
217
211
218
<style scoped>
0 commit comments