s2cfgtojson 3.3.0 → 3.4.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 +19 -1
- package/Struct.test.mts +30 -1
- package/package.json +1 -1
package/Struct.mts
CHANGED
|
@@ -156,7 +156,11 @@ export class Struct {
|
|
|
156
156
|
if (typeof obj === "object" && !!obj) {
|
|
157
157
|
const instance = new Struct();
|
|
158
158
|
Object.entries(obj).forEach(([key, value]) => {
|
|
159
|
-
|
|
159
|
+
if (key === "__internal__") {
|
|
160
|
+
instance[key] = new Refs(value);
|
|
161
|
+
} else {
|
|
162
|
+
instance[key] = Struct.fromJson(value);
|
|
163
|
+
}
|
|
160
164
|
});
|
|
161
165
|
return instance as any;
|
|
162
166
|
}
|
|
@@ -164,6 +168,20 @@ export class Struct {
|
|
|
164
168
|
return obj as any;
|
|
165
169
|
}
|
|
166
170
|
|
|
171
|
+
toJson<T extends object>() {
|
|
172
|
+
const obj = {};
|
|
173
|
+
Object.entries(this).forEach(([key, value]) => {
|
|
174
|
+
if (value instanceof Struct) {
|
|
175
|
+
obj[key] = value.toJson();
|
|
176
|
+
} else if (value instanceof Refs) {
|
|
177
|
+
obj[key] = { ...value };
|
|
178
|
+
} else {
|
|
179
|
+
obj[key] = value;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
return obj as T;
|
|
183
|
+
}
|
|
184
|
+
|
|
167
185
|
toString(): string {
|
|
168
186
|
if (!(this.__internal__ instanceof Refs)) {
|
|
169
187
|
this.__internal__ = new Refs(this.__internal__);
|
package/Struct.test.mts
CHANGED
|
@@ -31,11 +31,15 @@ class TradePrototype extends Struct {
|
|
|
31
31
|
class TradeGenerators extends Struct {
|
|
32
32
|
__internal__ = new Refs({
|
|
33
33
|
isArray: true,
|
|
34
|
+
rawName: "TradeGenerators",
|
|
34
35
|
//useAsterisk: true, this option is not supported for now
|
|
35
36
|
});
|
|
36
37
|
"0" = new TradeGenerator();
|
|
37
38
|
}
|
|
38
39
|
class TradeGenerator extends Struct {
|
|
40
|
+
__internal__ = {
|
|
41
|
+
rawName: "0",
|
|
42
|
+
};
|
|
39
43
|
BuyLimitations = new BuyLimitations();
|
|
40
44
|
}
|
|
41
45
|
|
|
@@ -270,7 +274,7 @@ struct.end`;
|
|
|
270
274
|
expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("EItemType::Weapon");
|
|
271
275
|
expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("EItemType::Armor");
|
|
272
276
|
a.TradeGenerators[0].BuyLimitations.forEach(([k]) => {
|
|
273
|
-
a.TradeGenerators[0].BuyLimitations[k] = "forEach";
|
|
277
|
+
a.TradeGenerators[0].BuyLimitations[k] = "forEach" as any;
|
|
274
278
|
});
|
|
275
279
|
expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("forEach");
|
|
276
280
|
expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("forEach");
|
|
@@ -321,6 +325,31 @@ struct.end`;
|
|
|
321
325
|
});
|
|
322
326
|
});
|
|
323
327
|
|
|
328
|
+
describe("toJson", () => {
|
|
329
|
+
test("1", () => {
|
|
330
|
+
const struct = new TradePrototype();
|
|
331
|
+
expect(struct.toJson()).toEqual({
|
|
332
|
+
__internal__: {
|
|
333
|
+
rawName: "TradersDontBuyWeaponsArmor",
|
|
334
|
+
isRoot: true,
|
|
335
|
+
refurl: "../TradePrototypes.cfg",
|
|
336
|
+
refkey: 0,
|
|
337
|
+
},
|
|
338
|
+
TradeGenerators: {
|
|
339
|
+
__internal__: { rawName: "TradeGenerators", isArray: true },
|
|
340
|
+
"0": {
|
|
341
|
+
__internal__: { rawName: "0" },
|
|
342
|
+
BuyLimitations: {
|
|
343
|
+
__internal__: { rawName: "BuyLimitations", isArray: true },
|
|
344
|
+
"0": "EItemType::Weapon",
|
|
345
|
+
"1": "EItemType::Armor",
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
|
|
324
353
|
describe("fromJson", () => {
|
|
325
354
|
test("1", () => {
|
|
326
355
|
const json = {
|