config2ts 2.2.6 → 2.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/config/data.csv CHANGED
@@ -1,7 +1,7 @@
1
- id,name,bool,num,mytype,stringarray,numberarray,typearray
2
- Index,String,Boolean,Number,Enum,String[],Number[],Enum[]
3
- 1,xxx,1,111,type1,"1, 2, 3","1, 2, 3",t1
4
- 2,xxx,,222,type2,"a,b",,"t1,t2"
5
- 3,333,,NaN,,,"1,2","t2,t3,t4"
6
- ,,,,,,,
7
- 4,444,0,Infinity,type2,",1,,b",",1,,3",
1
+ id,name,bool,num,mytype,stringarray,numberarray,typearray,space field/sub
2
+ Index,String,Boolean,Number,Enum,String[],Number[],Enum[],String
3
+ 1,xxx,1,111,type1,"1, 2, 3","1, 2, 3",t1,a
4
+ 2,xxx,,222,type2," a , b",,"t1, t2",a
5
+ 3,333,,NaN,,,"1,2","t2, t3, t4",b
6
+ ,,,,,,,,
7
+ 4,444,0,Infinity,type2,",1,,b",",1,,3",,c
@@ -15,6 +15,7 @@ export namespace DataCsv {
15
15
  stringarray: string[];
16
16
  numberarray: number[];
17
17
  typearray: typearray[];
18
+ 'space field/sub': string;
18
19
  };
19
20
 
20
21
  export const List: Record[] = [
@@ -26,8 +27,8 @@ export namespace DataCsv {
26
27
  mytype: 'type1',
27
28
  stringarray: [
28
29
  '1',
29
- ' 2',
30
- ' 3',
30
+ '2',
31
+ '3',
31
32
  ],
32
33
  numberarray: [
33
34
  1,
@@ -37,6 +38,7 @@ export namespace DataCsv {
37
38
  typearray: [
38
39
  't1',
39
40
  ],
41
+ 'space field/sub': 'a',
40
42
  },
41
43
  {
42
44
  id: '2',
@@ -55,6 +57,7 @@ export namespace DataCsv {
55
57
  't1',
56
58
  't2',
57
59
  ],
60
+ 'space field/sub': 'a',
58
61
  },
59
62
  {
60
63
  id: '3',
@@ -62,9 +65,7 @@ export namespace DataCsv {
62
65
  bool: false,
63
66
  num: NaN,
64
67
  mytype: '',
65
- stringarray: [
66
- '',
67
- ],
68
+ stringarray: [],
68
69
  numberarray: [
69
70
  1,
70
71
  2,
@@ -74,6 +75,7 @@ export namespace DataCsv {
74
75
  't3',
75
76
  't4',
76
77
  ],
78
+ 'space field/sub': 'b',
77
79
  },
78
80
  {
79
81
  id: '4',
@@ -94,6 +96,7 @@ export namespace DataCsv {
94
96
  3,
95
97
  ],
96
98
  typearray: [],
99
+ 'space field/sub': 'c',
97
100
  },
98
101
  ];
99
102
 
@@ -1,9 +1,9 @@
1
- num = 1
2
-
3
- str = "string"
4
-
5
- bool = true
6
-
7
- [ItemType]
8
- book = 1
9
- fruit = 2
1
+ num = 1
2
+
3
+ str = "string"
4
+
5
+ bool = true
6
+
7
+ [ItemType]
8
+ book = 1
9
+ fruit = 2
package/config/no-id.csv CHANGED
@@ -1,5 +1,5 @@
1
- name,bool,num
2
- String,Boolean,Number
3
- "xxx",1,111
4
- "xxx",,222
1
+ name,bool,num
2
+ String,Boolean,Number
3
+ "xxx",1,111
4
+ "xxx",,222
5
5
  333,,
@@ -1,7 +1,7 @@
1
- num = 1
2
-
3
- str = "test"
4
-
5
- [ItemType]
6
- book = 1
7
- fruit = 2
1
+ num = 1
2
+
3
+ str = "test"
4
+
5
+ [ItemType]
6
+ book = 1
7
+ fruit = 2
package/config/total.ts CHANGED
@@ -15,6 +15,7 @@ export namespace DataCsv {
15
15
  stringarray: string[];
16
16
  numberarray: number[];
17
17
  typearray: typearray[];
18
+ 'space field/sub': string;
18
19
  };
19
20
 
20
21
  export const List: Record[] = [
@@ -26,8 +27,8 @@ export namespace DataCsv {
26
27
  mytype: 'type1',
27
28
  stringarray: [
28
29
  '1',
29
- ' 2',
30
- ' 3',
30
+ '2',
31
+ '3',
31
32
  ],
32
33
  numberarray: [
33
34
  1,
@@ -37,6 +38,7 @@ export namespace DataCsv {
37
38
  typearray: [
38
39
  't1',
39
40
  ],
41
+ 'space field/sub': 'a',
40
42
  },
41
43
  {
42
44
  id: '2',
@@ -55,6 +57,7 @@ export namespace DataCsv {
55
57
  't1',
56
58
  't2',
57
59
  ],
60
+ 'space field/sub': 'a',
58
61
  },
59
62
  {
60
63
  id: '3',
@@ -62,9 +65,7 @@ export namespace DataCsv {
62
65
  bool: false,
63
66
  num: NaN,
64
67
  mytype: '',
65
- stringarray: [
66
- '',
67
- ],
68
+ stringarray: [],
68
69
  numberarray: [
69
70
  1,
70
71
  2,
@@ -74,6 +75,7 @@ export namespace DataCsv {
74
75
  't3',
75
76
  't4',
76
77
  ],
78
+ 'space field/sub': 'b',
77
79
  },
78
80
  {
79
81
  id: '4',
@@ -94,6 +96,7 @@ export namespace DataCsv {
94
96
  3,
95
97
  ],
96
98
  typearray: [],
99
+ 'space field/sub': 'c',
97
100
  },
98
101
  ];
99
102
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.2.6",
3
+ "version": "2.2.8",
4
4
  "description": "convert config to ts file.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/src/config2ts.js CHANGED
@@ -36,7 +36,12 @@ function csv2ts(csvString, moduleName) {
36
36
  if (global[convert[k]] instanceof Function) {
37
37
  d[k] = global[convert[k]](d[k]);
38
38
  } else if (convert[k] === 'String[]') {
39
- d[k] = d[k].trim().split(',');
39
+ if (d[k] === '') {
40
+ // Enum array do not include empty string
41
+ d[k] = [];
42
+ } else {
43
+ d[k] = d[k].trim().split(',').map((v) => v.trim());
44
+ }
40
45
  } else if (convert[k] === 'Number[]') {
41
46
  d[k] = d[k].trim().split(',').map((val) => Number(val));
42
47
  } else if (convert[k] === 'Enum[]') {
@@ -44,7 +49,7 @@ function csv2ts(csvString, moduleName) {
44
49
  // Enum array do not include empty string
45
50
  d[k] = [];
46
51
  } else {
47
- d[k] = d[k].trim().split(',');
52
+ d[k] = d[k].trim().split(',').map((v) => v.trim());
48
53
  }
49
54
  } else {
50
55
  d[k] = String(d[k]);
@@ -98,7 +103,6 @@ function csv2ts(csvString, moduleName) {
98
103
  for (var field in convert) {
99
104
  // var fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
100
105
  // console.log(`generate interface field: ${field}, convert[field]: ${convert[field]}`);
101
-
102
106
  var fieldType = 'string';
103
107
 
104
108
  if (convert[field] != '') {
@@ -113,6 +117,7 @@ function csv2ts(csvString, moduleName) {
113
117
  }
114
118
  }
115
119
 
120
+ field = serializeField(field)
116
121
  template += " ".concat(field, ": ").concat(fieldType, ";\n");
117
122
  }
118
123
  template += ' };\n\n';
@@ -136,6 +141,19 @@ function csv2ts(csvString, moduleName) {
136
141
  template += "};";
137
142
  return template;
138
143
  }
144
+
145
+ /**
146
+ * serialize interface field
147
+ * @param {string} field
148
+ */
149
+ function serializeField(key) {
150
+ const obj = {}
151
+ obj[key] = true
152
+ const objString = json5.stringify(obj);
153
+ // console.log('serializeField:', json5.stringify(obj))
154
+ return objString.includes("'") ? "'" + key + "'" : key;
155
+ }
156
+
139
157
  function GetFileExt(filePath) {
140
158
  var pathObject = path.parse(filePath);
141
159
  return pathObject.ext.slice(1);