* Updates an object at a specific path
( obj: JsonObject, path: string[], value: JsonValue, )
| 187 | * Updates an object at a specific path |
| 188 | */ |
| 189 | function updateObject( |
| 190 | obj: JsonObject, |
| 191 | path: string[], |
| 192 | value: JsonValue, |
| 193 | ): JsonObject { |
| 194 | const [key, ...restPath] = path; |
| 195 | |
| 196 | // Validate object key |
| 197 | if (typeof key !== "string") { |
| 198 | console.error(`Invalid object key: ${key}`); |
| 199 | return obj; |
| 200 | } |
| 201 | |
| 202 | const newObj = { ...obj }; |
| 203 | |
| 204 | if (restPath.length === 0) { |
| 205 | newObj[key] = value; |
| 206 | } else { |
| 207 | // Ensure key exists |
| 208 | if (!(key in newObj)) { |
| 209 | newObj[key] = {}; |
| 210 | } |
| 211 | newObj[key] = updateValueAtPath(newObj[key], restPath, value); |
| 212 | } |
| 213 | return newObj; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Gets a value at a specific path in a nested JSON structure |
no test coverage detected