config2ts 2.6.0 → 2.6.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/README.md CHANGED
@@ -36,10 +36,11 @@ config2ts -d config -o dist -n csv.ts -a public
36
36
  * `Enum[]` union contains every value actually present in the data (including `""` for empty slots)
37
37
  * `EnumIndex` will generate index type, and will use `Enum` type to generate interface
38
38
  * `Ref` / `RefEnum` reference another csv's Record/enum by id: `Ref[file]` is one row (`file.Map["id"]`), `Ref[file][]` is an array of rows with comma-separated ids (`[file.Map["1"],file.Map["2"]]`, typed `File.Record[]`); use `Object[]` for inline private structures and `Ref[]` for shared, identity-bearing rows
39
- * `Template[file]` inherits a whole row from another table and overrides selected fields: a cell is `<baseId>|<key>:<value>,<key>:<value>` — a pipe separates the base row id from Object-style override pairs (e.g. `101|damage:150,range:8`), emitting `{ ...file.Map["101"], damage: 150, range: 8 }` typed `File.Record`; with no overrides the cell is just the id and emits a plain `file.Map["id"]` like `Ref`. `Template[file][]` separates entries with `;` (the same element separator as `Object[]`), e.g. `101|damage:150;102|heal:500` emits `[{ ...file.Map["101"], damage: 150 },{ ...file.Map["102"], heal: 500 }]`. Override key typos and value-type mismatches fail at tsc compile time; overrides are flat top-level fields only, and the target table must have an Index/Map (same file-ordering rule as `Ref`)
39
+ * `Template[file]` inherits a whole row from another table and overrides selected fields: a cell is `<baseId>|<key>:<value>,<key>:<value>` — a pipe separates the base row id from override pairs (e.g. `101|damage:150,range:8`), emitting `{ ...file.Map["101"], damage: 150, range: 8 }` typed `File.Record`; with no overrides the cell is just the id and emits a plain `file.Map["id"]` like `Ref`. Override values may be bare scalars or bracket-wrapped composites mirroring the generated TS syntax — arrays as `[v1,v2]` (e.g. `Params:[6,2.07]`, required for array-typed fields; a bare `Params:6,2.07` gets chopped by the pair comma and prints a warning) and flat objects as `{k:v,k:v}`. `Template[file][]` separates entries with `;` (the same element separator as `Object[]`), e.g. `101|damage:150;102|heal:500` emits `[{ ...file.Map["101"], damage: 150 },{ ...file.Map["102"], heal: 500 }]`. Override key typos and value-type mismatches fail at tsc compile time; overrides are flat top-level fields (no dot-path deep merge), and the target table must have an Index/EnumIndex Map (same file-ordering rule as `Ref`)
40
40
  * `Object` parse `key:value,key:value` format, auto-infer value types (number/boolean/string), generates a dedicated `type` that merges all keys
41
41
  * `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[]`
42
- * 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
42
+ * Array convention: primitive arrays (`String[]`/`Number[]`/`Enum[]`/`RefEnum[...] []`) separate elements with `,` and `Object[]` separates objects with `;`; raw cell values are normalized (CRLF/CR unified to LF, 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
43
+ * Line breaks inside a cell (quote-wrapped in CSV) are preserved for `String` fields (emitted as `\n`) and act as the equivalent separator in structural fields — a line feed equals a comma between array elements / Object pairs / Template override pairs, and equals a semicolon between Object[] elements / Template[] entries (one entry per line works; a blank line is an empty slot by the n+1 rule). Scalar fields (Number/Boolean/Enum/single Ref) have no separator semantics, so a line break there prints a warning and must be removed
43
44
  * unrecognized field types fall back to `string` and print a warning (check for typos)
44
45
 
45
46
  ## assets2ts
package/lib/config2ts.js CHANGED
@@ -57,10 +57,9 @@ const RefEnumPrefix = "RefEnum[";
57
57
  const RefEnumArraySuffix = "][]";
58
58
  const TemplatePrefix = "Template[";
59
59
  // Inside a Template cell, '|' separates the referenced base row id from the
60
- // Object-style override pairs; ';' separates multiple entries in the array
61
- // form (same element separator as Object[]).
60
+ // Object-style override pairs. Entries in the array form (and elements of
61
+ // Object[]/primitive arrays) are separated by ';' and line feeds.
62
62
  const TemplateOverrideSeparator = "|";
63
- const TemplateEntrySeparator = ";";
64
63
  const AutoGenHeader = "// Auto Generated by config2ts, DO NOT EDIT\n\n";
65
64
  exports.Convert = {
66
65
  ini: function (str, moduleName) {
@@ -98,6 +97,15 @@ function csv2ts(csvString, moduleName) {
98
97
  else {
99
98
  for (const k in d) {
100
99
  d[k] = normalizeCell(d[k]);
100
+ // Line feeds are preserved on purpose for free-form String fields
101
+ // (multi-line text such as i18n descriptions), and structural types
102
+ // treat them as entry/pair separators (one element or key:value pair
103
+ // per line). Scalar types (Number/Boolean/Enum/single Ref) have no
104
+ // separator semantics, so a line break there is meaningless — warn
105
+ // and let the author remove it from the cell.
106
+ if (convert[k] !== "String" && convert[k] !== "" && !isStructuralType(convert[k]) && d[k].includes("\n")) {
107
+ console.warn(`[config2ts] warning: ${moduleName} row ${i} field "${k}" of type "${convert[k]}" contains a line break inside the cell; line breaks are only supported in String fields and structural (array/Object/Template) fields, please remove it`);
108
+ }
101
109
  if (convert[k] === ObjectStr) {
102
110
  d[k] = parseObject(d[k]);
103
111
  }
@@ -139,12 +147,19 @@ function csv2ts(csvString, moduleName) {
139
147
  }
140
148
  else if (parseTemplate(convert[k])) {
141
149
  const template = parseTemplate(convert[k]);
150
+ const templateContext = { moduleName, field: k, row: i };
142
151
  if (template.isArray) {
143
- const entries = d[k] === "" ? [] : d[k].split(TemplateEntrySeparator).map((part) => parseTemplateEntry(part));
152
+ // Semicolons and line feeds both separate template entries (one
153
+ // entry per line works in multi-line cells); override pairs inside
154
+ // an entry stay on the same line.
155
+ const entries = d[k] === "" ? [] : d[k].split(/[;\n]/).map((part) => parseTemplateEntry(part, templateContext));
144
156
  for (let entryIndex = 0; entryIndex < entries.length; entryIndex++) {
145
157
  if (entries[entryIndex].baseId === "") {
146
158
  console.warn(`[config2ts] warning: ${moduleName} row ${i} field "${k}" template array entry ${entryIndex + 1} has an empty base id`);
147
159
  }
160
+ else if (entries[entryIndex].baseId.includes(":")) {
161
+ console.warn(`[config2ts] warning: ${moduleName} row ${i} field "${k}" template array entry ${entryIndex + 1} "${entries[entryIndex].baseId}" looks like a key:value pair; each line/semicolon slot must be a full "<baseId>|<overrides>" entry, keep override pairs on the same line`);
162
+ }
148
163
  }
149
164
  d[k] = entries;
150
165
  }
@@ -152,7 +167,7 @@ function csv2ts(csvString, moduleName) {
152
167
  if (d[k] === "") {
153
168
  console.warn(`[config2ts] warning: ${moduleName} row ${i} field "${k}" template value is empty`);
154
169
  }
155
- const entry = parseTemplateEntry(d[k]);
170
+ const entry = parseTemplateEntry(d[k], templateContext);
156
171
  if (d[k] !== "" && entry.baseId === "") {
157
172
  console.warn(`[config2ts] warning: ${moduleName} row ${i} field "${k}" template base id is empty`);
158
173
  }
@@ -334,7 +349,9 @@ function parseObject(str) {
334
349
  if (trimmed === "") {
335
350
  return result;
336
351
  }
337
- const pairs = trimmed.split(",");
352
+ // Commas and line feeds both separate key:value pairs, so one pair per
353
+ // line works in multi-line cells (e.g. "num:1\nstr:a").
354
+ const pairs = trimmed.split(/[,\n]/);
338
355
  for (const pair of pairs) {
339
356
  const colonIndex = pair.indexOf(":");
340
357
  if (colonIndex > 0) {
@@ -345,10 +362,13 @@ function parseObject(str) {
345
362
  }
346
363
  return result;
347
364
  }
348
- // Normalize a raw cell value: drop line-break artifacts (e.g. Excel/CSV \r\n)
349
- // and surrounding whitespace so all downstream parsing sees clean text.
365
+ // Normalize a raw cell value: convert CRLF/CR line breaks to LF and trim
366
+ // surrounding whitespace. Line feeds inside a quoted cell are intentional
367
+ // (multi-line cells commonly use LF as their entry separator, e.g. one effect
368
+ // per line), so they must be preserved — only the Windows/old-Mac line-break
369
+ // variants are unified to LF.
350
370
  function normalizeCell(str) {
351
- return String(str).replace(/\r\n?|\n/g, "").trim();
371
+ return String(str).replace(/\r\n?/g, "\n").trim();
352
372
  }
353
373
  // Split a comma-separated primitive-array cell: n commas make n+1 slots and every
354
374
  // slot is kept (including an interior or trailing empty one), so parallel arrays
@@ -361,18 +381,20 @@ function splitStringArray(str) {
361
381
  if (str === "") {
362
382
  return [];
363
383
  }
364
- return str.split(",").map((v) => v.trim());
384
+ // Commas and line feeds both separate elements, so one value per line
385
+ // works in multi-line cells (e.g. "a\nb" -> ['a','b']).
386
+ return str.split(/[,\n]/).map((v) => v.trim());
365
387
  }
366
- // Object[] separates objects with semicolons (commas are reserved for the
367
- // key:value pairs inside an object, same syntax as the Object type); each
368
- // semicolon slot is parsed by the same object rule, so stray empty pairs within
369
- // an object (e.g. "num:1,,str:a") are ignored while an empty object slot between
370
- // semicolons (e.g. "a:1;;b:2") becomes an empty object placeholder.
388
+ // Object[] separates objects with semicolons or line feeds (commas are reserved
389
+ // for the key:value pairs inside an object, same syntax as the Object type); each
390
+ // slot is parsed by the same object rule, so stray empty pairs within an object
391
+ // (e.g. "num:1,,str:a") are ignored while an empty object slot between separators
392
+ // (e.g. "a:1;;b:2" or a blank line) becomes an empty object placeholder.
371
393
  function parseObjectArray(str) {
372
394
  if (str === "") {
373
395
  return [];
374
396
  }
375
- return str.split(";").map((part) => parseObject(part));
397
+ return str.split(/[;\n]/).map((part) => parseObject(part));
376
398
  }
377
399
  function parseObjectValue(value) {
378
400
  if (value === "true")
@@ -416,6 +438,27 @@ function isKnownType(typeStr) {
416
438
  }
417
439
  return false;
418
440
  }
441
+ // Structural types split cell content on separators (commas/semicolons/pipes),
442
+ // so a line feed can act as the equivalent separator and one entry per line is
443
+ // valid in multi-line cells. Scalar types (Number/Boolean/Enum/single Ref) have
444
+ // no separator semantics, so a line feed in those cells is an error.
445
+ function isStructuralType(typeStr) {
446
+ if (typeStr === ObjectStr || typeStr === ObjectArrayString || typeStr === "String[]" || typeStr === "Number[]" || typeStr === EnumArrayString) {
447
+ return true;
448
+ }
449
+ const ref = parseRef(typeStr);
450
+ if (ref && ref.isArray) {
451
+ return true;
452
+ }
453
+ const refEnum = parseRefEnum(typeStr);
454
+ if (refEnum && refEnum.isArray) {
455
+ return true;
456
+ }
457
+ if (parseTemplate(typeStr)) {
458
+ return true;
459
+ }
460
+ return false;
461
+ }
419
462
  // Parse Ref[module] (single) and Ref[module][] (array of row references).
420
463
  // Note: "RefEnum[...]" starts with "RefE", so it never collides with "Ref[".
421
464
  function parseRef(typeStr) {
@@ -478,18 +521,90 @@ function parseTemplate(typeStr) {
478
521
  return null;
479
522
  }
480
523
  // Parse one Template cell entry: "<baseId>|<key>:<value>,<key>:<value>".
481
- // The part before the first '|' is the base row id; the part after it reuses
482
- // the Object rule for the override pairs. No '|' means zero overrides, which
483
- // is equivalent to a plain Ref.
484
- function parseTemplateEntry(str) {
524
+ // The part before the first '|' is the base row id; the part after it holds
525
+ // the override pairs. Pair splitting is bracket-aware, so composite values
526
+ // are not chopped apart: array values are wrapped in [...] (e.g.
527
+ // "Params:[6,2.07]") and flat object values in {...} (e.g.
528
+ // "params:{damage:100,range:5}"), mirroring the generated TypeScript syntax.
529
+ // No '|' means zero overrides, which is equivalent to a plain Ref.
530
+ function parseTemplateEntry(str, context) {
485
531
  const separatorIndex = str.indexOf(TemplateOverrideSeparator);
486
532
  if (separatorIndex < 0) {
487
533
  return { baseId: str.trim(), overrides: {} };
488
534
  }
489
535
  const baseId = str.slice(0, separatorIndex).trim();
490
- const overrides = parseObject(str.slice(separatorIndex + 1));
536
+ const overrides = parseTemplateOverrides(str.slice(separatorIndex + 1), context);
491
537
  return { baseId, overrides };
492
538
  }
539
+ // Split a string on any character in `separators` while ignoring separators
540
+ // inside [] / {} brackets, so commas inside composite values stay intact.
541
+ // Line feeds are accepted as separators too, so one override pair / array
542
+ // element per line works in multi-line cells.
543
+ function splitBracketAware(str, separators) {
544
+ const parts = [];
545
+ let depth = 0;
546
+ let start = 0;
547
+ for (let i = 0; i < str.length; i++) {
548
+ const ch = str[i];
549
+ if (ch === "[" || ch === "{") {
550
+ depth++;
551
+ }
552
+ else if (ch === "]" || ch === "}") {
553
+ if (depth > 0)
554
+ depth--;
555
+ }
556
+ else if (separators.includes(ch) && depth === 0) {
557
+ parts.push(str.slice(start, i));
558
+ start = i + 1;
559
+ }
560
+ }
561
+ parts.push(str.slice(start));
562
+ return parts;
563
+ }
564
+ // Parse the override part of a Template entry. Scalar pairs reuse the Object
565
+ // rule ("key:value"); a stray segment without a key (e.g. the "2.07" produced
566
+ // by writing "Params:6,2.07" without brackets) is reported instead of being
567
+ // silently dropped.
568
+ function parseTemplateOverrides(str, context) {
569
+ const overrides = {};
570
+ for (const pair of splitBracketAware(str, ",\n")) {
571
+ const colonIndex = pair.indexOf(":");
572
+ const trimmedPair = pair.trim();
573
+ if (colonIndex <= 0) {
574
+ if (trimmedPair !== "" && context) {
575
+ console.warn(`[config2ts] warning: ${context.moduleName} row ${context.row} field "${context.field}" template override segment "${trimmedPair}" is not a key:value pair and was ignored; wrap array values in brackets, e.g. Params:[6,2.07]`);
576
+ }
577
+ continue;
578
+ }
579
+ const key = pair.slice(0, colonIndex).trim();
580
+ overrides[key] = parseOverrideValue(pair.slice(colonIndex + 1));
581
+ }
582
+ return overrides;
583
+ }
584
+ // Parse one override value: "[...]" resolves to an array (elements inferred
585
+ // like Object values, flat {...} elements are supported), "{...}" resolves to
586
+ // a flat object (same rule as the Object type), and anything else resolves to
587
+ // a scalar (number/boolean/string).
588
+ function parseOverrideValue(value) {
589
+ const trimmed = value.trim();
590
+ if (trimmed.startsWith("[") && trimmed.endsWith("]")) {
591
+ const inner = trimmed.slice(1, -1).trim();
592
+ if (inner === "") {
593
+ return [];
594
+ }
595
+ return splitBracketAware(inner, ",\n").map((element) => {
596
+ const elementTrimmed = element.trim();
597
+ if (elementTrimmed.startsWith("{") && elementTrimmed.endsWith("}")) {
598
+ return parseObject(elementTrimmed.slice(1, -1));
599
+ }
600
+ return parseObjectValue(elementTrimmed);
601
+ });
602
+ }
603
+ if (trimmed.startsWith("{") && trimmed.endsWith("}")) {
604
+ return parseObject(trimmed.slice(1, -1));
605
+ }
606
+ return parseObjectValue(trimmed);
607
+ }
493
608
  // Serialize one parsed Template entry to runtime code. Entries without
494
609
  // overrides emit a plain ref (same as Ref: `Module.Map["id"]`); entries with
495
610
  // overrides spread the base record and apply the overrides on top. Because
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "config2ts",
3
- "version": "2.6.0",
3
+ "version": "2.6.2",
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",