( fromRecords: Map<string, KeyedRecord>, toRecords: Map<string, KeyedRecord> )
| 22 | }; |
| 23 | |
| 24 | export function diffKeyedRecords( |
| 25 | fromRecords: Map<string, KeyedRecord>, |
| 26 | toRecords: Map<string, KeyedRecord> |
| 27 | ): KeyedDiff { |
| 28 | const added: { id: string; record: KeyedRecord }[] = []; |
| 29 | const removed: { id: string; record: KeyedRecord }[] = []; |
| 30 | const changed: RecordChange[] = []; |
| 31 | |
| 32 | const fromIds = new Set(fromRecords.keys()); |
| 33 | const toIds = new Set(toRecords.keys()); |
| 34 | |
| 35 | for (const id of Array.from(toIds).sort()) { |
| 36 | if (!fromIds.has(id)) { |
| 37 | const record = toRecords.get(id); |
| 38 | if (record) { |
| 39 | added.push({ id, record }); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | for (const id of Array.from(fromIds).sort()) { |
| 45 | if (!toIds.has(id)) { |
| 46 | const record = fromRecords.get(id); |
| 47 | if (record) { |
| 48 | removed.push({ id, record }); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | for (const id of Array.from(fromIds).sort()) { |
| 54 | if (!toIds.has(id)) { |
| 55 | continue; |
| 56 | } |
| 57 | const fromRecord = fromRecords.get(id); |
| 58 | const toRecord = toRecords.get(id); |
| 59 | if (!fromRecord || !toRecord) { |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | const keys = Array.from( |
| 64 | new Set([...Object.keys(fromRecord), ...Object.keys(toRecord)]) |
| 65 | ).sort(); |
| 66 | const fields: FieldChange[] = []; |
| 67 | for (const key of keys) { |
| 68 | const fromValue = Object.hasOwn(fromRecord, key) |
| 69 | ? fromRecord[key] |
| 70 | : MISSING; |
| 71 | const toValue = Object.hasOwn(toRecord, key) ? toRecord[key] : MISSING; |
| 72 | if (!isEqual(fromValue, toValue)) { |
| 73 | fields.push({ key, from: fromValue, to: toValue }); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (fields.length) { |
| 78 | changed.push({ id, fromRecord, toRecord, fields }); |
| 79 | } |
| 80 | } |
| 81 |
no test coverage detected