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

Function deleteNestedDataByPath

packages/query-devtools/src/utils.tsx:251–305  ·  view source on GitHub ↗
(
  oldData: unknown,
  deletePath: Array<string>,
)

Source from the content-addressed store, hash-verified

249 * @returns newData without the deleted items by path
250 */
251export const deleteNestedDataByPath = (
252 oldData: unknown,
253 deletePath: Array<string>,
254): any => {
255 if (oldData instanceof Map) {
256 const newData = new Map(oldData)
257
258 if (deletePath.length === 1) {
259 newData.delete(deletePath[0])
260 return newData
261 }
262
263 const [head, ...tail] = deletePath
264 newData.set(head, deleteNestedDataByPath(newData.get(head), tail))
265 return newData
266 }
267
268 if (oldData instanceof Set) {
269 const setAsArray = deleteNestedDataByPath(Array.from(oldData), deletePath)
270 return new Set(setAsArray)
271 }
272
273 if (Array.isArray(oldData)) {
274 const newData = [...oldData]
275
276 if (deletePath.length === 1) {
277 return newData.filter((_, idx) => idx.toString() !== deletePath[0])
278 }
279
280 const [head, ...tail] = deletePath
281
282 // @ts-expect-error
283 newData[head] = deleteNestedDataByPath(newData[head], tail)
284
285 return newData
286 }
287
288 if (oldData instanceof Object) {
289 const newData = { ...oldData }
290
291 if (deletePath.length === 1) {
292 // @ts-expect-error
293 delete newData[deletePath[0]]
294 return newData
295 }
296
297 const [head, ...tail] = deletePath
298 // @ts-expect-error
299 newData[head] = deleteNestedDataByPath(newData[head], tail)
300
301 return newData
302 }
303
304 return oldData
305}
306
307// Sets up the goober stylesheet
308// Adds a nonce to the style tag if needed

Callers 2

DeleteItemButtonFunction · 0.90
utils.test.tsFile · 0.90

Calls 1

getMethod · 0.80

Tested by

no test coverage detected