cogsbox-state 0.5.426 → 0.5.428

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/src/utility.ts CHANGED
@@ -72,6 +72,7 @@ export const isDeepEqual = (
72
72
  );
73
73
  }
74
74
  };
75
+
75
76
  export function updateNestedProperty(
76
77
  path: string[],
77
78
  state: any,
@@ -168,24 +169,101 @@ export function getNestedValue<TStateObject extends unknown>(
168
169
  let value: any = obj;
169
170
 
170
171
  for (let i = 0; i < pathArray.length; i++) {
171
- const key = pathArray[i];
172
- // console.log(key, "loooo");
173
- if (!key) {
174
- throw new Error("Invalid path");
172
+ const key = pathArray[i]!;
173
+ if (value === undefined || value === null) {
174
+ // Cannot traverse further
175
+ return undefined;
175
176
  }
176
- if (Array.isArray(value)) {
177
+
178
+ if (typeof key === "string" && key.startsWith("id:")) {
179
+ if (!Array.isArray(value)) {
180
+ console.error("Path segment with 'id:' requires an array.", {
181
+ path: pathArray,
182
+ currentValue: value,
183
+ });
184
+ return undefined;
185
+ }
186
+ const targetId = key.split(":")[1];
187
+ value = value.find((item: any) => String(item.id) === targetId);
188
+ } else if (Array.isArray(value)) {
177
189
  value = value[parseInt(key)];
178
190
  } else {
179
- if (!value) {
180
- // console.log(key, value, pathArray);
181
- return;
182
- }
183
-
184
191
  value = value[key];
185
192
  }
186
193
  }
187
194
  return value;
188
195
  }
196
+
197
+ export function updateNestedPropertyIds(
198
+ path: string[],
199
+ state: any,
200
+ newValue: any
201
+ ) {
202
+ if (path.length === 0) {
203
+ return newValue;
204
+ }
205
+
206
+ const newState = Array.isArray(state) ? [...state] : { ...state };
207
+ let current: any = newState;
208
+
209
+ for (let i = 0; i < path.length - 1; i++) {
210
+ const key = path[i]!;
211
+
212
+ if (typeof key === "string" && key.startsWith("id:")) {
213
+ if (!Array.isArray(current)) {
214
+ throw new Error(
215
+ `Path segment "${key}" requires an array, but got a non-array.`
216
+ );
217
+ }
218
+ const targetId = key.split(":")[1];
219
+ const index = current.findIndex(
220
+ (item: any) => String(item.id) === targetId
221
+ );
222
+
223
+ if (index === -1) {
224
+ throw new Error(`Item with id "${targetId}" not found in array.`);
225
+ }
226
+ // Create a copy of the object at the found index to avoid direct mutation
227
+ current[index] = { ...current[index] };
228
+ current = current[index];
229
+ } else {
230
+ // Create a copy of the object/array to avoid direct mutation
231
+ current[key] = Array.isArray(current[key])
232
+ ? [...current[key]]
233
+ : { ...current[key] };
234
+ current = current[key];
235
+ }
236
+ }
237
+
238
+ // --- FIX IS HERE ---
239
+ // The original code only did `current[lastKey] = newValue;`
240
+ // This new logic handles the `id:` syntax for the *last* segment of the path.
241
+ const lastKey = path[path.length - 1]!;
242
+ if (typeof lastKey === "string" && lastKey.startsWith("id:")) {
243
+ if (!Array.isArray(current)) {
244
+ throw new Error(
245
+ `Final path segment "${lastKey}" requires an array, but got a non-array.`
246
+ );
247
+ }
248
+ const targetId = lastKey.split(":")[1];
249
+ const index = current.findIndex(
250
+ (item: any) => String(item.id) === targetId
251
+ );
252
+ if (index === -1) {
253
+ throw new Error(
254
+ `Item with id "${targetId}" not found in array to update.`
255
+ );
256
+ }
257
+ // Replace the item at its correct index
258
+ current[index] = newValue;
259
+ } else {
260
+ // This is the old logic, which works for normal properties.
261
+ current[lastKey] = newValue;
262
+ }
263
+ // --- END OF FIX ---
264
+
265
+ return newState;
266
+ }
189
267
  type DifferencePaths = string[];
190
268
 
191
269
  export function getDifferences(