s2cfgtojson 3.1.0 → 3.2.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
@@ -1,6 +1,6 @@
1
1
  export * from "./types.mts";
2
2
  export * from "./enums.mts";
3
- import { DefaultEntries } from "./types.mts";
3
+ import { DefaultEntries, Internal } from "./types.mts";
4
4
 
5
5
  const TAB = " ";
6
6
  const WILDCARD = "_wildcard";
@@ -11,6 +11,17 @@ const KEYWORDS = [
11
11
  "bpatch", // allows patching only specific keys
12
12
  ];
13
13
  const REMOVE_NODE = "removenode";
14
+ const INTERNAL_PROPS = new Set([
15
+ "__internal__",
16
+ "fork",
17
+ "removeNode",
18
+ "addNode",
19
+ "clone",
20
+ "forEach",
21
+ "filter",
22
+ "map",
23
+ "toString",
24
+ ]);
14
25
 
15
26
  /**
16
27
  * This file is part of the Stalker 2 Modding Tools project.
@@ -83,25 +94,32 @@ export class Struct {
83
94
  return Struct.fromString(this.toString())[0] as this;
84
95
  }
85
96
 
86
- forEach(callback: ([key, value]) => void): void {
87
- Object.entries(this)
88
- .filter(([key]) => key !== "__internal__")
89
- .forEach(callback);
97
+ entries(): [keyof this, (typeof this)[keyof this]][] {
98
+ return Object.entries(this).filter(([key]) => !INTERNAL_PROPS.has(key)) as [
99
+ keyof this,
100
+ (typeof this)[keyof this],
101
+ ][];
102
+ }
103
+
104
+ forEach<K extends Exclude<keyof this, Internal>, V extends (typeof this)[K]>(
105
+ callback: ([key, value]: [K, V]) => void,
106
+ ): void {
107
+ this.entries().forEach(([key, value]) => callback([key as K, value as V]));
90
108
  }
91
109
 
92
110
  /**
93
111
  * Filters the struct entries based on a callback function. Returns a copy.
94
112
  * @param callback
95
113
  */
96
- filter(callback: ([key, value]) => boolean): Partial<this> & Struct {
114
+ filter<K extends Exclude<keyof this, Internal>, V extends (typeof this)[K]>(
115
+ callback: ([key, value]: [K, V]) => boolean,
116
+ ): Partial<this> & Struct {
97
117
  const clone = this.clone();
98
- Object.entries(clone)
99
- .filter(([key]) => key !== "__internal__")
100
- .forEach(([key, value]) => {
101
- if (!callback([key, value])) {
102
- delete clone[key];
103
- }
104
- });
118
+ clone.entries().forEach(([key, value]) => {
119
+ if (!callback([key as K, value as V])) {
120
+ delete clone[key];
121
+ }
122
+ });
105
123
  return clone;
106
124
  }
107
125
 
@@ -109,13 +127,13 @@ export class Struct {
109
127
  * Maps the struct entries based on a callback function. Returns a copy.
110
128
  * @param callback
111
129
  */
112
- map(callback: ([key, value]) => typeof value): this {
130
+ map<K extends Exclude<keyof this, Internal>, V extends (typeof this)[K]>(
131
+ callback: ([key, value]: [K, V]) => V,
132
+ ): this {
113
133
  const clone = this.clone();
114
- Object.entries(clone)
115
- .filter(([key]) => key !== "__internal__")
116
- .forEach(([key, value]) => {
117
- clone[key] = callback([key, value]);
118
- });
134
+ clone.entries().forEach(([key, value]) => {
135
+ clone[key] = callback([key as K, value as V]);
136
+ });
119
137
  return clone;
120
138
  }
121
139
 
@@ -123,23 +141,26 @@ export class Struct {
123
141
  if (!(this.__internal__ instanceof Refs)) {
124
142
  this.__internal__ = new Refs(this.__internal__);
125
143
  }
126
- const { __internal__: internal, ...entries } = this;
127
144
 
128
- let text: string = internal.rawName ? `${internal.rawName} : ` : "";
145
+ let text: string = this.__internal__.rawName
146
+ ? `${this.__internal__.rawName} : `
147
+ : "";
129
148
  text += "struct.begin";
130
149
 
131
- const refs = internal.toString();
150
+ const refs = this.__internal__.toString();
132
151
  if (refs) {
133
152
  text += ` {${refs}}`;
134
153
  }
135
154
 
136
155
  text += "\n";
137
156
  // Add all keys
138
- text += Object.entries(entries || {})
157
+ text += Object.entries(this)
158
+ .filter(([key]) => !INTERNAL_PROPS.has(key))
139
159
  .map(([key, value]) => {
140
160
  const nameAlreadyRendered =
141
161
  value instanceof Struct && value.__internal__.rawName;
142
- const useAsterisk = internal.isArray && internal.useAsterisk;
162
+ const useAsterisk =
163
+ this.__internal__.isArray && this.__internal__.useAsterisk;
143
164
  let keyOrIndex = "";
144
165
  let equalsOrColon = "";
145
166
  let spaceOrNoSpace = "";
package/Struct.test.mts CHANGED
@@ -42,8 +42,8 @@ class BuyLimitations extends Struct {
42
42
  rawName: "BuyLimitations",
43
43
  isArray: true,
44
44
  });
45
- [0] = "EItemType::Weapon";
46
- [1] = "EItemType::Armor";
45
+ "0" = "EItemType::Weapon";
46
+ "1" = "EItemType::Armor";
47
47
  }
48
48
 
49
49
  describe("Struct", () => {
@@ -309,7 +309,7 @@ struct.end`;
309
309
  const a = new TradePrototype().fork(true);
310
310
  expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("EItemType::Weapon");
311
311
  expect(a.TradeGenerators[0].BuyLimitations[1]).toBe("EItemType::Armor");
312
- a.TradeGenerators[0].BuyLimitations.removeNode(0);
312
+ a.TradeGenerators[0].BuyLimitations.removeNode("0");
313
313
  expect(a.TradeGenerators[0].BuyLimitations[0]).toBe("removenode");
314
314
  });
315
315
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "s2cfgtojson",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Converts Stalker 2 Cfg file into POJOs",
5
5
  "keywords": [
6
6
  "stalker",
package/types.mts CHANGED
@@ -68,7 +68,17 @@ import {
68
68
  } from "./enums.mjs";
69
69
  import { Struct } from "./Struct.mjs";
70
70
 
71
- export type Internal = "__internal__";
71
+ export type Internal =
72
+ | "__internal__"
73
+ | "fork"
74
+ | "removeNode"
75
+ | "addNode"
76
+ | "clone"
77
+ | "forEach"
78
+ | "filter"
79
+ | "map"
80
+ | "entries"
81
+ | "toString";
72
82
 
73
83
  export type DeeplyPartial<T> = {
74
84
  [P in keyof T]?: T[P] extends Array<infer U>
@@ -81,7 +91,7 @@ export type DeeplyPartial<T> = {
81
91
  };
82
92
 
83
93
  export type DeeplyPartialStruct<T> = DeeplyPartial<
84
- T extends Struct ? Omit<T, Internal> : T
94
+ T extends Struct ? Exclude<T, Internal> : T
85
95
  >;
86
96
 
87
97
  export interface DefaultEntries {