MCPcopy
hub / github.com/TanStack/query / updateNestedDataByPath

Function updateNestedDataByPath

packages/query-devtools/src/utils.tsx:175–240  ·  view source on GitHub ↗
(
  oldData: unknown,
  updatePath: Array<string>,
  value: unknown,
)

Source from the content-addressed store, hash-verified

173 * @param {unknown} value New value
174 */
175export 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

Callers 4

ClearArrayButtonFunction · 0.90
ToggleValueButtonFunction · 0.90
ExplorerFunction · 0.90
utils.test.tsFile · 0.90

Calls 1

getMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…