( inImmutable: boolean, existing: C, keyPath: Array<K>, i: number, notSetValue: NSV | undefined, updater: UpdaterFunctionWithNSV<K, C, NSV> | UpdaterFunction<K, C> )
| 127 | } |
| 128 | |
| 129 | function 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 | } |
no test coverage detected