s2cfgtojson 1.0.3 → 2.0.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.
package/Struct.mts CHANGED
@@ -110,8 +110,21 @@ export abstract class Struct<T extends Entries = {}> {
110
110
  return text;
111
111
  }
112
112
 
113
- toTs(): string {
114
- return JSON.stringify(this, null, 2);
113
+ toTs(pretty = false): string {
114
+ const collect = (struct: Struct) => {
115
+ const obj = {};
116
+ if (struct.entries) {
117
+ Object.entries(struct.entries).forEach(([key, value]) => {
118
+ if (value instanceof Struct) {
119
+ obj[key] = collect(value);
120
+ } else {
121
+ obj[key] = value;
122
+ }
123
+ });
124
+ }
125
+ return obj;
126
+ };
127
+ return JSON.stringify(collect(this), null, pretty ? 2 : 0);
115
128
  }
116
129
 
117
130
  static addEntry(
@@ -163,7 +176,7 @@ export abstract class Struct<T extends Entries = {}> {
163
176
  Struct.parseStructName(match[1].trim()) || `UnnamedStruct${index}`;
164
177
 
165
178
  const dummy = new (Struct.createDynamicClass(name))();
166
- dummy._id = match[1];
179
+ dummy._id = match[1].trim();
167
180
  if (match[3]) {
168
181
  const refs = match[3]
169
182
  .split(";")
@@ -190,7 +203,10 @@ export abstract class Struct<T extends Entries = {}> {
190
203
  throw new Error(`Invalid key-value pair: ${line}`);
191
204
  }
192
205
  const key = match[1].trim();
193
- const value = match[3].trim();
206
+ let value: string | number = match[3].trim();
207
+ try {
208
+ value = JSON.parse(value);
209
+ } catch (e) {}
194
210
  Struct.addEntry(parent, key, value, index);
195
211
  };
196
212
  let index = 0;
package/Struct.test.mts CHANGED
@@ -7,7 +7,7 @@ class ChimeraHPFix extends Struct {
7
7
  entries = { MaxHP: 750 };
8
8
  isRoot = true;
9
9
  }
10
- class TradersDontBuyWeaponsArmor extends Struct {
10
+ class TradePrototype extends Struct {
11
11
  _id = "TradersDontBuyWeaponsArmor";
12
12
  refurl = "../TradePrototypes.cfg";
13
13
  refkey = 0;
@@ -35,7 +35,7 @@ describe("Struct", () => {
35
35
  struct.end`,
36
36
  );
37
37
 
38
- expect(new TradersDontBuyWeaponsArmor().toString()).toBe(
38
+ expect(new TradePrototype().toString()).toBe(
39
39
  `TradersDontBuyWeaponsArmor : struct.begin {refurl=../TradePrototypes.cfg;refkey=[0]}
40
40
  TradeGenerators : struct.begin
41
41
  [*] : struct.begin
@@ -54,6 +54,23 @@ struct.end`,
54
54
  expect(Struct.pad(Struct.pad("test"))).toBe(" test");
55
55
  });
56
56
 
57
+ describe("toTs()", () => {
58
+ test("1", () => {
59
+ expect(new TradePrototype().toTs()).toBe(
60
+ JSON.stringify({
61
+ TradeGenerators: {
62
+ "*": {
63
+ BuyLimitations: {
64
+ "0": "EItemType::Weapon",
65
+ "1": "EItemType::Armor",
66
+ },
67
+ },
68
+ },
69
+ }),
70
+ );
71
+ });
72
+ });
73
+
57
74
  describe("fromString()", () => {
58
75
  test("1", () => {
59
76
  const chimeraText = new ChimeraHPFix().toString();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "s2cfgtojson",
3
- "version": "1.0.3",
3
+ "version": "2.0.0",
4
4
  "description": "Converts Stalker 2 Cfg file into a POJO",
5
5
  "keywords": [
6
6
  "stalker",