config2ts 2.5.6 → 2.5.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/README.md CHANGED
@@ -31,12 +31,12 @@ config2ts -d config -o dist -n csv.ts -a public
31
31
 
32
32
  * `Number` support Infinity and NaN
33
33
  * `Enum` support empty string type
34
- * `Enum[]` do not include empty string type
34
+ * `Enum[]` union contains every value actually present in the data (including `""` for empty slots)
35
35
  * `EnumIndex` will generate index type, and will use `Enum` type to generate interface
36
36
  * `Ref` / `RefEnum` reference another csv's Record/type by id
37
37
  * `Object` parse `key:value,key:value` format, auto-infer value types (number/boolean/string), generates a dedicated `type` that merges all keys
38
- * `Object[]` parse `key:value,key:value;key:value,key:value` format (`;` separates objects, `,` separates key-value pairs), empty value generates `[]`, generates a dedicated element `type` with the field typed as `type[]`
39
- * Array empty-data convention (`String[]`/`Number[]`/`Enum[]`/`RefEnum[...] []`/`Object[]`): an empty cell generates `[]`; empty segments from stray, leading or trailing separators (e.g. `"a,,b"`, `"1,,3"`, `"x:1;;y:2"`) are skipped — no phantom `""`, `0` or `{}` elements
38
+ * `Object[]` parse comma-separated `key:value` slots — each slot becomes one object (`num:1,,str:a,` makes `[{num:1},{},{str:'a'},{}]`), generates a dedicated element `type` merging all keys with the field typed as `type[]`
39
+ * Array convention (`String[]`/`Number[]`/`Enum[]`/`RefEnum[...] []`/`Object[]`): raw cell values are normalized (line breaks removed, whitespace trimmed); an empty cell generates `[]`; otherwise n commas make n+1 slots and every slot is kept (including interior or trailing ones) so parallel arrays stay index-aligned. Empty slots use the type's own zero value — `''` for string/enum/ref-enum, `0` for number, `{}` for objects; `null` is never generated
40
40
  * unrecognized field types fall back to `string` and print a warning (check for typos)
41
41
 
42
42
  ## assets2ts
package/lib/config2ts.js CHANGED
@@ -91,6 +91,7 @@ function csv2ts(csvString, moduleName) {
91
91
  }
92
92
  else {
93
93
  for (const k in d) {
94
+ d[k] = normalizeCell(d[k]);
94
95
  if (convert[k] === ObjectStr) {
95
96
  d[k] = parseObject(d[k]);
96
97
  }
@@ -101,10 +102,10 @@ function csv2ts(csvString, moduleName) {
101
102
  d[k] = global[convert[k]](d[k]);
102
103
  }
103
104
  else if (convert[k] === "String[]" || convert[k] === "Enum[]") {
104
- d[k] = splitArrayValues(d[k]);
105
+ d[k] = splitStringArray(d[k]);
105
106
  }
106
107
  else if (convert[k] === "Number[]") {
107
- d[k] = splitArrayValues(d[k]).map((val) => Number(val));
108
+ d[k] = splitStringArray(d[k]).map((val) => Number(val));
108
109
  }
109
110
  else if (convert[k].startsWith(RefPrefix) && convert[k].endsWith(RefSuffix)) {
110
111
  if (d[k] === "") {
@@ -114,7 +115,7 @@ function csv2ts(csvString, moduleName) {
114
115
  else if (parseRefEnum(convert[k])) {
115
116
  const refEnum = parseRefEnum(convert[k]);
116
117
  if (refEnum.isArray) {
117
- d[k] = splitArrayValues(d[k]);
118
+ d[k] = splitStringArray(d[k]);
118
119
  }
119
120
  else {
120
121
  if (d[k] === "") {
@@ -286,21 +287,27 @@ function parseObject(str) {
286
287
  }
287
288
  return result;
288
289
  }
289
- function parseObjectArray(str) {
290
- return str
291
- .split(";")
292
- .map((part) => part.trim())
293
- .filter((part) => part !== "")
294
- .map((part) => parseObject(part));
290
+ // Normalize a raw cell value: drop line-break artifacts (e.g. Excel/CSV \r\n)
291
+ // and surrounding whitespace so all downstream parsing sees clean text.
292
+ function normalizeCell(str) {
293
+ return String(str).replace(/\r\n?|\n/g, "").trim();
295
294
  }
296
- // Split a comma-separated array cell, trimming and dropping empty segments
297
- // so stray/leading/trailing commas never produce phantom elements.
295
+ // Split a comma-separated array cell: n commas make n+1 slots and every slot
296
+ // is kept (including an interior or trailing empty one), so parallel arrays
297
+ // stay index-aligned. Empty slots follow the element's own parsing rule
298
+ // ('' for strings/enums, 0 for numbers, {} for objects); null is never emitted.
298
299
  // An empty cell resolves to an empty array.
299
- function splitArrayValues(str) {
300
- return str
301
- .split(",")
302
- .map((v) => v.trim())
303
- .filter((v) => v !== "");
300
+ function splitStringArray(str) {
301
+ if (str === "") {
302
+ return [];
303
+ }
304
+ return str.split(",").map((v) => v.trim());
305
+ }
306
+ function parseObjectArray(str) {
307
+ if (str === "") {
308
+ return [];
309
+ }
310
+ return str.split(",").map((part) => parseObject(part));
304
311
  }
305
312
  function parseObjectValue(value) {
306
313
  if (value === "true")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.5.6",
3
+ "version": "2.5.7",
4
4
  "description": "Generate type-safe TypeScript from CSV/INI/TOML config tables — inferred types, enums, cross-table references, and a typed asset index, all from one CLI command.",
5
5
  "main": "lib/config2ts.js",
6
6
  "types": "lib/config2ts.d.ts",