11<template >
22 <div :id =" idName" @click =" generate" >
3- <slot >Download {{name}}</slot >
3+ <slot >Download {{ name }}</slot >
44 </div >
55</template >
66
7- <script >
7+ <script lang="ts">
8+ import {defineComponent } from ' vue'
89import mapKeys from " lodash.mapkeys" ;
910import pickBy from " lodash.pickby" ;
1011import pick from " lodash.pick" ;
1112
12- import { saveAs } from " file-saver" ;
13- import { unparse } from " papaparse" ;
13+ import {saveAs } from " file-saver" ;
14+ import {unparse } from " papaparse" ;
1415
15- export const isType = (value , type ) => typeof value === type;
16+ export const isType = (value : any , type : string ) => typeof value === type ;
1617
17- export default {
18+ export default defineComponent ( {
1819 name: " JsonCSV" ,
1920 props: {
2021 /**
@@ -30,6 +31,7 @@ export default {
3031 * Can either be an array or a function
3132 */
3233 fields: {
34+ type: [Array , Function ],
3335 required: false
3436 },
3537 /**
@@ -68,14 +70,16 @@ export default {
6870 */
6971 advancedOptions: {
7072 type: Object ,
71- default : () => {}
73+ default : () => {
74+ }
7275 },
7376 /**
7477 * Labels for columns
7578 *
7679 * Object or function
7780 */
7881 labels: {
82+ type: [Object , Function ],
7983 required: false
8084 },
8185 /**
@@ -102,64 +106,67 @@ export default {
102106 }
103107 },
104108 methods: {
105- labelsFunctionGenerator () {
109+
110+ labelsFunctionGenerator(): (item : any ) => any {
111+ let labels: any = this .labels ;
106112 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" )
110116 ) {
111117 throw new Error (" Labels needs to be a function(value,key) or object." );
112118 }
113119
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 );
117123 return _mapKeys ;
118124 };
119125 }
120126
121- if (isType (this . labels , " object" )) {
127+ if (isType (labels , " object" )) {
122128 return item => {
123129 return mapKeys (item , (item , key ) => {
124- return this . labels [key] || key;
130+ return labels [key ] || key ;
125131 });
126132 };
127133 }
128134
129135 return item => item ;
130136 },
131137
132- fieldsFunctionGenerator () {
138+ fieldsFunctionGenerator(): (item : any ) => any {
139+ let fields: any = this .fields ;
133140 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 )
138145 ) {
139146 throw new Error (" Fields needs to be a function(value,key) or array." );
140147 }
141148
142149 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 ))
145152 ) {
146153 return item => {
147- return pickBy (item, this . fields );
154+ return pickBy (item , fields );
148155 };
149156 }
150157
151- if (Array .isArray (this . fields )) {
158+ if (Array .isArray (fields )) {
152159 return item => {
153- return pick (item, this . fields );
160+ return pick (item , fields );
154161 };
155162 }
156163 return item => item ;
157164 },
158165
159166 cleaningData() {
160167 if (
161- isType (this .fields , " undefined" ) &&
162- isType (this .labels , " undefined" )
168+ isType (this .fields , " undefined" ) &&
169+ isType (this .labels , " undefined" )
163170 ) {
164171 return this .data ;
165172 }
@@ -180,14 +187,14 @@ export default {
180187 }
181188
182189 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+ )
191198 );
192199 if (this .separatorExcel ) {
193200 csv = " SEP=" + this .delimiter + " \r\n " + csv ;
@@ -205,7 +212,7 @@ export default {
205212 }
206213 }
207214 }
208- };
215+ }) ;
209216 </script >
210217
211218<style scoped>
0 commit comments