MCPcopy Create free account
hub / github.com/TanStack/query / updateNestedDataByPath

Function updateNestedDataByPath

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

Source from the content-addressed store, hash-verified

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