( original: M, diff: Map<K, V>, newDataContainKey: (key: K) => boolean )
| 10 | * @description |
| 11 | */ |
| 12 | export function diffUpdate<K, V, M extends Map<K, V>>( |
| 13 | original: M, |
| 14 | diff: Map<K, V>, |
| 15 | newDataContainKey: (key: K) => boolean |
| 16 | ): M { |
| 17 | let hasDelete = false; |
| 18 | |
| 19 | for (const [key] of original) { |
| 20 | if (!newDataContainKey(key)) { |
| 21 | original.delete(key); |
| 22 | hasDelete = true; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | if (diff.size === 0 && !hasDelete) { |
| 27 | return original; |
| 28 | } |
| 29 | |
| 30 | const NewMapConstructor = original.constructor as new (entries?: Iterable<[K, V]>) => M; |
| 31 | const newMap = new NewMapConstructor(original); // shallow copy to preserve type |
| 32 | |
| 33 | for (const [key, value] of diff) { |
| 34 | newMap.set(key, value); |
| 35 | } |
| 36 | |
| 37 | return newMap; |
| 38 | } |
no test coverage detected