MCPcopy Create free account
hub / github.com/TanStack/form / doSet

Function doSet

packages/form-core/src/utils.ts:52–86  ·  view source on GitHub ↗
(parent?: any)

Source from the content-addressed store, hash-verified

50 const path = makePathArray(_path)
51
52 function doSet(parent?: any): any {
53 if (!path.length) {
54 return functionalUpdate(updater, parent)
55 }
56
57 const key = path.shift()
58
59 if (
60 typeof key === 'string' ||
61 (typeof key === 'number' && !Array.isArray(parent))
62 ) {
63 if (typeof parent === 'object') {
64 if (parent === null) {
65 parent = {}
66 }
67 return {
68 ...parent,
69 [key]: doSet(parent[key]),
70 }
71 }
72 return {
73 [key]: doSet(),
74 }
75 }
76
77 if (Array.isArray(parent) && typeof key === 'number') {
78 const prefix = parent.slice(0, key)
79 return [
80 ...(prefix.length ? prefix : new Array(key)),
81 doSet(parent[key]),
82 ...parent.slice(key + 1),
83 ]
84 }
85 return [...new Array(key), doSet()]
86 }
87
88 return doSet(obj)
89}

Callers 1

setByFunction · 0.85

Calls 1

functionalUpdateFunction · 0.85

Tested by

no test coverage detected