config2ts 2.2.1 → 2.2.2

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,5 +1,5 @@
1
1
  id,name,bool,num,mytype,stringarray,numberarray
2
- String,String,Boolean,Number,Enum,String[],Number[]
2
+ Index,String,Boolean,Number,Enum,String[],Number[]
3
3
  1,xxx,1,111,type1,"1, 2, 3","1, 2, 3"
4
4
  2,xxx,,222,type2,"a,b",
5
5
  3,333,,33,,,"1,2"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.2.1",
3
+ "version": "2.2.2",
4
4
  "description": "convert config to ts file.",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/src/config2ts.js CHANGED
@@ -1,133 +1,137 @@
1
- exports.GetValidFileList = exports.GetHeaderInfo = exports.GetTsStringFromFileList = exports.GetTsString = exports.Convert = void 0;
2
- var path = require("path");
3
- var fs = require("fs");
4
- var d3 = require("d3");
5
- var changeCase = require("change-case");
6
- var toml = require('toml');
7
- var packageJson = require('../package.json');
8
- var enumStr = 'Enum';
9
-
10
- exports.Convert = {
11
- ini: function (str, moduleName) {
12
- var obj = toml.parse(str);
13
- return "export const ".concat(moduleName, " = ").concat(JSON.stringify(obj, null, 4), ";");
14
- },
15
- csv: function (str, moduleName) {
16
- return csv2ts(str, moduleName);
17
- },
18
- toml: function (str, moduleName) {
19
- var obj = toml.parse(str);
20
- return "export const ".concat(moduleName, " = ").concat(JSON.stringify(obj, null, 4), ";");
21
- }
22
- };
23
- function csv2ts(csvString, moduleName) {
24
- var convert = {};
25
- var result = d3.csvParse(csvString, function (d, i) {
26
- if (i === 0) {
27
- convert = d;
28
- return null;
29
- }
30
- else {
31
- for (var k in d) {
32
- if (global[convert[k]] instanceof Function) {
33
- d[k] = global[convert[k]](d[k]);
34
- } else {
35
- // console.log(`convert[k]: ${convert[k]}, d[k]: ${d[k]}, k: ${k}`)
36
- if (convert[k] === 'String[]') {
37
- d[k] = d[k].trim().split(',');
38
- } else if (convert[k] === 'Number[]') {
39
- d[k] = d[k].trim().split(',').map((val) => Number(val));
40
- }
41
- }
42
- }
43
- return d;
44
- }
45
- });
46
- // 删除columns属性
47
- delete result.columns;
48
- if (result.length <= 0) {
49
- return "";
50
- }
51
- var template = "export namespace ".concat(moduleName, " {\n\n");
52
- // generate enum type
53
- for (var field in convert) {
54
- if (convert[field] == enumStr) {
55
- let enumValues = [];
56
- for (const row of result) {
57
- let enumValue = row[field];
58
- if (!enumValues.includes(enumValue)) {
59
- enumValues.push(enumValue);
60
- }
61
- }
62
-
63
- enumValues = enumValues.map((value) => {return `"${value}"`});
64
- template += ` export type ${field} = ${enumValues.join(' | ')};\n`;
65
- }
66
- }
67
-
68
- // generate interface
69
- template += " export interface Record {\n";
70
- for (var field in convert) {
71
- // var fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
72
-
73
- var fieldType = 'string';
74
-
75
- if (convert[field] != '') {
76
- if (convert[field] == enumStr) {
77
- fieldType = field;
78
- } else {
79
- var fieldType = convert[field].toLowerCase();
80
- }
81
- }
82
-
83
- template += " ".concat(field, ": ").concat(fieldType, ";\n");
84
- }
85
- template += ' };\n\n';
86
- // generate table
87
- template += " export const List: Record[] = ";
88
- // console.log(prettyFormat(result, op));
89
- template += JSON.stringify(result, null, 4).replace(/\n/g, '\n ');
90
- template += ';\n\n';
91
- if (convert['id'] === 'String') {
92
- template += " export const Map: { [id: string]: Record } = {};\n";
93
- template += " for (const v of List) { Map[v.id] = v; };\n\n";
94
- }
95
- template += "};";
96
- return template;
97
- }
98
- function GetFileExt(filePath) {
99
- var pathObject = path.parse(filePath);
100
- return pathObject.ext.slice(1);
101
- }
102
- function GetTsString(filePath) {
103
- var pathObject = path.parse(filePath);
104
- var handle = exports.Convert[GetFileExt(filePath)];
105
- if (handle) {
106
- var moduleName = changeCase.pascalCase(pathObject.base);
107
- var fileString = fs.readFileSync(filePath).toString();
108
- if (fileString.charAt(0) === '\uFEFF') fileString = fileString.substr(1);
109
- return handle(fileString, moduleName);
110
- }
111
- else {
112
- return '';
113
- }
114
- }
115
- exports.GetTsString = GetTsString;
116
- function GetTsStringFromFileList(fileList) {
117
- return fileList.map(function (filePath) {
118
- return GetTsString(filePath);
119
- }).join('\n\n');
120
- }
121
- exports.GetTsStringFromFileList = GetTsStringFromFileList;
122
- function GetHeaderInfo() {
123
- var header = "export let config2ts_version = \"".concat(packageJson.version, "\";\n\n");
124
- header += "export let config2ts_build_timestamp = ".concat(Date.now(), ";\n\n");
125
- return header;
126
- }
127
- exports.GetHeaderInfo = GetHeaderInfo;
128
- function GetValidFileList(fileList) {
129
- return fileList.filter(function (filePath) {
130
- return exports.Convert[GetFileExt(filePath)] != null;
131
- });
132
- }
133
- exports.GetValidFileList = GetValidFileList;
1
+ exports.GetValidFileList = exports.GetHeaderInfo = exports.GetTsStringFromFileList = exports.GetTsString = exports.Convert = void 0;
2
+ var path = require("path");
3
+ var fs = require("fs");
4
+ var d3 = require("d3");
5
+ var changeCase = require("change-case");
6
+ var toml = require('toml');
7
+ var packageJson = require('../package.json');
8
+ var EnumStr = 'Enum';
9
+ var IndexStr = 'Index';
10
+
11
+ exports.Convert = {
12
+ ini: function (str, moduleName) {
13
+ var obj = toml.parse(str);
14
+ return "export const ".concat(moduleName, " = ").concat(JSON.stringify(obj, null, 4), ";");
15
+ },
16
+ csv: function (str, moduleName) {
17
+ return csv2ts(str, moduleName);
18
+ },
19
+ toml: function (str, moduleName) {
20
+ var obj = toml.parse(str);
21
+ return "export const ".concat(moduleName, " = ").concat(JSON.stringify(obj, null, 4), ";");
22
+ }
23
+ };
24
+ function csv2ts(csvString, moduleName) {
25
+ var convert = {};
26
+ var result = d3.csvParse(csvString, function (d, i) {
27
+ if (i === 0) {
28
+ convert = d;
29
+ return null;
30
+ }
31
+ else {
32
+ for (var k in d) {
33
+ if (global[convert[k]] instanceof Function) {
34
+ 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
+ d[k] = d[k].trim().split(',');
39
+ } else if (convert[k] === 'Number[]') {
40
+ d[k] = d[k].trim().split(',').map((val) => Number(val));
41
+ }
42
+ }
43
+ }
44
+ return d;
45
+ }
46
+ });
47
+ // 删除columns属性
48
+ delete result.columns;
49
+ if (result.length <= 0) {
50
+ return "";
51
+ }
52
+ var template = "export namespace ".concat(moduleName, " {\n\n");
53
+ // generate enum type
54
+ for (var field in convert) {
55
+ if (convert[field] == EnumStr) {
56
+ let enumValues = [];
57
+ for (const row of result) {
58
+ let enumValue = row[field];
59
+ if (!enumValues.includes(enumValue)) {
60
+ enumValues.push(enumValue);
61
+ }
62
+ }
63
+
64
+ enumValues = enumValues.map((value) => {return `"${value}"`});
65
+ template += ` export type ${field} = ${enumValues.join(' | ')};\n`;
66
+ }
67
+ }
68
+
69
+ // generate interface
70
+ template += " export interface Record {\n";
71
+ var indexField = null;
72
+ for (var field in convert) {
73
+ // var fieldType = convert[field] !== '' ? convert[field].toLowerCase() : 'string';
74
+
75
+ var fieldType = 'string';
76
+
77
+ if (convert[field] != '') {
78
+ if (convert[field] == EnumStr) {
79
+ fieldType = field;
80
+ } else if (convert[field] == IndexStr) {
81
+ indexField = field;
82
+ } else {
83
+ fieldType = convert[field].toLowerCase();
84
+ }
85
+ }
86
+
87
+ template += " ".concat(field, ": ").concat(fieldType, ";\n");
88
+ }
89
+ template += ' };\n\n';
90
+ // generate table
91
+ template += " export const List: Record[] = ";
92
+ // console.log(prettyFormat(result, op));
93
+ template += JSON.stringify(result, null, 4).replace(/\n/g, '\n ');
94
+ template += ';\n\n';
95
+ if (indexField != null) {
96
+ template += " export const Map: { [id: string]: Record } = {};\n";
97
+ template += ` for (const v of List) { Map[v.${indexField}] = v; };\n\n`;
98
+ }
99
+ template += "};";
100
+ return template;
101
+ }
102
+ function GetFileExt(filePath) {
103
+ var pathObject = path.parse(filePath);
104
+ return pathObject.ext.slice(1);
105
+ }
106
+ function GetTsString(filePath) {
107
+ var pathObject = path.parse(filePath);
108
+ var handle = exports.Convert[GetFileExt(filePath)];
109
+ if (handle) {
110
+ var moduleName = changeCase.pascalCase(pathObject.base);
111
+ var fileString = fs.readFileSync(filePath).toString();
112
+ if (fileString.charAt(0) === '\uFEFF') fileString = fileString.substr(1);
113
+ return handle(fileString, moduleName);
114
+ }
115
+ else {
116
+ return '';
117
+ }
118
+ }
119
+ exports.GetTsString = GetTsString;
120
+ function GetTsStringFromFileList(fileList) {
121
+ return fileList.map(function (filePath) {
122
+ return GetTsString(filePath);
123
+ }).join('\n\n');
124
+ }
125
+ exports.GetTsStringFromFileList = GetTsStringFromFileList;
126
+ function GetHeaderInfo() {
127
+ var header = "export let config2ts_version = \"".concat(packageJson.version, "\";\n\n");
128
+ header += "export let config2ts_build_timestamp = ".concat(Date.now(), ";\n\n");
129
+ return header;
130
+ }
131
+ exports.GetHeaderInfo = GetHeaderInfo;
132
+ function GetValidFileList(fileList) {
133
+ return fileList.filter(function (filePath) {
134
+ return exports.Convert[GetFileExt(filePath)] != null;
135
+ });
136
+ }
137
+ exports.GetValidFileList = GetValidFileList;
@@ -1,22 +1,22 @@
1
- var fs = require("fs");
2
- var config2ts = require("../src/config2ts");
3
- 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());
6
- });
7
- test('convert csv file without id', function () {
8
- expect(config2ts.GetTsString('./config/no-id.csv')).toBe(fs.readFileSync('./config/no-id.csv.ts').toString());
9
- });
10
- test('convert ini file', function () {
11
- expect(config2ts.GetTsString('./config/ini-file.ini')).toBe(fs.readFileSync('./config/ini-file.ini.ts').toString());
12
- });
13
- 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());
16
- });
17
- 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());
19
- });
20
- test('config2ts merge csv file list', function () {
21
- expect(config2ts.GetValidFileList(['a.csv', 'b.ini', 'c.xxx'])).toEqual(['a.csv', 'b.ini']);
22
- });
1
+ var fs = require("fs");
2
+ var config2ts = require("../src/config2ts");
3
+ 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());
6
+ });
7
+ test('convert csv file without id', function () {
8
+ expect(config2ts.GetTsString('./config/no-id.csv')).toBe(fs.readFileSync('./config/no-id.csv.ts').toString());
9
+ });
10
+ test('convert ini file', function () {
11
+ expect(config2ts.GetTsString('./config/ini-file.ini')).toBe(fs.readFileSync('./config/ini-file.ini.ts').toString());
12
+ });
13
+ 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());
16
+ });
17
+ 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());
19
+ });
20
+ test('config2ts merge csv file list', function () {
21
+ expect(config2ts.GetValidFileList(['a.csv', 'b.ini', 'c.xxx'])).toEqual(['a.csv', 'b.ini']);
22
+ });