( from: T[], to: T[], )
| 52 | } |
| 53 | |
| 54 | export function getTransformDiff<T extends Idable>( |
| 55 | from: T[], |
| 56 | to: T[], |
| 57 | ): TransformDiff<T> { |
| 58 | const diff: TransformDiff<T> = { |
| 59 | inserted: [], |
| 60 | deleted: [], |
| 61 | transformed: [], |
| 62 | }; |
| 63 | |
| 64 | const fromMap = getIdMap(from); |
| 65 | const toMap = getIdMap(to); |
| 66 | |
| 67 | for (const [key, fromItem] of fromMap.entries()) { |
| 68 | const toItem = toMap.get(key); |
| 69 | if (toItem) { |
| 70 | toMap.delete(key); |
| 71 | for (let i = 0; i < Math.max(fromItem.length, toItem.length); i++) { |
| 72 | const insert = i >= fromItem.length; |
| 73 | const remove = i >= toItem.length; |
| 74 | |
| 75 | const fromNode = !insert ? fromItem[i] : fromItem[fromItem.length - 1]; |
| 76 | const toNode = !remove ? toItem[i] : toItem[toItem.length - 1]; |
| 77 | |
| 78 | diff.transformed.push({ |
| 79 | insert, |
| 80 | remove, |
| 81 | from: fromNode, |
| 82 | to: toNode, |
| 83 | }); |
| 84 | } |
| 85 | } else { |
| 86 | for (const node of fromItem) { |
| 87 | diff.deleted.push(node); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | for (const toItem of toMap.values()) { |
| 93 | for (const node of toItem) { |
| 94 | diff.inserted.push(node); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return diff; |
| 99 | } |
| 100 | |
| 101 | export function applyTransformDiff<T extends Idable>( |
| 102 | current: T[], |
no test coverage detected