config2ts 2.2.2 → 2.2.4

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,13 +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
8
- config2ts folder contains csv
10
+
11
+ config2ts -d config -o dist -m csv.ts
12
+
13
+ # support 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
9
29
 
10
30
  ## Usage
31
+
11
32
  ```
12
33
  Options:
13
34
 
@@ -18,4 +39,4 @@ config2ts folder contains csv
18
39
  -p, --prefix <prefix> set interface prefix. default: ""
19
40
  -s, --suffix <suffix> set interface suffix. default: "Csv"
20
41
  -m, --merge <name> merge all to one ts file. default: csv.ts
21
- ```
42
+ ```
package/config/data.csv CHANGED
@@ -1,6 +1,7 @@
1
- id,name,bool,num,mytype,stringarray,numberarray
2
- Index,String,Boolean,Number,Enum,String[],Number[]
3
- 1,xxx,1,111,type1,"1, 2, 3","1, 2, 3"
4
- 2,xxx,,222,type2,"a,b",
5
- 3,333,,33,,,"1,2"
6
- 4,444,0,444,type2,",1,,b",",1,,3"
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,84 +1,103 @@
1
- export namespace DataCsv {
2
-
3
- export type mytype = "type1" | "type2" | "";
4
- export interface Record {
5
- id: string;
6
- name: string;
7
- bool: boolean;
8
- num: number;
9
- mytype: mytype;
10
- stringarray: string[];
11
- numberarray: number[];
12
- };
13
-
14
- export const List: Record[] = [
15
- {
16
- "id": "1",
17
- "name": "xxx",
18
- "bool": true,
19
- "num": 111,
20
- "mytype": "type1",
21
- "stringarray": [
22
- "1",
23
- " 2",
24
- " 3"
25
- ],
26
- "numberarray": [
27
- 1,
28
- 2,
29
- 3
30
- ]
31
- },
32
- {
33
- "id": "2",
34
- "name": "xxx",
35
- "bool": false,
36
- "num": 222,
37
- "mytype": "type2",
38
- "stringarray": [
39
- "a",
40
- "b"
41
- ],
42
- "numberarray": [
43
- 0
44
- ]
45
- },
46
- {
47
- "id": "3",
48
- "name": "333",
49
- "bool": false,
50
- "num": 33,
51
- "mytype": "",
52
- "stringarray": [
53
- ""
54
- ],
55
- "numberarray": [
56
- 1,
57
- 2
58
- ]
59
- },
60
- {
61
- "id": "4",
62
- "name": "444",
63
- "bool": true,
64
- "num": 444,
65
- "mytype": "type2",
66
- "stringarray": [
67
- "",
68
- "1",
69
- "",
70
- "b"
71
- ],
72
- "numberarray": [
73
- 0,
74
- 1,
75
- 0,
76
- 3
77
- ]
78
- }
79
- ];
80
-
81
- export const Map: { [id: string]: Record } = {};
82
- for (const v of List) { Map[v.id] = v; };
83
-
1
+ export namespace DataCsv {
2
+
3
+ export type mytype = "type1" | "type2" | "";
4
+ export const mytypeList: mytype[] = ["type1", "type2", ""];
5
+
6
+ export type typearray = "t1" | "t2" | "t3" | "t4";
7
+ export const typearrayList: typearray[] = ["t1", "t2", "t3", "t4"];
8
+
9
+ export interface Record {
10
+ id: string;
11
+ name: string;
12
+ bool: boolean;
13
+ num: number;
14
+ mytype: mytype;
15
+ stringarray: string[];
16
+ numberarray: number[];
17
+ typearray: typearray[];
18
+ };
19
+
20
+ export const List: Record[] = [
21
+ {
22
+ id: '1',
23
+ name: 'xxx',
24
+ bool: true,
25
+ num: 111,
26
+ mytype: 'type1',
27
+ stringarray: [
28
+ '1',
29
+ ' 2',
30
+ ' 3',
31
+ ],
32
+ numberarray: [
33
+ 1,
34
+ 2,
35
+ 3,
36
+ ],
37
+ typearray: [
38
+ 't1',
39
+ ],
40
+ },
41
+ {
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,
53
+ ],
54
+ typearray: [
55
+ 't1',
56
+ 't2',
57
+ ],
58
+ },
59
+ {
60
+ id: '3',
61
+ name: '333',
62
+ bool: false,
63
+ num: NaN,
64
+ mytype: '',
65
+ stringarray: [
66
+ '',
67
+ ],
68
+ numberarray: [
69
+ 1,
70
+ 2,
71
+ ],
72
+ typearray: [
73
+ 't2',
74
+ 't3',
75
+ 't4',
76
+ ],
77
+ },
78
+ {
79
+ id: '4',
80
+ name: '444',
81
+ bool: true,
82
+ num: Infinity,
83
+ mytype: 'type2',
84
+ stringarray: [
85
+ '',
86
+ '1',
87
+ '',
88
+ 'b',
89
+ ],
90
+ numberarray: [
91
+ 0,
92
+ 1,
93
+ 0,
94
+ 3,
95
+ ],
96
+ typearray: [],
97
+ },
98
+ ];
99
+
100
+ export const Map: { [id: string]: Record } = {};
101
+ for (const v of List) { Map[v.id] = v; };
102
+
84
103
  };
@@ -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
@@ -1,9 +1,9 @@
1
- export const IniFileIni = {
2
- "num": 1,
3
- "str": "string",
4
- "bool": true,
5
- "ItemType": {
6
- "book": 1,
7
- "fruit": 2
8
- }
1
+ export const IniFileIni = {
2
+ "num": 1,
3
+ "str": "string",
4
+ "bool": true,
5
+ "ItemType": {
6
+ "book": 1,
7
+ "fruit": 2
8
+ }
9
9
  };
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,27 +1,27 @@
1
- export namespace NoIdCsv {
2
-
3
- export interface Record {
4
- name: string;
5
- bool: boolean;
6
- num: number;
7
- };
8
-
9
- export const List: Record[] = [
10
- {
11
- "name": "xxx",
12
- "bool": true,
13
- "num": 111
14
- },
15
- {
16
- "name": "xxx",
17
- "bool": false,
18
- "num": 222
19
- },
20
- {
21
- "name": "333",
22
- "bool": false,
23
- "num": 0
24
- }
25
- ];
26
-
1
+ export namespace NoIdCsv {
2
+
3
+ export interface Record {
4
+ name: string;
5
+ bool: boolean;
6
+ num: number;
7
+ };
8
+
9
+ export const List: Record[] = [
10
+ {
11
+ name: 'xxx',
12
+ bool: true,
13
+ num: 111,
14
+ },
15
+ {
16
+ name: 'xxx',
17
+ bool: false,
18
+ num: 222,
19
+ },
20
+ {
21
+ name: '333',
22
+ bool: false,
23
+ num: 0,
24
+ },
25
+ ];
26
+
27
27
  };
@@ -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
@@ -1,8 +1,8 @@
1
- export const TomlFileToml = {
2
- "num": 1,
3
- "str": "test",
4
- "ItemType": {
5
- "book": 1,
6
- "fruit": 2
7
- }
1
+ export const TomlFileToml = {
2
+ "num": 1,
3
+ "str": "test",
4
+ "ItemType": {
5
+ "book": 1,
6
+ "fruit": 2
7
+ }
8
8
  };
package/config/total.ts CHANGED
@@ -1,131 +1,150 @@
1
- export namespace DataCsv {
2
-
3
- export type mytype = "type1" | "type2" | "";
4
- export interface Record {
5
- id: string;
6
- name: string;
7
- bool: boolean;
8
- num: number;
9
- mytype: mytype;
10
- stringarray: string[];
11
- numberarray: number[];
12
- };
13
-
14
- export const List: Record[] = [
15
- {
16
- "id": "1",
17
- "name": "xxx",
18
- "bool": true,
19
- "num": 111,
20
- "mytype": "type1",
21
- "stringarray": [
22
- "1",
23
- " 2",
24
- " 3"
25
- ],
26
- "numberarray": [
27
- 1,
28
- 2,
29
- 3
30
- ]
31
- },
32
- {
33
- "id": "2",
34
- "name": "xxx",
35
- "bool": false,
36
- "num": 222,
37
- "mytype": "type2",
38
- "stringarray": [
39
- "a",
40
- "b"
41
- ],
42
- "numberarray": [
43
- 0
44
- ]
45
- },
46
- {
47
- "id": "3",
48
- "name": "333",
49
- "bool": false,
50
- "num": 33,
51
- "mytype": "",
52
- "stringarray": [
53
- ""
54
- ],
55
- "numberarray": [
56
- 1,
57
- 2
58
- ]
59
- },
60
- {
61
- "id": "4",
62
- "name": "444",
63
- "bool": true,
64
- "num": 444,
65
- "mytype": "type2",
66
- "stringarray": [
67
- "",
68
- "1",
69
- "",
70
- "b"
71
- ],
72
- "numberarray": [
73
- 0,
74
- 1,
75
- 0,
76
- 3
77
- ]
78
- }
79
- ];
80
-
81
- export const Map: { [id: string]: Record } = {};
82
- for (const v of List) { Map[v.id] = v; };
83
-
84
- };
85
-
86
- export const IniFileIni = {
87
- "num": 1,
88
- "str": "string",
89
- "bool": true,
90
- "ItemType": {
91
- "book": 1,
92
- "fruit": 2
93
- }
94
- };
95
-
96
- export namespace NoIdCsv {
97
-
98
- export interface Record {
99
- name: string;
100
- bool: boolean;
101
- num: number;
102
- };
103
-
104
- export const List: Record[] = [
105
- {
106
- "name": "xxx",
107
- "bool": true,
108
- "num": 111
109
- },
110
- {
111
- "name": "xxx",
112
- "bool": false,
113
- "num": 222
114
- },
115
- {
116
- "name": "333",
117
- "bool": false,
118
- "num": 0
119
- }
120
- ];
121
-
122
- };
123
-
124
- export const TomlFileToml = {
125
- "num": 1,
126
- "str": "test",
127
- "ItemType": {
128
- "book": 1,
129
- "fruit": 2
130
- }
1
+ export namespace DataCsv {
2
+
3
+ export type mytype = "type1" | "type2" | "";
4
+ export const mytypeList: mytype[] = ["type1", "type2", ""];
5
+
6
+ export type typearray = "t1" | "t2" | "t3" | "t4";
7
+ export const typearrayList: typearray[] = ["t1", "t2", "t3", "t4"];
8
+
9
+ export interface Record {
10
+ id: string;
11
+ name: string;
12
+ bool: boolean;
13
+ num: number;
14
+ mytype: mytype;
15
+ stringarray: string[];
16
+ numberarray: number[];
17
+ typearray: typearray[];
18
+ };
19
+
20
+ export const List: Record[] = [
21
+ {
22
+ id: '1',
23
+ name: 'xxx',
24
+ bool: true,
25
+ num: 111,
26
+ mytype: 'type1',
27
+ stringarray: [
28
+ '1',
29
+ ' 2',
30
+ ' 3',
31
+ ],
32
+ numberarray: [
33
+ 1,
34
+ 2,
35
+ 3,
36
+ ],
37
+ typearray: [
38
+ 't1',
39
+ ],
40
+ },
41
+ {
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,
53
+ ],
54
+ typearray: [
55
+ 't1',
56
+ 't2',
57
+ ],
58
+ },
59
+ {
60
+ id: '3',
61
+ name: '333',
62
+ bool: false,
63
+ num: NaN,
64
+ mytype: '',
65
+ stringarray: [
66
+ '',
67
+ ],
68
+ numberarray: [
69
+ 1,
70
+ 2,
71
+ ],
72
+ typearray: [
73
+ 't2',
74
+ 't3',
75
+ 't4',
76
+ ],
77
+ },
78
+ {
79
+ id: '4',
80
+ name: '444',
81
+ bool: true,
82
+ num: Infinity,
83
+ mytype: 'type2',
84
+ stringarray: [
85
+ '',
86
+ '1',
87
+ '',
88
+ 'b',
89
+ ],
90
+ numberarray: [
91
+ 0,
92
+ 1,
93
+ 0,
94
+ 3,
95
+ ],
96
+ typearray: [],
97
+ },
98
+ ];
99
+
100
+ export const Map: { [id: string]: Record } = {};
101
+ for (const v of List) { Map[v.id] = v; };
102
+
103
+ };
104
+
105
+ export const IniFileIni = {
106
+ "num": 1,
107
+ "str": "string",
108
+ "bool": true,
109
+ "ItemType": {
110
+ "book": 1,
111
+ "fruit": 2
112
+ }
113
+ };
114
+
115
+ export namespace NoIdCsv {
116
+
117
+ export interface Record {
118
+ name: string;
119
+ bool: boolean;
120
+ num: number;
121
+ };
122
+
123
+ export const List: Record[] = [
124
+ {
125
+ name: 'xxx',
126
+ bool: true,
127
+ num: 111,
128
+ },
129
+ {
130
+ name: 'xxx',
131
+ bool: false,
132
+ num: 222,
133
+ },
134
+ {
135
+ name: '333',
136
+ bool: false,
137
+ num: 0,
138
+ },
139
+ ];
140
+
141
+ };
142
+
143
+ export const TomlFileToml = {
144
+ "num": 1,
145
+ "str": "test",
146
+ "ItemType": {
147
+ "book": 1,
148
+ "fruit": 2
149
+ }
131
150
  };
package/jest.config.js ADDED
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ moduleNameMapper: {
3
+ "^d3$": "<rootDir>/node_modules/d3/dist/d3.min.js",
4
+ },
5
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
4
4
  "description": "convert config to ts file.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -27,8 +27,9 @@
27
27
  "dependencies": {
28
28
  "change-case": "^4.1.2",
29
29
  "commander": "^9.5.0",
30
- "d3": "^4.12.2",
30
+ "d3": "^7.9.0",
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,10 +2,12 @@ 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');
8
9
  var EnumStr = 'Enum';
10
+ var EnumArrayString = 'Enum[]';
9
11
  var IndexStr = 'Index';
10
12
 
11
13
  exports.Convert = {
@@ -30,15 +32,22 @@ function csv2ts(csvString, moduleName) {
30
32
  }
31
33
  else {
32
34
  for (var k in d) {
35
+ // console.log(`convert[k]: ${convert[k]}, d[k]: ${d[k]}, k: ${k}`)
33
36
  if (global[convert[k]] instanceof Function) {
34
37
  d[k] = global[convert[k]](d[k]);
35
- } else {
36
- // console.log(`convert[k]: ${convert[k]}, d[k]: ${d[k]}, k: ${k}`)
37
- if (convert[k] === 'String[]') {
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 {
38
47
  d[k] = d[k].trim().split(',');
39
- } else if (convert[k] === 'Number[]') {
40
- d[k] = d[k].trim().split(',').map((val) => Number(val));
41
48
  }
49
+ } else {
50
+ d[k] = String(d[k]);
42
51
  }
43
52
  }
44
53
  return d;
@@ -63,6 +72,23 @@ function csv2ts(csvString, moduleName) {
63
72
 
64
73
  enumValues = enumValues.map((value) => {return `"${value}"`});
65
74
  template += ` export type ${field} = ${enumValues.join(' | ')};\n`;
75
+ // general enum list for length test
76
+ template += ` export const ${field}List: ${field}[] = [${enumValues.join(', ')}];\n\n`;
77
+ } else if (convert[field] == EnumArrayString) {
78
+ let enumValues = [];
79
+ for (const row of result) {
80
+ let enumArrayValue = row[field];
81
+ for (const enumValue of enumArrayValue) {
82
+ if (!enumValues.includes(enumValue)) {
83
+ enumValues.push(enumValue);
84
+ }
85
+ }
86
+ }
87
+
88
+ enumValues = enumValues.map((value) => {return `"${value}"`});
89
+ template += ` export type ${field} = ${enumValues.join(' | ')};\n`;
90
+ // general enum list for length test
91
+ template += ` export const ${field}List: ${field}[] = [${enumValues.join(', ')}];\n\n`;
66
92
  }
67
93
  }
68
94
 
@@ -71,6 +97,7 @@ function csv2ts(csvString, moduleName) {
71
97
  var indexField = null;
72
98
  for (var field in convert) {
73
99
  // var fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
100
+ // console.log(`generate interface field: ${field}, convert[field]: ${convert[field]}`);
74
101
 
75
102
  var fieldType = 'string';
76
103
 
@@ -79,6 +106,8 @@ function csv2ts(csvString, moduleName) {
79
106
  fieldType = field;
80
107
  } else if (convert[field] == IndexStr) {
81
108
  indexField = field;
109
+ } else if (convert[field] == EnumArrayString) {
110
+ fieldType = field + '[]';
82
111
  } else {
83
112
  fieldType = convert[field].toLowerCase();
84
113
  }
@@ -87,10 +116,18 @@ function csv2ts(csvString, moduleName) {
87
116
  template += " ".concat(field, ": ").concat(fieldType, ";\n");
88
117
  }
89
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
+
90
127
  // generate table
91
128
  template += " export const List: Record[] = ";
92
129
  // console.log(prettyFormat(result, op));
93
- template += JSON.stringify(result, null, 4).replace(/\n/g, '\n ');
130
+ template += json5.stringify(result, null, 4).replace(/\n/g, '\n ');
94
131
  template += ';\n\n';
95
132
  if (indexField != null) {
96
133
  template += " export const Map: { [id: string]: Record } = {};\n";
@@ -1,22 +1,27 @@
1
1
  var fs = require("fs");
2
2
  var config2ts = require("../src/config2ts");
3
+
4
+ var reg = /[\t\r\f\n\s]*/g;
5
+ function getTsFileString(tsFilePath) {
6
+ return fs.readFileSync(tsFilePath).toString().replace(reg, '');
7
+ }
8
+
3
9
  test('convert csv file with id', function () {
4
- // console.log(config2ts.GetTsString('./config/data.csv'));
5
- expect(config2ts.GetTsString('./config/data.csv')).toBe(fs.readFileSync('./config/data.csv.ts').toString());
10
+ expect(config2ts.GetTsString('./config/data.csv').replace(reg, '')).toBe(getTsFileString('./config/data.csv.ts'));
6
11
  });
7
12
  test('convert csv file without id', function () {
8
- expect(config2ts.GetTsString('./config/no-id.csv')).toBe(fs.readFileSync('./config/no-id.csv.ts').toString());
13
+ // console.log(config2ts.GetTsString('./config/no-id.csv'))
14
+ expect(config2ts.GetTsString('./config/no-id.csv').replace(reg, '')).toBe(getTsFileString('./config/no-id.csv.ts'));
9
15
  });
10
16
  test('convert ini file', function () {
11
- expect(config2ts.GetTsString('./config/ini-file.ini')).toBe(fs.readFileSync('./config/ini-file.ini.ts').toString());
17
+ expect(config2ts.GetTsString('./config/ini-file.ini').replace(reg, '')).toBe(getTsFileString('./config/ini-file.ini.ts'));
12
18
  });
13
19
  test('convert toml file', function () {
14
- // console.log(toml.parse(fs.readFileSync('./config/toml_file.toml')));
15
- expect(config2ts.GetTsString('./config/toml_file.toml')).toBe(fs.readFileSync('./config/toml_file.toml.ts').toString());
20
+ expect(config2ts.GetTsString('./config/toml_file.toml').replace(reg, '')).toBe(getTsFileString('./config/toml_file.toml.ts'));
16
21
  });
17
22
  test('config2ts merge csv file list', function () {
18
- expect(config2ts.GetTsStringFromFileList(["./config/data.csv", "./config/ini-file.ini", "./config/no-id.csv", "./config/toml_file.toml"])).toBe(fs.readFileSync('./config/total.ts').toString());
23
+ expect(config2ts.GetTsStringFromFileList(["./config/data.csv", "./config/ini-file.ini", "./config/no-id.csv", "./config/toml_file.toml"]).replace(reg, '')).toBe(fs.readFileSync('./config/total.ts').toString().replace(/\s/g, ''));
19
24
  });
20
- test('config2ts merge csv file list', function () {
25
+ test('config2ts GetValidFileList', function () {
21
26
  expect(config2ts.GetValidFileList(['a.csv', 'b.ini', 'c.xxx'])).toEqual(['a.csv', 'b.ini']);
22
27
  });