(sourceBranch: string, sourceIndex: number, targetBranch: string)
| 126 | |
| 127 | // Adjust target path if it was affected by source removal |
| 128 | export const adjustTargetPath = (sourceBranch: string, sourceIndex: number, targetBranch: string): string => { |
| 129 | const sourceParts = sourceBranch.split('.'); |
| 130 | const targetParts = targetBranch.split('.'); |
| 131 | |
| 132 | // If target path is shorter or equal to source path, it's not a descendant (unless same list, handled elsewhere) |
| 133 | if (targetParts.length <= sourceParts.length) return targetBranch; |
| 134 | |
| 135 | // Check if target is a descendant of the source list |
| 136 | // i.e. sourcePath prefix matches targetPath prefix |
| 137 | for (let i = 0; i < sourceParts.length; i++) { |
| 138 | if (sourceParts[i] !== targetParts[i]) return targetBranch; |
| 139 | } |
| 140 | |
| 141 | // sourceParts points to the list that was modified. |
| 142 | // targetParts[sourceParts.length] is the index in that list that leads to the target. |
| 143 | const affectedIndexStr = targetParts[sourceParts.length]; |
| 144 | const affectedIndex = Number(affectedIndexStr); |
| 145 | |
| 146 | if (isNaN(affectedIndex)) return targetBranch; |
| 147 | |
| 148 | // If the item removed was before the path to the target, shift the index down |
| 149 | if (sourceIndex < affectedIndex) { |
| 150 | const newParts = [...targetParts]; |
| 151 | newParts[sourceParts.length] = String(affectedIndex - 1); |
| 152 | return newParts.join('.'); |
| 153 | } |
| 154 | |
| 155 | return targetBranch; |
| 156 | }; |
| 157 | |
| 158 | // Insert item at specific path and index |
| 159 | export const insertNestedItem = ( |
no outgoing calls
no test coverage detected