Skip to content

Commit 2f105b2

Browse files
committed
perf(TypeScript): Use typescript under the hood
Still can't figure out how to export the type declaration: vuejs/vue-cli#1081
1 parent ca1ffa2 commit 2f105b2

File tree

4 files changed

+271
-48
lines changed

4 files changed

+271
-48
lines changed

Diff for: package.json

+6
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@
3232
"@babel/preset-env": "^7.12.10",
3333
"@semantic-release/changelog": "^6.0.1",
3434
"@semantic-release/git": "^10.0",
35+
"@types/file-saver": "^2.0.5",
36+
"@types/lodash.mapkeys": "^4.6.6",
37+
"@types/lodash.pick": "^4.4.6",
38+
"@types/lodash.pickby": "^4.6.6",
39+
"@types/papaparse": "^5.3.2",
3540
"@vitejs/plugin-vue": "^2.2.0",
3641
"@vue/cli": "^5.0.4",
3742
"@vue/cli-plugin-babel": "~5.0.4",
3843
"@vue/cli-plugin-unit-jest": "~5.0.0",
44+
"@vue/cli-plugin-typescript": "~5.0.0",
3945
"@vue/cli-service": "~5.0.4",
4046
"@vue/compiler-sfc": "^3.2.0",
4147
"@vue/test-utils": "^2.0.0-rc.17",

Diff for: src/JsonCSV.vue

+44-37
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
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'
89
import mapKeys from "lodash.mapkeys";
910
import pickBy from "lodash.pickby";
1011
import 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>

Diff for: tsconfig.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"target": "esnext",
5+
"module": "esnext",
6+
"strict": true,
7+
"jsx": "preserve",
8+
"importHelpers": true,
9+
"moduleResolution": "node",
10+
"skipLibCheck": true,
11+
"esModuleInterop": true,
12+
"allowSyntheticDefaultImports": true,
13+
"sourceMap": true,
14+
"noImplicitAny": false,
15+
"baseUrl": ".",
16+
"types": [
17+
"webpack-env"
18+
],
19+
"paths": {
20+
"@/*": [
21+
"src/*"
22+
]
23+
},
24+
"lib": [
25+
"esnext",
26+
"dom",
27+
"dom.iterable",
28+
"scripthost"
29+
]
30+
},
31+
"include": [
32+
"package/**/*.ts",
33+
"package/**/*.vue",
34+
"src/**/*.ts",
35+
"src/**/*.tsx",
36+
"src/**/*.vue",
37+
"tests/**/*.ts",
38+
"tests/**/*.tsx"
39+
],
40+
"exclude": [
41+
"node_modules"
42+
]
43+
}

0 commit comments

Comments
 (0)