config2ts 2.2.2 → 2.2.3

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
@@ -5,7 +5,19 @@ convert config to ts file.
5
5
  run command: `npm install -g config2ts`
6
6
 
7
7
  # how to use
8
- config2ts folder contains csv
8
+ config2ts -d config -o dist -m csv.ts
9
+
10
+ # 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[] |
9
21
 
10
22
  ## Usage
11
23
  ```
package/config/data.csv CHANGED
@@ -1,6 +1,6 @@
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,,33,,,"1,2","t2,t3,t4"
6
+ 4,444,0,444,type2,",1,,b",",1,,3",
@@ -1,84 +1,105 @@
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": 33,
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": 444,
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
+ ];
101
+
102
+ export const Map: { [id: string]: Record } = {};
103
+ for (const v of List) { Map[v.id] = v; };
104
+
84
105
  };
@@ -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,152 @@
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": 33,
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": 444,
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
+ ];
101
+
102
+ export const Map: { [id: string]: Record } = {};
103
+ for (const v of List) { Map[v.id] = v; };
104
+
105
+ };
106
+
107
+ export const IniFileIni = {
108
+ "num": 1,
109
+ "str": "string",
110
+ "bool": true,
111
+ "ItemType": {
112
+ "book": 1,
113
+ "fruit": 2
114
+ }
115
+ };
116
+
117
+ export namespace NoIdCsv {
118
+
119
+ export interface Record {
120
+ name: string;
121
+ bool: boolean;
122
+ num: number;
123
+ };
124
+
125
+ export const List: Record[] = [
126
+ {
127
+ "name": "xxx",
128
+ "bool": true,
129
+ "num": 111
130
+ },
131
+ {
132
+ "name": "xxx",
133
+ "bool": false,
134
+ "num": 222
135
+ },
136
+ {
137
+ "name": "333",
138
+ "bool": false,
139
+ "num": 0
140
+ }
141
+ ];
142
+
143
+ };
144
+
145
+ export const TomlFileToml = {
146
+ "num": 1,
147
+ "str": "test",
148
+ "ItemType": {
149
+ "book": 1,
150
+ "fruit": 2
151
+ }
131
152
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.2.2",
3
+ "version": "2.2.3",
4
4
  "description": "convert config to ts file.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/src/config2ts.js CHANGED
@@ -6,6 +6,7 @@ var changeCase = require("change-case");
6
6
  var toml = require('toml');
7
7
  var packageJson = require('../package.json');
8
8
  var EnumStr = 'Enum';
9
+ var EnumArrayString = 'Enum[]';
9
10
  var IndexStr = 'Index';
10
11
 
11
12
  exports.Convert = {
@@ -38,6 +39,8 @@ function csv2ts(csvString, moduleName) {
38
39
  d[k] = d[k].trim().split(',');
39
40
  } else if (convert[k] === 'Number[]') {
40
41
  d[k] = d[k].trim().split(',').map((val) => Number(val));
42
+ } else if (convert[k] === 'Enum[]') {
43
+ d[k] = d[k].trim().split(',');
41
44
  }
42
45
  }
43
46
  }
@@ -63,6 +66,23 @@ function csv2ts(csvString, moduleName) {
63
66
 
64
67
  enumValues = enumValues.map((value) => {return `"${value}"`});
65
68
  template += ` export type ${field} = ${enumValues.join(' | ')};\n`;
69
+ // general enum list for length test
70
+ template += ` export const ${field}List: ${field}[] = [${enumValues.join(', ')}];\n\n`;
71
+ } else if (convert[field] == EnumArrayString) {
72
+ let enumValues = [];
73
+ for (const row of result) {
74
+ let enumArrayValue = row[field];
75
+ for (const enumValue of enumArrayValue) {
76
+ if (!enumValues.includes(enumValue)) {
77
+ enumValues.push(enumValue);
78
+ }
79
+ }
80
+ }
81
+
82
+ enumValues = enumValues.map((value) => {return `"${value}"`});
83
+ template += ` export type ${field} = ${enumValues.join(' | ')};\n`;
84
+ // general enum list for length test
85
+ template += ` export const ${field}List: ${field}[] = [${enumValues.join(', ')}];\n\n`;
66
86
  }
67
87
  }
68
88
 
@@ -71,6 +91,7 @@ function csv2ts(csvString, moduleName) {
71
91
  var indexField = null;
72
92
  for (var field in convert) {
73
93
  // var fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
94
+ // console.log(`generate interface field: ${field}, convert[field]: ${convert[field]}`);
74
95
 
75
96
  var fieldType = 'string';
76
97
 
@@ -79,6 +100,8 @@ function csv2ts(csvString, moduleName) {
79
100
  fieldType = field;
80
101
  } else if (convert[field] == IndexStr) {
81
102
  indexField = field;
103
+ } else if (convert[field] == EnumArrayString) {
104
+ fieldType = field + '[]';
82
105
  } else {
83
106
  fieldType = convert[field].toLowerCase();
84
107
  }
@@ -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
+ console.log(config2ts.GetTsString('./config/data.csv'))
11
+ expect(config2ts.GetTsString('./config/data.csv').replace(reg, '')).toBe(getTsFileString('./config/data.csv.ts'));
6
12
  });
7
13
  test('convert csv file without id', function () {
8
- expect(config2ts.GetTsString('./config/no-id.csv')).toBe(fs.readFileSync('./config/no-id.csv.ts').toString());
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
  });