( current: T[], diff: TransformDiff<T>, cloner: (original: T) => T, )
| 99 | } |
| 100 | |
| 101 | export function applyTransformDiff<T extends Idable>( |
| 102 | current: T[], |
| 103 | diff: TransformDiff<T>, |
| 104 | cloner: (original: T) => T, |
| 105 | ): ApplyTransformResult<T> { |
| 106 | function insert(item: TransformDiffItem<T>) { |
| 107 | let idIndex = -1; |
| 108 | const index = item.before |
| 109 | ? current.findIndex(({id}) => { |
| 110 | if (id === item.before?.id) { |
| 111 | idIndex++; |
| 112 | if (idIndex === item.beforeIdIndex) return true; |
| 113 | } |
| 114 | return false; |
| 115 | }) |
| 116 | : 0; |
| 117 | current.splice(index + 1, 0, item.current); |
| 118 | } |
| 119 | |
| 120 | const result: ApplyTransformResult<T> = { |
| 121 | inserted: diff.inserted.map(item => ({ |
| 122 | item, |
| 123 | order: item.currentIndex, |
| 124 | })), |
| 125 | }; |
| 126 | |
| 127 | for (const item of diff.transformed) { |
| 128 | if (!item.insert) continue; |
| 129 | |
| 130 | const from = item.from; |
| 131 | item.from = { |
| 132 | ...item.to, |
| 133 | current: cloner(from.current), |
| 134 | }; |
| 135 | result.inserted.push({ |
| 136 | item: item.from, |
| 137 | order: item.to.currentIndex, |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | result.inserted.sort((a, b) => a.order - b.order); |
| 142 | |
| 143 | for (const item of result.inserted) { |
| 144 | insert(item.item); |
| 145 | } |
| 146 | |
| 147 | return result; |
| 148 | } |
no test coverage detected