config2ts 2.2.7 → 2.2.9

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
@@ -19,6 +19,7 @@ config2ts -d config -o dist -m csv.ts
19
19
  | Number | number |
20
20
  | Boolean | boolean |
21
21
  | Enum | type |
22
+ | EnumIndex | type |
22
23
  | String[] | string[] |
23
24
  | Number[] | number[] |
24
25
  | Enum[] | type[] |
@@ -26,6 +27,7 @@ config2ts -d config -o dist -m csv.ts
26
27
  * `Number` support Infinity and NaN
27
28
  * `Enum` support empty string type
28
29
  * `Enum[]` do not include empty string type
30
+ * `EnumIndex` will generate index type, and will use `Enum` type to generate interface
29
31
 
30
32
  ## Usage
31
33
 
package/config/data.csv CHANGED
@@ -1,7 +1,7 @@
1
1
  id,name,bool,num,mytype,stringarray,numberarray,typearray,space field/sub
2
2
  Index,String,Boolean,Number,Enum,String[],Number[],Enum[],String
3
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
4
+ 2,xxx,,222,type2," a , b",,"t1, t2",a
5
+ 3,333,,NaN,,,"1,2","t2, t3, t4",b
6
6
  ,,,,,,,,
7
7
  4,444,0,Infinity,type2,",1,,b",",1,,3",,c
@@ -1,108 +1,106 @@
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
-
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
+ numberarray: [
70
+ 1,
71
+ 2,
72
+ ],
73
+ typearray: [
74
+ 't2',
75
+ 't3',
76
+ 't4',
77
+ ],
78
+ 'space field/sub': 'b',
79
+ },
80
+ {
81
+ id: '4',
82
+ name: '444',
83
+ bool: true,
84
+ num: Infinity,
85
+ mytype: 'type2',
86
+ stringarray: [
87
+ '',
88
+ '1',
89
+ '',
90
+ 'b',
91
+ ],
92
+ numberarray: [
93
+ 0,
94
+ 1,
95
+ 0,
96
+ 3,
97
+ ],
98
+ typearray: [],
99
+ 'space field/sub': 'c',
100
+ },
101
+ ];
102
+
103
+ export const Map: { [id: string]: Record } = {};
104
+ for (const v of List) { Map[v.id] = v; };
105
+
108
106
  };
@@ -0,0 +1,5 @@
1
+ name,bool,num
2
+ EnumIndex,Boolean,Number
3
+ 111,1,111
4
+ 222,,222
5
+ 333,,
@@ -0,0 +1,33 @@
1
+ export namespace EnumIndexCsv {
2
+
3
+ export type name = "111" | "222" | "333";
4
+ export const nameList: name[] = ["111", "222", "333"];
5
+
6
+ export interface Record {
7
+ name: name;
8
+ bool: boolean;
9
+ num: number;
10
+ };
11
+
12
+ export const List: Record[] = [
13
+ {
14
+ name: '111',
15
+ bool: true,
16
+ num: 111,
17
+ },
18
+ {
19
+ name: '222',
20
+ bool: false,
21
+ num: 222,
22
+ },
23
+ {
24
+ name: '333',
25
+ bool: false,
26
+ num: 0,
27
+ },
28
+ ];
29
+
30
+ export const Map: { [id: string]: Record } = {};
31
+ for (const v of List) { Map[v.name] = v; };
32
+
33
+ };
@@ -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
@@ -27,8 +27,8 @@ export namespace DataCsv {
27
27
  mytype: 'type1',
28
28
  stringarray: [
29
29
  '1',
30
- ' 2',
31
- ' 3',
30
+ '2',
31
+ '3',
32
32
  ],
33
33
  numberarray: [
34
34
  1,
@@ -65,9 +65,7 @@ export namespace DataCsv {
65
65
  bool: false,
66
66
  num: NaN,
67
67
  mytype: '',
68
- stringarray: [
69
- '',
70
- ],
68
+ stringarray: [],
71
69
  numberarray: [
72
70
  1,
73
71
  2,
@@ -107,6 +105,40 @@ export namespace DataCsv {
107
105
 
108
106
  };
109
107
 
108
+ export namespace EnumIndexCsv {
109
+
110
+ export type name = "111" | "222" | "333";
111
+ export const nameList: name[] = ["111", "222", "333"];
112
+
113
+ export interface Record {
114
+ name: name;
115
+ bool: boolean;
116
+ num: number;
117
+ };
118
+
119
+ export const List: Record[] = [
120
+ {
121
+ name: '111',
122
+ bool: true,
123
+ num: 111,
124
+ },
125
+ {
126
+ name: '222',
127
+ bool: false,
128
+ num: 222,
129
+ },
130
+ {
131
+ name: '333',
132
+ bool: false,
133
+ num: 0,
134
+ },
135
+ ];
136
+
137
+ export const Map: { [id: string]: Record } = {};
138
+ for (const v of List) { Map[v.name] = v; };
139
+
140
+ };
141
+
110
142
  export const IniFileIni = {
111
143
  num: 1,
112
144
  str: 'string',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.2.7",
3
+ "version": "2.2.9",
4
4
  "description": "convert config to ts file.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -11,6 +11,7 @@
11
11
  "config2ts": "./bin/config2ts"
12
12
  },
13
13
  "scripts": {
14
+ "convert": "node ./bin/config2ts -d ./config -o ./config",
14
15
  "test": "jest",
15
16
  "watch-test": "node_modules/.bin/jest --watch"
16
17
  },
package/src/config2ts.js CHANGED
@@ -7,6 +7,7 @@ var changeCase = require("change-case");
7
7
  var toml = require('toml');
8
8
  var packageJson = require('../package.json');
9
9
  var EnumStr = 'Enum';
10
+ var EnumIndexStr = 'EnumIndex';
10
11
  var EnumArrayString = 'Enum[]';
11
12
  var IndexStr = 'Index';
12
13
 
@@ -36,7 +37,12 @@ function csv2ts(csvString, moduleName) {
36
37
  if (global[convert[k]] instanceof Function) {
37
38
  d[k] = global[convert[k]](d[k]);
38
39
  } else if (convert[k] === 'String[]') {
39
- d[k] = d[k].trim().split(',');
40
+ if (d[k] === '') {
41
+ // Enum array do not include empty string
42
+ d[k] = [];
43
+ } else {
44
+ d[k] = d[k].trim().split(',').map((v) => v.trim());
45
+ }
40
46
  } else if (convert[k] === 'Number[]') {
41
47
  d[k] = d[k].trim().split(',').map((val) => Number(val));
42
48
  } else if (convert[k] === 'Enum[]') {
@@ -44,7 +50,7 @@ function csv2ts(csvString, moduleName) {
44
50
  // Enum array do not include empty string
45
51
  d[k] = [];
46
52
  } else {
47
- d[k] = d[k].trim().split(',');
53
+ d[k] = d[k].trim().split(',').map((v) => v.trim());
48
54
  }
49
55
  } else {
50
56
  d[k] = String(d[k]);
@@ -61,7 +67,7 @@ function csv2ts(csvString, moduleName) {
61
67
  var template = "export namespace ".concat(moduleName, " {\n\n");
62
68
  // generate enum type
63
69
  for (var field in convert) {
64
- if (convert[field] == EnumStr) {
70
+ if (convert[field] == EnumStr || convert[field] == EnumIndexStr) {
65
71
  let enumValues = [];
66
72
  for (const row of result) {
67
73
  let enumValue = row[field];
@@ -103,6 +109,9 @@ function csv2ts(csvString, moduleName) {
103
109
  if (convert[field] != '') {
104
110
  if (convert[field] == EnumStr) {
105
111
  fieldType = field;
112
+ } else if (convert[field] == EnumIndexStr) {
113
+ fieldType = field;
114
+ indexField = field;
106
115
  } else if (convert[field] == IndexStr) {
107
116
  indexField = field;
108
117
  } else if (convert[field] == EnumArrayString) {
@@ -12,6 +12,9 @@ test('convert csv file with id', function () {
12
12
  test('convert csv file without id', function () {
13
13
  expect(config2ts.GetTsString('./config/no-id.csv').replace(reg, '')).toBe(getTsFileString('./config/no-id.csv.ts'));
14
14
  });
15
+ // test('convert csv file with enum index', function () {
16
+ // expect(config2ts.GetTsString('./config/enum-index.csv').replace(reg, '')).toBe(getTsFileString('./config/enum-index.csv.ts'));
17
+ // });
15
18
  test('convert ini file', function () {
16
19
  expect(config2ts.GetTsString('./config/ini-file.ini').replace(reg, '')).toBe(getTsFileString('./config/ini-file.ini.ts'));
17
20
  });
@@ -20,7 +23,7 @@ test('convert toml file', function () {
20
23
  expect(config2ts.GetTsString('./config/toml_file.toml').replace(reg, '')).toBe(getTsFileString('./config/toml_file.toml.ts'));
21
24
  });
22
25
  test('config2ts merge csv file list', function () {
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, ''));
26
+ expect(config2ts.GetTsStringFromFileList(["./config/data.csv", "./config/enum-index.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, ''));
24
27
  });
25
28
  test('config2ts GetValidFileList', function () {
26
29
  expect(config2ts.GetValidFileList(['a.csv', 'b.ini', 'c.xxx'])).toEqual(['a.csv', 'b.ini']);
package/data.csv DELETED
@@ -1,7 +0,0 @@
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 DELETED
@@ -1,108 +0,0 @@
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
- };