( oldData: unknown, updatePath: Array<string>, value: unknown, )
| 173 | * @param {unknown} value New value |
| 174 | */ |
| 175 | export const updateNestedDataByPath = ( |
| 176 | oldData: unknown, |
| 177 | updatePath: Array<string>, |
| 178 | value: unknown, |
| 179 | ): any => { |
| 180 | if (updatePath.length === 0) { |
| 181 | return value |
| 182 | } |
| 183 | |
| 184 | if (oldData instanceof Map) { |
| 185 | const newData = new Map(oldData) |
| 186 | |
| 187 | if (updatePath.length === 1) { |
| 188 | newData.set(updatePath[0], value) |
| 189 | return newData |
| 190 | } |
| 191 | |
| 192 | const [head, ...tail] = updatePath |
| 193 | newData.set(head, updateNestedDataByPath(newData.get(head), tail, value)) |
| 194 | return newData |
| 195 | } |
| 196 | |
| 197 | if (oldData instanceof Set) { |
| 198 | const setAsArray = updateNestedDataByPath( |
| 199 | Array.from(oldData), |
| 200 | updatePath, |
| 201 | value, |
| 202 | ) |
| 203 | |
| 204 | return new Set(setAsArray) |
| 205 | } |
| 206 | |
| 207 | if (Array.isArray(oldData)) { |
| 208 | const newData = [...oldData] |
| 209 | |
| 210 | if (updatePath.length === 1) { |
| 211 | // @ts-expect-error |
| 212 | newData[updatePath[0]] = value |
| 213 | return newData |
| 214 | } |
| 215 | |
| 216 | const [head, ...tail] = updatePath |
| 217 | // @ts-expect-error |
| 218 | newData[head] = updateNestedDataByPath(newData[head], tail, value) |
| 219 | |
| 220 | return newData |
| 221 | } |
| 222 | |
| 223 | if (oldData instanceof Object) { |
| 224 | const newData = { ...oldData } |
| 225 | |
| 226 | if (updatePath.length === 1) { |
| 227 | // @ts-expect-error |
| 228 | newData[updatePath[0]] = value |
| 229 | return newData |
| 230 | } |
| 231 | |
| 232 | const [head, ...tail] = updatePath |
no test coverage detected
searching dependent graphs…