config2ts 2.5.5 → 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 +3 -2
- package/lib/config2ts.js +23 -25
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,11 +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[]` parse `key:value
|
|
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
|
|
39
40
|
* unrecognized field types fall back to `string` and print a warning (check for typos)
|
|
40
41
|
|
|
41
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
|
}
|
|
@@ -100,24 +101,11 @@ function csv2ts(csvString, moduleName) {
|
|
|
100
101
|
else if (typeof global[convert[k]] === "function") {
|
|
101
102
|
d[k] = global[convert[k]](d[k]);
|
|
102
103
|
}
|
|
103
|
-
else if (convert[k] === "String[]") {
|
|
104
|
-
|
|
105
|
-
d[k] = [];
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
d[k] = d[k].trim().split(",").map((v) => v.trim());
|
|
109
|
-
}
|
|
104
|
+
else if (convert[k] === "String[]" || convert[k] === "Enum[]") {
|
|
105
|
+
d[k] = splitStringArray(d[k]);
|
|
110
106
|
}
|
|
111
107
|
else if (convert[k] === "Number[]") {
|
|
112
|
-
d[k] = d[k]
|
|
113
|
-
}
|
|
114
|
-
else if (convert[k] === "Enum[]") {
|
|
115
|
-
if (d[k] === "") {
|
|
116
|
-
d[k] = [];
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
d[k] = d[k].trim().split(",").map((v) => v.trim());
|
|
120
|
-
}
|
|
108
|
+
d[k] = splitStringArray(d[k]).map((val) => Number(val));
|
|
121
109
|
}
|
|
122
110
|
else if (convert[k].startsWith(RefPrefix) && convert[k].endsWith(RefSuffix)) {
|
|
123
111
|
if (d[k] === "") {
|
|
@@ -127,12 +115,7 @@ function csv2ts(csvString, moduleName) {
|
|
|
127
115
|
else if (parseRefEnum(convert[k])) {
|
|
128
116
|
const refEnum = parseRefEnum(convert[k]);
|
|
129
117
|
if (refEnum.isArray) {
|
|
130
|
-
|
|
131
|
-
d[k] = [];
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
d[k] = d[k].trim().split(",").map((v) => v.trim());
|
|
135
|
-
}
|
|
118
|
+
d[k] = splitStringArray(d[k]);
|
|
136
119
|
}
|
|
137
120
|
else {
|
|
138
121
|
if (d[k] === "") {
|
|
@@ -304,12 +287,27 @@ function parseObject(str) {
|
|
|
304
287
|
}
|
|
305
288
|
return result;
|
|
306
289
|
}
|
|
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();
|
|
294
|
+
}
|
|
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.
|
|
299
|
+
// An empty cell resolves to an empty array.
|
|
300
|
+
function splitStringArray(str) {
|
|
301
|
+
if (str === "") {
|
|
302
|
+
return [];
|
|
303
|
+
}
|
|
304
|
+
return str.split(",").map((v) => v.trim());
|
|
305
|
+
}
|
|
307
306
|
function parseObjectArray(str) {
|
|
308
|
-
|
|
309
|
-
if (trimmed === "") {
|
|
307
|
+
if (str === "") {
|
|
310
308
|
return [];
|
|
311
309
|
}
|
|
312
|
-
return
|
|
310
|
+
return str.split(",").map((part) => parseObject(part));
|
|
313
311
|
}
|
|
314
312
|
function parseObjectValue(value) {
|
|
315
313
|
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.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",
|