config2ts 2.3.4 → 2.4.0

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.
Files changed (2) hide show
  1. package/lib/config2ts.js +63 -1
  2. package/package.json +1 -1
package/lib/config2ts.js CHANGED
@@ -48,6 +48,7 @@ const EnumStr = "Enum";
48
48
  const EnumIndexStr = "EnumIndex";
49
49
  const EnumArrayString = "Enum[]";
50
50
  const IndexStr = "Index";
51
+ const ObjectStr = "Object";
51
52
  const RefPrefix = "Ref[";
52
53
  const RefSuffix = "]";
53
54
  const RefEnumPrefix = "RefEnum[";
@@ -84,7 +85,10 @@ function csv2ts(csvString, moduleName) {
84
85
  }
85
86
  else {
86
87
  for (const k in d) {
87
- if (typeof global[convert[k]] === "function") {
88
+ if (convert[k] === ObjectStr) {
89
+ d[k] = parseObject(d[k]);
90
+ }
91
+ else if (typeof global[convert[k]] === "function") {
88
92
  d[k] = global[convert[k]](d[k]);
89
93
  }
90
94
  else if (convert[k] === "String[]") {
@@ -166,6 +170,24 @@ function csv2ts(csvString, moduleName) {
166
170
  template += ` export type ${field} = ${enumValueStrings.join(" | ")};\n`;
167
171
  template += ` export const ${field}List: ${field}[] = [${enumValueStrings.join(", ")}];\n\n`;
168
172
  }
173
+ else if (convert[field] == ObjectStr) {
174
+ const keyTypes = {};
175
+ for (const row of result) {
176
+ const objValue = row[field];
177
+ for (const key in objValue) {
178
+ if (!keyTypes[key]) {
179
+ keyTypes[key] = new Set();
180
+ }
181
+ keyTypes[key].add(getTypeName(objValue[key]));
182
+ }
183
+ }
184
+ template += ` export type ${field} = {\n`;
185
+ for (const key in keyTypes) {
186
+ const types = Array.from(keyTypes[key]).join(" | ");
187
+ template += ` ${serializeField(key)}?: ${types};\n`;
188
+ }
189
+ template += ` };\n\n`;
190
+ }
169
191
  }
170
192
  template += " export interface Record {\n";
171
193
  let indexField = null;
@@ -185,6 +207,9 @@ function csv2ts(csvString, moduleName) {
185
207
  else if (convert[field] == EnumArrayString) {
186
208
  fieldType = field + "[]";
187
209
  }
210
+ else if (convert[field] == ObjectStr) {
211
+ fieldType = field;
212
+ }
188
213
  else if (convert[field].startsWith(RefPrefix) && convert[field].endsWith(RefSuffix)) {
189
214
  const refModule = convert[field].slice(RefPrefix.length, -RefSuffix.length);
190
215
  const refModuleName = changeCase.pascalCase(refModule);
@@ -247,6 +272,43 @@ function serializeField(key) {
247
272
  const objString = json5.stringify(obj);
248
273
  return objString.includes("'") ? "'" + key + "'" : key;
249
274
  }
275
+ function parseObject(str) {
276
+ const result = {};
277
+ const trimmed = str.trim();
278
+ if (trimmed === "") {
279
+ return result;
280
+ }
281
+ const pairs = trimmed.split(",");
282
+ for (const pair of pairs) {
283
+ const colonIndex = pair.indexOf(":");
284
+ if (colonIndex > 0) {
285
+ const key = pair.slice(0, colonIndex).trim();
286
+ const value = pair.slice(colonIndex + 1).trim();
287
+ result[key] = parseObjectValue(value);
288
+ }
289
+ }
290
+ return result;
291
+ }
292
+ function parseObjectValue(value) {
293
+ if (value === "true")
294
+ return true;
295
+ if (value === "false")
296
+ return false;
297
+ if (value === "")
298
+ return "";
299
+ const num = Number(value);
300
+ if (!isNaN(num)) {
301
+ return num;
302
+ }
303
+ return value;
304
+ }
305
+ function getTypeName(value) {
306
+ if (typeof value === "number")
307
+ return "number";
308
+ if (typeof value === "boolean")
309
+ return "boolean";
310
+ return "string";
311
+ }
250
312
  function parseRefEnum(typeStr) {
251
313
  if (typeStr.startsWith(RefEnumPrefix)) {
252
314
  let isArray = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.3.4",
3
+ "version": "2.4.0",
4
4
  "description": "convert config to ts file.",
5
5
  "main": "lib/config2ts.js",
6
6
  "types": "lib/config2ts.d.ts",