( obj: JsonValue, path: string[], value: JsonValue, )
| 113 | * @returns A new JSON value with the updated path |
| 114 | */ |
| 115 | export function updateValueAtPath( |
| 116 | obj: JsonValue, |
| 117 | path: string[], |
| 118 | value: JsonValue, |
| 119 | ): JsonValue { |
| 120 | if (path.length === 0) return value; |
| 121 | |
| 122 | if (obj === null || obj === undefined) { |
| 123 | obj = !isNaN(Number(path[0])) ? [] : {}; |
| 124 | } |
| 125 | |
| 126 | if (Array.isArray(obj)) { |
| 127 | return updateArray(obj, path, value); |
| 128 | } else if (typeof obj === "object" && obj !== null) { |
| 129 | return updateObject(obj as JsonObject, path, value); |
| 130 | } else { |
| 131 | console.error( |
| 132 | `Cannot update path ${path.join(".")} in non-object/array value:`, |
| 133 | obj, |
| 134 | ); |
| 135 | return obj; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Updates an array at a specific path |
no test coverage detected
searching dependent graphs…