MCPcopy Index your code
hub / github.com/immutable-js/immutable-js / updateInDeeply

Function updateInDeeply

src/functional/updateIn.ts:129–180  ·  view source on GitHub ↗
(
  inImmutable: boolean,
  existing: C,
  keyPath: Array<K>,
  i: number,
  notSetValue: NSV | undefined,
  updater: UpdaterFunctionWithNSV<K, C, NSV> | UpdaterFunction<K, C>
)

Source from the content-addressed store, hash-verified

127}
128
129function updateInDeeply<
130 K,
131 TProps extends object,
132 C extends PossibleCollection<unknown, unknown, TProps>,
133 NSV,
134>(
135 inImmutable: boolean,
136 existing: C,
137 keyPath: Array<K>,
138 i: number,
139 notSetValue: NSV | undefined,
140 updater: UpdaterFunctionWithNSV<K, C, NSV> | UpdaterFunction<K, C>
141): C {
142 const wasNotSet = existing === NOT_SET;
143 if (i === keyPath.length) {
144 const existingValue = wasNotSet ? notSetValue : existing;
145 // @ts-expect-error mixed type with optional value
146 const newValue = updater(existingValue);
147 // @ts-expect-error mixed type
148 return newValue === existingValue ? existing : newValue;
149 }
150 if (!wasNotSet && !isDataStructure(existing)) {
151 throw new TypeError(
152 'Cannot update within non-data-structure value in path [' +
153 Array.from(keyPath).slice(0, i).map(quoteString) +
154 ']: ' +
155 existing
156 );
157 }
158 const key = keyPath[i];
159
160 const nextExisting = wasNotSet ? NOT_SET : get(existing, key, NOT_SET);
161 const nextUpdated = updateInDeeply(
162 nextExisting === NOT_SET ? inImmutable : isImmutable(nextExisting),
163 // @ts-expect-error mixed type
164 nextExisting,
165 keyPath,
166 i + 1,
167 notSetValue,
168 updater
169 );
170
171 return nextUpdated === nextExisting
172 ? existing
173 : nextUpdated === NOT_SET
174 ? remove(existing, key)
175 : set(
176 wasNotSet ? (inImmutable ? emptyMap() : {}) : existing,
177 key,
178 nextUpdated
179 );
180}

Callers 1

updateInFunction · 0.85

Calls 8

getFunction · 0.90
isImmutableFunction · 0.90
removeFunction · 0.90
setFunction · 0.90
emptyMapFunction · 0.90
isDataStructureFunction · 0.85
mapMethod · 0.65
sliceMethod · 0.65

Tested by

no test coverage detected