import { Snapshot } from "./Snapshot" ; import { SnapshotDiffResult , SnapshotDiffResultType , } from "./SnapshotDiffResult" ; import { diffLines } from "diff" ; export class SnapshotDiff { results = new Map < string , SnapshotDiffResult > ( ) ; constructor ( public left , public right ) { this . calculateDiff ( ) ; } public calculateDiff ( ) { const left = this . left . values ; const right = this . right . values ; for ( const [ key , value ] of left . entries ( ) ) { if ( right . has ( key ) ) { const rightValue = right . get ( key ) ! ; const lines = diffLines ( rightValue , value ) ; const result = new SnapshotDiffResult ( ) ; result . left = value ; result . right = rightValue ; result . type = value === rightValue ? SnapshotDiffResultType . NoChange : SnapshotDiffResultType . Different ; result . changes = lines ; this . results . set ( key , result ) ; } else { const result = new SnapshotDiffResult ( ) ; result . left = value ; result . right = null ; result . type = SnapshotDiffResultType . Added ; result . changes = diffLines ( "" , value ) ; this . results . set ( key , result ) ; } } for ( const [ key , value ] of right . entries ( ) ) { if ( ! left . has ( key ) ) { const result = new SnapshotDiffResult ( ) ; result . left = null ; result . right = value ; result . changes = diffLines ( value , "" ) ; result . type = SnapshotDiffResultType . Removed ; this . results . set ( key , result ) ; } } } }