config2ts 2.2.3 → 2.2.5

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/README.md CHANGED
@@ -1,25 +1,34 @@
1
1
  # config2ts
2
+
2
3
  convert config to ts file.
3
4
 
4
5
  # install
6
+
5
7
  run command: `npm install -g config2ts`
6
8
 
7
9
  # how to use
10
+
8
11
  config2ts -d config -o dist -m csv.ts
9
12
 
10
13
  # support type
11
- | csv field | typescript type |
12
- | :---------: | :------------: |
13
- | Index | string |
14
- | String | string |
15
- | Number | number |
16
- | Boolean | boolean |
17
- | Enum | type |
18
- | String[] | string[] |
19
- | Number[] | number[] |
20
- | Enum[] | type[] |
14
+
15
+ | csv field | typescript type |
16
+ | :-------: | :-------------: |
17
+ | Index | string |
18
+ | String | string |
19
+ | Number | number |
20
+ | Boolean | boolean |
21
+ | Enum | type |
22
+ | String[] | string[] |
23
+ | Number[] | number[] |
24
+ | Enum[] | type[] |
25
+
26
+ * `Number` support Infinity and NaN
27
+ * `Enum` support empty string type
28
+ * `Enum[]` do not include empty string type
21
29
 
22
30
  ## Usage
31
+
23
32
  ```
24
33
  Options:
25
34
 
@@ -30,4 +39,4 @@ config2ts -d config -o dist -m csv.ts
30
39
  -p, --prefix <prefix> set interface prefix. default: ""
31
40
  -s, --suffix <suffix> set interface suffix. default: "Csv"
32
41
  -m, --merge <name> merge all to one ts file. default: csv.ts
33
- ```
42
+ ```
package/config/data.csv CHANGED
@@ -2,5 +2,6 @@ id,name,bool,num,mytype,stringarray,numberarray,typearray
2
2
  Index,String,Boolean,Number,Enum,String[],Number[],Enum[]
3
3
  1,xxx,1,111,type1,"1, 2, 3","1, 2, 3",t1
4
4
  2,xxx,,222,type2,"a,b",,"t1,t2"
5
- 3,333,,33,,,"1,2","t2,t3,t4"
6
- 4,444,0,444,type2,",1,,b",",1,,3",
5
+ 3,333,,NaN,,,"1,2","t2,t3,t4"
6
+ ,,,,,,,
7
+ 4,444,0,Infinity,type2,",1,,b",",1,,3",
@@ -3,8 +3,8 @@ export namespace DataCsv {
3
3
  export type mytype = "type1" | "type2" | "";
4
4
  export const mytypeList: mytype[] = ["type1", "type2", ""];
5
5
 
6
- export type typearray = "t1" | "t2" | "t3" | "t4" | "";
7
- export const typearrayList: typearray[] = ["t1", "t2", "t3", "t4", ""];
6
+ export type typearray = "t1" | "t2" | "t3" | "t4";
7
+ export const typearrayList: typearray[] = ["t1", "t2", "t3", "t4"];
8
8
 
9
9
  export interface Record {
10
10
  id: string;
@@ -19,84 +19,82 @@ export namespace DataCsv {
19
19
 
20
20
  export const List: Record[] = [
21
21
  {
22
- "id": "1",
23
- "name": "xxx",
24
- "bool": true,
25
- "num": 111,
26
- "mytype": "type1",
27
- "stringarray": [
28
- "1",
29
- " 2",
30
- " 3"
22
+ id: '1',
23
+ name: 'xxx',
24
+ bool: true,
25
+ num: 111,
26
+ mytype: 'type1',
27
+ stringarray: [
28
+ '1',
29
+ ' 2',
30
+ ' 3',
31
31
  ],
32
- "numberarray": [
32
+ numberarray: [
33
33
  1,
34
34
  2,
35
- 3
35
+ 3,
36
+ ],
37
+ typearray: [
38
+ 't1',
36
39
  ],
37
- "typearray": [
38
- "t1"
39
- ]
40
40
  },
41
41
  {
42
- "id": "2",
43
- "name": "xxx",
44
- "bool": false,
45
- "num": 222,
46
- "mytype": "type2",
47
- "stringarray": [
48
- "a",
49
- "b"
42
+ id: '2',
43
+ name: 'xxx',
44
+ bool: false,
45
+ num: 222,
46
+ mytype: 'type2',
47
+ stringarray: [
48
+ 'a',
49
+ 'b',
50
+ ],
51
+ numberarray: [
52
+ 0,
50
53
  ],
51
- "numberarray": [
52
- 0
54
+ typearray: [
55
+ 't1',
56
+ 't2',
53
57
  ],
54
- "typearray": [
55
- "t1",
56
- "t2"
57
- ]
58
58
  },
59
59
  {
60
- "id": "3",
61
- "name": "333",
62
- "bool": false,
63
- "num": 33,
64
- "mytype": "",
65
- "stringarray": [
66
- ""
60
+ id: '3',
61
+ name: '333',
62
+ bool: false,
63
+ num: NaN,
64
+ mytype: '',
65
+ stringarray: [
66
+ '',
67
67
  ],
68
- "numberarray": [
68
+ numberarray: [
69
69
  1,
70
- 2
70
+ 2,
71
+ ],
72
+ typearray: [
73
+ 't2',
74
+ 't3',
75
+ 't4',
71
76
  ],
72
- "typearray": [
73
- "t2",
74
- "t3",
75
- "t4"
76
- ]
77
77
  },
78
78
  {
79
- "id": "4",
80
- "name": "444",
81
- "bool": true,
82
- "num": 444,
83
- "mytype": "type2",
84
- "stringarray": [
85
- "",
86
- "1",
87
- "",
88
- "b"
79
+ id: '4',
80
+ name: '444',
81
+ bool: true,
82
+ num: Infinity,
83
+ mytype: 'type2',
84
+ stringarray: [
85
+ '',
86
+ '1',
87
+ '',
88
+ 'b',
89
89
  ],
90
- "numberarray": [
90
+ numberarray: [
91
91
  0,
92
92
  1,
93
93
  0,
94
- 3
94
+ 3,
95
95
  ],
96
- "typearray": [
97
- ""
98
- ]
99
- }
96
+ typearray: [],
97
+ },
100
98
  ];
101
99
 
102
100
  export const Map: { [id: string]: Record } = {};
@@ -8,20 +8,20 @@ export namespace NoIdCsv {
8
8
 
9
9
  export const List: Record[] = [
10
10
  {
11
- "name": "xxx",
12
- "bool": true,
13
- "num": 111
11
+ name: 'xxx',
12
+ bool: true,
13
+ num: 111,
14
14
  },
15
15
  {
16
- "name": "xxx",
17
- "bool": false,
18
- "num": 222
16
+ name: 'xxx',
17
+ bool: false,
18
+ num: 222,
19
19
  },
20
20
  {
21
- "name": "333",
22
- "bool": false,
23
- "num": 0
24
- }
21
+ name: '333',
22
+ bool: false,
23
+ num: 0,
24
+ },
25
25
  ];
26
26
 
27
27
  };
package/config/total.ts CHANGED
@@ -3,8 +3,8 @@ export namespace DataCsv {
3
3
  export type mytype = "type1" | "type2" | "";
4
4
  export const mytypeList: mytype[] = ["type1", "type2", ""];
5
5
 
6
- export type typearray = "t1" | "t2" | "t3" | "t4" | "";
7
- export const typearrayList: typearray[] = ["t1", "t2", "t3", "t4", ""];
6
+ export type typearray = "t1" | "t2" | "t3" | "t4";
7
+ export const typearrayList: typearray[] = ["t1", "t2", "t3", "t4"];
8
8
 
9
9
  export interface Record {
10
10
  id: string;
@@ -19,84 +19,82 @@ export namespace DataCsv {
19
19
 
20
20
  export const List: Record[] = [
21
21
  {
22
- "id": "1",
23
- "name": "xxx",
24
- "bool": true,
25
- "num": 111,
26
- "mytype": "type1",
27
- "stringarray": [
28
- "1",
29
- " 2",
30
- " 3"
22
+ id: '1',
23
+ name: 'xxx',
24
+ bool: true,
25
+ num: 111,
26
+ mytype: 'type1',
27
+ stringarray: [
28
+ '1',
29
+ ' 2',
30
+ ' 3',
31
31
  ],
32
- "numberarray": [
32
+ numberarray: [
33
33
  1,
34
34
  2,
35
- 3
35
+ 3,
36
+ ],
37
+ typearray: [
38
+ 't1',
36
39
  ],
37
- "typearray": [
38
- "t1"
39
- ]
40
40
  },
41
41
  {
42
- "id": "2",
43
- "name": "xxx",
44
- "bool": false,
45
- "num": 222,
46
- "mytype": "type2",
47
- "stringarray": [
48
- "a",
49
- "b"
42
+ id: '2',
43
+ name: 'xxx',
44
+ bool: false,
45
+ num: 222,
46
+ mytype: 'type2',
47
+ stringarray: [
48
+ 'a',
49
+ 'b',
50
+ ],
51
+ numberarray: [
52
+ 0,
50
53
  ],
51
- "numberarray": [
52
- 0
54
+ typearray: [
55
+ 't1',
56
+ 't2',
53
57
  ],
54
- "typearray": [
55
- "t1",
56
- "t2"
57
- ]
58
58
  },
59
59
  {
60
- "id": "3",
61
- "name": "333",
62
- "bool": false,
63
- "num": 33,
64
- "mytype": "",
65
- "stringarray": [
66
- ""
60
+ id: '3',
61
+ name: '333',
62
+ bool: false,
63
+ num: NaN,
64
+ mytype: '',
65
+ stringarray: [
66
+ '',
67
67
  ],
68
- "numberarray": [
68
+ numberarray: [
69
69
  1,
70
- 2
70
+ 2,
71
+ ],
72
+ typearray: [
73
+ 't2',
74
+ 't3',
75
+ 't4',
71
76
  ],
72
- "typearray": [
73
- "t2",
74
- "t3",
75
- "t4"
76
- ]
77
77
  },
78
78
  {
79
- "id": "4",
80
- "name": "444",
81
- "bool": true,
82
- "num": 444,
83
- "mytype": "type2",
84
- "stringarray": [
85
- "",
86
- "1",
87
- "",
88
- "b"
79
+ id: '4',
80
+ name: '444',
81
+ bool: true,
82
+ num: Infinity,
83
+ mytype: 'type2',
84
+ stringarray: [
85
+ '',
86
+ '1',
87
+ '',
88
+ 'b',
89
89
  ],
90
- "numberarray": [
90
+ numberarray: [
91
91
  0,
92
92
  1,
93
93
  0,
94
- 3
94
+ 3,
95
95
  ],
96
- "typearray": [
97
- ""
98
- ]
99
- }
96
+ typearray: [],
97
+ },
100
98
  ];
101
99
 
102
100
  export const Map: { [id: string]: Record } = {};
@@ -124,20 +122,20 @@ export namespace NoIdCsv {
124
122
 
125
123
  export const List: Record[] = [
126
124
  {
127
- "name": "xxx",
128
- "bool": true,
129
- "num": 111
125
+ name: 'xxx',
126
+ bool: true,
127
+ num: 111,
130
128
  },
131
129
  {
132
- "name": "xxx",
133
- "bool": false,
134
- "num": 222
130
+ name: 'xxx',
131
+ bool: false,
132
+ num: 222,
135
133
  },
136
134
  {
137
- "name": "333",
138
- "bool": false,
139
- "num": 0
140
- }
135
+ name: '333',
136
+ bool: false,
137
+ num: 0,
138
+ },
141
139
  ];
142
140
 
143
141
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.2.3",
3
+ "version": "2.2.5",
4
4
  "description": "convert config to ts file.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -29,6 +29,7 @@
29
29
  "commander": "^9.5.0",
30
30
  "d3": "^4.12.2",
31
31
  "ini": "^3.0.1",
32
+ "json5": "^2.2.3",
32
33
  "toml": "^3.0.0"
33
34
  },
34
35
  "devDependencies": {
package/src/config2ts.js CHANGED
@@ -2,6 +2,7 @@ exports.GetValidFileList = exports.GetHeaderInfo = exports.GetTsStringFromFileLi
2
2
  var path = require("path");
3
3
  var fs = require("fs");
4
4
  var d3 = require("d3");
5
+ var json5 = require("json5");
5
6
  var changeCase = require("change-case");
6
7
  var toml = require('toml');
7
8
  var packageJson = require('../package.json');
@@ -31,17 +32,22 @@ function csv2ts(csvString, moduleName) {
31
32
  }
32
33
  else {
33
34
  for (var k in d) {
35
+ // console.log(`convert[k]: ${convert[k]}, d[k]: ${d[k]}, k: ${k}`)
34
36
  if (global[convert[k]] instanceof Function) {
35
37
  d[k] = global[convert[k]](d[k]);
36
- } else {
37
- // console.log(`convert[k]: ${convert[k]}, d[k]: ${d[k]}, k: ${k}`)
38
- if (convert[k] === 'String[]') {
39
- d[k] = d[k].trim().split(',');
40
- } else if (convert[k] === 'Number[]') {
41
- d[k] = d[k].trim().split(',').map((val) => Number(val));
42
- } else if (convert[k] === 'Enum[]') {
38
+ } else if (convert[k] === 'String[]') {
39
+ d[k] = d[k].trim().split(',');
40
+ } else if (convert[k] === 'Number[]') {
41
+ d[k] = d[k].trim().split(',').map((val) => Number(val));
42
+ } else if (convert[k] === 'Enum[]') {
43
+ if (d[k] === '') {
44
+ // Enum array do not include empty string
45
+ d[k] = [];
46
+ } else {
43
47
  d[k] = d[k].trim().split(',');
44
48
  }
49
+ } else {
50
+ d[k] = String(d[k]);
45
51
  }
46
52
  }
47
53
  return d;
@@ -110,10 +116,18 @@ function csv2ts(csvString, moduleName) {
110
116
  template += " ".concat(field, ": ").concat(fieldType, ";\n");
111
117
  }
112
118
  template += ' };\n\n';
119
+
120
+ // remove object when index is empty
121
+ if (indexField) {
122
+ result = result.filter((v) => {
123
+ return v[indexField] !== '';
124
+ })
125
+ }
126
+
113
127
  // generate table
114
128
  template += " export const List: Record[] = ";
115
129
  // console.log(prettyFormat(result, op));
116
- template += JSON.stringify(result, null, 4).replace(/\n/g, '\n ');
130
+ template += json5.stringify(result, null, 4).replace(/\n/g, '\n ');
117
131
  template += ';\n\n';
118
132
  if (indexField != null) {
119
133
  template += " export const Map: { [id: string]: Record } = {};\n";
@@ -7,10 +7,10 @@ function getTsFileString(tsFilePath) {
7
7
  }
8
8
 
9
9
  test('convert csv file with id', function () {
10
- console.log(config2ts.GetTsString('./config/data.csv'))
11
10
  expect(config2ts.GetTsString('./config/data.csv').replace(reg, '')).toBe(getTsFileString('./config/data.csv.ts'));
12
11
  });
13
12
  test('convert csv file without id', function () {
13
+ // console.log(config2ts.GetTsString('./config/no-id.csv'))
14
14
  expect(config2ts.GetTsString('./config/no-id.csv').replace(reg, '')).toBe(getTsFileString('./config/no-id.csv.ts'));
15
15
  });
16
16
  test('convert ini file', function () {