config2ts 2.5.6 → 2.5.8
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 +3 -3
- package/lib/config2ts.js +30 -16
- package/package.json +1 -1
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[]`
|
|
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[]`
|
|
39
|
-
* Array
|
|
38
|
+
* `Object[]` separates objects with `;` while commas still separate `key:value` pairs inside each object (same syntax as the `Object` type), e.g. `num:1,,str:a;num:2` makes `[{num:1,str:'a'},{num:2}]`, a bare `;` makes `[{},{}]`; generates a dedicated element `type` merging all keys with the field typed as `type[]`
|
|
39
|
+
* Array convention: primitive arrays (`String[]`/`Number[]`/`Enum[]`/`RefEnum[...] []`) separate elements with `,` and `Object[]` separates objects with `;`; raw cell values are normalized (line breaks removed, whitespace trimmed); an empty cell generates `[]`; otherwise n separators 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 empty object slots; `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] =
|
|
105
|
+
d[k] = splitStringArray(d[k]);
|
|
105
106
|
}
|
|
106
107
|
else if (convert[k] === "Number[]") {
|
|
107
|
-
d[k] =
|
|
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] =
|
|
118
|
+
d[k] = splitStringArray(d[k]);
|
|
118
119
|
}
|
|
119
120
|
else {
|
|
120
121
|
if (d[k] === "") {
|
|
@@ -286,21 +287,34 @@ function parseObject(str) {
|
|
|
286
287
|
}
|
|
287
288
|
return result;
|
|
288
289
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
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
|
|
297
|
-
//
|
|
295
|
+
// Split a comma-separated primitive-array cell: n commas make n+1 slots and every
|
|
296
|
+
// slot 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); null is never emitted.
|
|
298
299
|
// An empty cell resolves to an empty array.
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
300
|
+
// Note: Object[] uses semicolons as element separator (see parseObjectArray),
|
|
301
|
+
// because commas already separate key:value pairs inside an object.
|
|
302
|
+
function splitStringArray(str) {
|
|
303
|
+
if (str === "") {
|
|
304
|
+
return [];
|
|
305
|
+
}
|
|
306
|
+
return str.split(",").map((v) => v.trim());
|
|
307
|
+
}
|
|
308
|
+
// Object[] separates objects with semicolons (commas are reserved for the
|
|
309
|
+
// key:value pairs inside an object, same syntax as the Object type); each
|
|
310
|
+
// semicolon slot is parsed by the same object rule, so stray empty pairs within
|
|
311
|
+
// an object (e.g. "num:1,,str:a") are ignored while an empty object slot between
|
|
312
|
+
// semicolons (e.g. "a:1;;b:2") becomes an empty object placeholder.
|
|
313
|
+
function parseObjectArray(str) {
|
|
314
|
+
if (str === "") {
|
|
315
|
+
return [];
|
|
316
|
+
}
|
|
317
|
+
return str.split(";").map((part) => parseObject(part));
|
|
304
318
|
}
|
|
305
319
|
function parseObjectValue(value) {
|
|
306
320
|
if (value === "true")
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "config2ts",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.8",
|
|
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",
|