config2ts 2.2.8 → 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
 
@@ -1,106 +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
- 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
-
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
+
106
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
- 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
  };
@@ -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,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
@@ -105,6 +105,40 @@ export namespace DataCsv {
105
105
 
106
106
  };
107
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
+
108
142
  export const IniFileIni = {
109
143
  num: 1,
110
144
  str: 'string',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.2.8",
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
 
@@ -66,7 +67,7 @@ function csv2ts(csvString, moduleName) {
66
67
  var template = "export namespace ".concat(moduleName, " {\n\n");
67
68
  // generate enum type
68
69
  for (var field in convert) {
69
- if (convert[field] == EnumStr) {
70
+ if (convert[field] == EnumStr || convert[field] == EnumIndexStr) {
70
71
  let enumValues = [];
71
72
  for (const row of result) {
72
73
  let enumValue = row[field];
@@ -108,6 +109,9 @@ function csv2ts(csvString, moduleName) {
108
109
  if (convert[field] != '') {
109
110
  if (convert[field] == EnumStr) {
110
111
  fieldType = field;
112
+ } else if (convert[field] == EnumIndexStr) {
113
+ fieldType = field;
114
+ indexField = field;
111
115
  } else if (convert[field] == IndexStr) {
112
116
  indexField = field;
113
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']);