s2cfgtojson 1.0.4 → 2.0.1
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 +26 -2
- package/Struct.test.mts +19 -2
- package/package.json +1 -1
package/Struct.mts
CHANGED
|
@@ -2,6 +2,17 @@ export type Value = Omit<Struct, "toTs"> | string | boolean | number;
|
|
|
2
2
|
type DefaultEntries = { _isArray?: boolean; _useAsterisk?: boolean };
|
|
3
3
|
export type Entries = Record<string | number, Value> & DefaultEntries;
|
|
4
4
|
|
|
5
|
+
export type GetTsType<In extends Struct, E = In["entries"]> = {
|
|
6
|
+
[key in Exclude<keyof E, keyof DefaultEntries>]: E[key] extends Struct
|
|
7
|
+
? GetTsType<E[key]>
|
|
8
|
+
: E[key];
|
|
9
|
+
};
|
|
10
|
+
export type GetStructType<In extends Record<string | number, any>> = Struct<{
|
|
11
|
+
[key in Exclude<keyof In, keyof DefaultEntries>]: In[key] extends In
|
|
12
|
+
? GetStructType<In[key]>
|
|
13
|
+
: In[key];
|
|
14
|
+
}>;
|
|
15
|
+
|
|
5
16
|
/**
|
|
6
17
|
* This file is part of the Stalker 2 Modding Tools project.
|
|
7
18
|
* This is a base class for all structs.
|
|
@@ -110,8 +121,21 @@ export abstract class Struct<T extends Entries = {}> {
|
|
|
110
121
|
return text;
|
|
111
122
|
}
|
|
112
123
|
|
|
113
|
-
toTs(): string {
|
|
114
|
-
|
|
124
|
+
toTs(pretty = false): string {
|
|
125
|
+
const collect = (struct: Struct) => {
|
|
126
|
+
const obj = {};
|
|
127
|
+
if (struct.entries) {
|
|
128
|
+
Object.entries(struct.entries).forEach(([key, value]) => {
|
|
129
|
+
if (value instanceof Struct) {
|
|
130
|
+
obj[key] = collect(value);
|
|
131
|
+
} else {
|
|
132
|
+
obj[key] = value;
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return obj;
|
|
137
|
+
};
|
|
138
|
+
return JSON.stringify(collect(this), null, pretty ? 2 : 0);
|
|
115
139
|
}
|
|
116
140
|
|
|
117
141
|
static addEntry(
|
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
|
|
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
|
|
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();
|