config2ts 2.2.5 → 2.2.7

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[] = [
@@ -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',
@@ -74,6 +77,7 @@ export namespace DataCsv {
74
77
  't3',
75
78
  't4',
76
79
  ],
80
+ 'space field/sub': 'b',
77
81
  },
78
82
  {
79
83
  id: '4',
@@ -94,6 +98,7 @@ export namespace DataCsv {
94
98
  3,
95
99
  ],
96
100
  typearray: [],
101
+ 'space field/sub': 'c',
97
102
  },
98
103
  ];
99
104
 
@@ -1,9 +1,9 @@
1
1
  export const IniFileIni = {
2
- "num": 1,
3
- "str": "string",
4
- "bool": true,
5
- "ItemType": {
6
- "book": 1,
7
- "fruit": 2
8
- }
2
+ num: 1,
3
+ str: 'string',
4
+ bool: true,
5
+ ItemType: {
6
+ book: 1,
7
+ fruit: 2,
8
+ },
9
9
  };
@@ -1,8 +1,8 @@
1
1
  export const TomlFileToml = {
2
- "num": 1,
3
- "str": "test",
4
- "ItemType": {
5
- "book": 1,
6
- "fruit": 2
7
- }
2
+ num: 1,
3
+ str: 'test',
4
+ ItemType: {
5
+ book: 1,
6
+ fruit: 2,
7
+ },
8
8
  };
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[] = [
@@ -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',
@@ -74,6 +77,7 @@ export namespace DataCsv {
74
77
  't3',
75
78
  't4',
76
79
  ],
80
+ 'space field/sub': 'b',
77
81
  },
78
82
  {
79
83
  id: '4',
@@ -94,6 +98,7 @@ export namespace DataCsv {
94
98
  3,
95
99
  ],
96
100
  typearray: [],
101
+ 'space field/sub': 'c',
97
102
  },
98
103
  ];
99
104
 
@@ -103,13 +108,13 @@ export namespace DataCsv {
103
108
  };
104
109
 
105
110
  export const IniFileIni = {
106
- "num": 1,
107
- "str": "string",
108
- "bool": true,
109
- "ItemType": {
110
- "book": 1,
111
- "fruit": 2
112
- }
111
+ num: 1,
112
+ str: 'string',
113
+ bool: true,
114
+ ItemType: {
115
+ book: 1,
116
+ fruit: 2,
117
+ },
113
118
  };
114
119
 
115
120
  export namespace NoIdCsv {
@@ -141,10 +146,10 @@ export namespace NoIdCsv {
141
146
  };
142
147
 
143
148
  export const TomlFileToml = {
144
- "num": 1,
145
- "str": "test",
146
- "ItemType": {
147
- "book": 1,
148
- "fruit": 2
149
- }
149
+ num: 1,
150
+ str: 'test',
151
+ ItemType: {
152
+ book: 1,
153
+ fruit: 2,
154
+ },
150
155
  };
package/data.csv ADDED
@@ -0,0 +1,7 @@
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
package/data.csv.ts ADDED
@@ -0,0 +1,108 @@
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
+ space field/sub: string;
19
+ };
20
+
21
+ export const List: Record[] = [
22
+ {
23
+ id: '1',
24
+ name: 'xxx',
25
+ bool: true,
26
+ num: 111,
27
+ mytype: 'type1',
28
+ stringarray: [
29
+ '1',
30
+ ' 2',
31
+ ' 3',
32
+ ],
33
+ numberarray: [
34
+ 1,
35
+ 2,
36
+ 3,
37
+ ],
38
+ typearray: [
39
+ 't1',
40
+ ],
41
+ 'space field/sub': 'a',
42
+ },
43
+ {
44
+ id: '2',
45
+ name: 'xxx',
46
+ bool: false,
47
+ num: 222,
48
+ mytype: 'type2',
49
+ stringarray: [
50
+ 'a',
51
+ 'b',
52
+ ],
53
+ numberarray: [
54
+ 0,
55
+ ],
56
+ typearray: [
57
+ 't1',
58
+ 't2',
59
+ ],
60
+ 'space field/sub': 'a',
61
+ },
62
+ {
63
+ id: '3',
64
+ name: '333',
65
+ bool: false,
66
+ num: NaN,
67
+ mytype: '',
68
+ stringarray: [
69
+ '',
70
+ ],
71
+ numberarray: [
72
+ 1,
73
+ 2,
74
+ ],
75
+ typearray: [
76
+ 't2',
77
+ 't3',
78
+ 't4',
79
+ ],
80
+ 'space field/sub': 'b',
81
+ },
82
+ {
83
+ id: '4',
84
+ name: '444',
85
+ bool: true,
86
+ num: Infinity,
87
+ mytype: 'type2',
88
+ stringarray: [
89
+ '',
90
+ '1',
91
+ '',
92
+ 'b',
93
+ ],
94
+ numberarray: [
95
+ 0,
96
+ 1,
97
+ 0,
98
+ 3,
99
+ ],
100
+ typearray: [],
101
+ 'space field/sub': 'c',
102
+ },
103
+ ];
104
+
105
+ export const Map: { [id: string]: Record } = {};
106
+ for (const v of List) { Map[v.id] = v; };
107
+
108
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.2.5",
3
+ "version": "2.2.7",
4
4
  "description": "convert config to ts file.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/src/config2ts.js CHANGED
@@ -13,14 +13,14 @@ var IndexStr = 'Index';
13
13
  exports.Convert = {
14
14
  ini: function (str, moduleName) {
15
15
  var obj = toml.parse(str);
16
- return "export const ".concat(moduleName, " = ").concat(JSON.stringify(obj, null, 4), ";");
16
+ return "export const ".concat(moduleName, " = ").concat(json5.stringify(obj, null, 4), ";");
17
17
  },
18
18
  csv: function (str, moduleName) {
19
19
  return csv2ts(str, moduleName);
20
20
  },
21
21
  toml: function (str, moduleName) {
22
22
  var obj = toml.parse(str);
23
- return "export const ".concat(moduleName, " = ").concat(JSON.stringify(obj, null, 4), ";");
23
+ return "export const ".concat(moduleName, " = ").concat(json5.stringify(obj, null, 4), ";");
24
24
  }
25
25
  };
26
26
  function csv2ts(csvString, moduleName) {
@@ -98,7 +98,6 @@ function csv2ts(csvString, moduleName) {
98
98
  for (var field in convert) {
99
99
  // var fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
100
100
  // console.log(`generate interface field: ${field}, convert[field]: ${convert[field]}`);
101
-
102
101
  var fieldType = 'string';
103
102
 
104
103
  if (convert[field] != '') {
@@ -113,6 +112,7 @@ function csv2ts(csvString, moduleName) {
113
112
  }
114
113
  }
115
114
 
115
+ field = serializeField(field)
116
116
  template += " ".concat(field, ": ").concat(fieldType, ";\n");
117
117
  }
118
118
  template += ' };\n\n';
@@ -136,6 +136,19 @@ function csv2ts(csvString, moduleName) {
136
136
  template += "};";
137
137
  return template;
138
138
  }
139
+
140
+ /**
141
+ * serialize interface field
142
+ * @param {string} field
143
+ */
144
+ function serializeField(key) {
145
+ const obj = {}
146
+ obj[key] = true
147
+ const objString = json5.stringify(obj);
148
+ // console.log('serializeField:', json5.stringify(obj))
149
+ return objString.includes("'") ? "'" + key + "'" : key;
150
+ }
151
+
139
152
  function GetFileExt(filePath) {
140
153
  var pathObject = path.parse(filePath);
141
154
  return pathObject.ext.slice(1);
@@ -10,13 +10,13 @@ test('convert csv file with id', function () {
10
10
  expect(config2ts.GetTsString('./config/data.csv').replace(reg, '')).toBe(getTsFileString('./config/data.csv.ts'));
11
11
  });
12
12
  test('convert csv file without id', function () {
13
- // console.log(config2ts.GetTsString('./config/no-id.csv'))
14
13
  expect(config2ts.GetTsString('./config/no-id.csv').replace(reg, '')).toBe(getTsFileString('./config/no-id.csv.ts'));
15
14
  });
16
15
  test('convert ini file', function () {
17
16
  expect(config2ts.GetTsString('./config/ini-file.ini').replace(reg, '')).toBe(getTsFileString('./config/ini-file.ini.ts'));
18
17
  });
19
18
  test('convert toml file', function () {
19
+ // console.log(config2ts.GetTsString('./config/toml_file.toml'))
20
20
  expect(config2ts.GetTsString('./config/toml_file.toml').replace(reg, '')).toBe(getTsFileString('./config/toml_file.toml.ts'));
21
21
  });
22
22
  test('config2ts merge csv file list', function () {