@ -14,35 +14,38 @@ function getLeanDiff(
showOnly : ObjectOptions [ "showOnly" ] = {
showOnly : ObjectOptions [ "showOnly" ] = {
statuses : [ ] ,
statuses : [ ] ,
granularity : GRANULARITY.BASIC ,
granularity : GRANULARITY.BASIC ,
}
} ,
) : ObjectDiff [ "diff" ] {
) : ObjectDiff [ "diff" ] {
const { statuses , granularity } = showOnly ;
const { statuses , granularity } = showOnly ;
return diff . reduce ( ( acc , value ) = > {
return diff . reduce (
if ( granularity === GRANULARITY . DEEP && value . subPropertiesDiff ) {
( acc , value ) = > {
const cleanSubPropertiesDiff = getLeanDiff (
if ( granularity === GRANULARITY . DEEP && value . subPropertiesDiff ) {
value . subPropertiesDiff ,
const cleanSubPropertiesDiff = getLeanDiff (
showOnly
value . subPropertiesDiff ,
) ;
showOnly ,
if ( cleanSubPropertiesDiff . length > 0 ) {
) ;
return [
if ( cleanSubPropertiesDiff . length > 0 ) {
. . . acc ,
return [
{ . . . value , subPropertiesDiff : cleanSubPropertiesDiff } ,
. . . acc ,
] ;
{ . . . value , subPropertiesDiff : cleanSubPropertiesDiff } ,
] ;
}
}
}
}
// @ts-ignore
if ( granularity === GRANULARITY . DEEP && value . subDiff ) {
// @ts-ignore
// @ts-ignore
const cleanSubDiff = getLeanDiff ( value . subDiff , showOnly ) ;
if ( granularity === GRANULARITY . DEEP && value . subDiff ) {
if ( cleanSubDiff . length > 0 ) {
// @ts-ignore
return [ . . . acc , { . . . value , subDiff : cleanSubDiff } ] ;
const cleanSubDiff = getLeanDiff ( value . subDiff , showOnly ) ;
if ( cleanSubDiff . length > 0 ) {
return [ . . . acc , { . . . value , subDiff : cleanSubDiff } ] ;
}
}
}
}
if ( statuses . includes ( value . status ) ) {
if ( statuses . includes ( value . status ) ) {
return [ . . . acc , value ] ;
return [ . . . acc , value ] ;
}
}
return acc ;
return acc ;
} ,
} , [ ] as ObjectDiff [ "diff" ] ) ;
[ ] as ObjectDiff [ "diff" ] ,
) ;
}
}
function getObjectStatus ( diff : ObjectDiff [ "diff" ] ) : ObjectDiffStatus {
function getObjectStatus ( diff : ObjectDiff [ "diff" ] ) : ObjectDiffStatus {
@ -57,7 +60,7 @@ function formatSingleObjectDiff(
options : ObjectOptions = {
options : ObjectOptions = {
ignoreArrayOrder : false ,
ignoreArrayOrder : false ,
showOnly : { statuses : [ ] , granularity : GRANULARITY.BASIC } ,
showOnly : { statuses : [ ] , granularity : GRANULARITY.BASIC } ,
}
} ,
) : ObjectDiff {
) : ObjectDiff {
if ( ! data ) {
if ( ! data ) {
return {
return {
@ -110,13 +113,13 @@ function formatSingleObjectDiff(
function getPreviousMatch (
function getPreviousMatch (
previousValue : any | undefined ,
previousValue : any | undefined ,
nextSubProperty : any ,
nextSubProperty : any ,
options? : ObjectOptions
options? : ObjectOptions ,
) : any | undefined {
) : any | undefined {
if ( ! previousValue ) {
if ( ! previousValue ) {
return undefined ;
return undefined ;
}
}
const previousMatch = Object . entries ( previousValue ) . find ( ( [ subPreviousKey ] ) = >
const previousMatch = Object . entries ( previousValue ) . find ( ( [ subPreviousKey ] ) = >
isEqual ( subPreviousKey , nextSubProperty , options )
isEqual ( subPreviousKey , nextSubProperty , options ) ,
) ;
) ;
return previousMatch ? previousMatch [ 1 ] : undefined ;
return previousMatch ? previousMatch [ 1 ] : undefined ;
}
}
@ -124,7 +127,7 @@ function getPreviousMatch(
function getValueStatus (
function getValueStatus (
previousValue : any ,
previousValue : any ,
nextValue : any ,
nextValue : any ,
options? : ObjectOptions
options? : ObjectOptions ,
) : ObjectDiffStatus {
) : ObjectDiffStatus {
if ( isEqual ( previousValue , nextValue , options ) ) {
if ( isEqual ( previousValue , nextValue , options ) ) {
return STATUS . EQUAL ;
return STATUS . EQUAL ;
@ -133,7 +136,7 @@ function getValueStatus(
}
}
function getPropertyStatus (
function getPropertyStatus (
subPropertiesDiff : SubProperties [ ]
subPropertiesDiff : SubProperties [ ] ,
) : ObjectDiffStatus {
) : ObjectDiffStatus {
return subPropertiesDiff . some ( ( property ) = > property . status !== STATUS . EQUAL )
return subPropertiesDiff . some ( ( property ) = > property . status !== STATUS . EQUAL )
? STATUS . UPDATED
? STATUS . UPDATED
@ -142,7 +145,7 @@ function getPropertyStatus(
function getDeletedProperties (
function getDeletedProperties (
previousValue : Record < string , any > | undefined ,
previousValue : Record < string , any > | undefined ,
nextValue : Record < string , any >
nextValue : Record < string , any > ,
) : { property : string ; value : any } [ ] | undefined {
) : { property : string ; value : any } [ ] | undefined {
if ( ! previousValue ) return undefined ;
if ( ! previousValue ) return undefined ;
const prevKeys = Object . keys ( previousValue ) ;
const prevKeys = Object . keys ( previousValue ) ;
@ -160,13 +163,13 @@ function getDeletedProperties(
function getSubPropertiesDiff (
function getSubPropertiesDiff (
previousValue : Record < string , any > | undefined ,
previousValue : Record < string , any > | undefined ,
nextValue : Record < string , any > ,
nextValue : Record < string , any > ,
options? : ObjectOptions
options? : ObjectOptions ,
) : SubProperties [ ] {
) : SubProperties [ ] {
const subPropertiesDiff : SubProperties [ ] = [ ] ;
const subPropertiesDiff : SubProperties [ ] = [ ] ;
let subDiff : SubProperties [ ] ;
let subDiff : SubProperties [ ] ;
const deletedMainSubProperties = getDeletedProperties (
const deletedMainSubProperties = getDeletedProperties (
previousValue ,
previousValue ,
nextValue
nextValue ,
) ;
) ;
if ( deletedMainSubProperties ) {
if ( deletedMainSubProperties ) {
deletedMainSubProperties . forEach ( ( deletedProperty ) = > {
deletedMainSubProperties . forEach ( ( deletedProperty ) = > {
@ -182,9 +185,9 @@ function getSubPropertiesDiff(
const previousMatch = getPreviousMatch (
const previousMatch = getPreviousMatch (
previousValue ,
previousValue ,
nextSubProperty ,
nextSubProperty ,
options
options ,
) ;
) ;
if ( ! ! ! previousMatch ) {
if ( ! previousMatch ) {
return subPropertiesDiff . push ( {
return subPropertiesDiff . push ( {
property : nextSubProperty ,
property : nextSubProperty ,
previousValue : previousMatch ,
previousValue : previousMatch ,
@ -193,15 +196,15 @@ function getSubPropertiesDiff(
! previousValue || ! ( nextSubProperty in previousValue )
! previousValue || ! ( nextSubProperty in previousValue )
? STATUS . ADDED
? STATUS . ADDED
: previousMatch === nextSubValue
: previousMatch === nextSubValue
? STATUS . EQUAL
? STATUS . EQUAL
: STATUS . UPDATED ,
: STATUS . UPDATED ,
} ) ;
} ) ;
}
}
if ( isObject ( nextSubValue ) ) {
if ( isObject ( nextSubValue ) ) {
const data : SubProperties [ ] = getSubPropertiesDiff (
const data : SubProperties [ ] = getSubPropertiesDiff (
previousMatch ,
previousMatch ,
nextSubValue ,
nextSubValue ,
options
options ,
) ;
) ;
if ( data && data . length > 0 ) {
if ( data && data . length > 0 ) {
subDiff = data ;
subDiff = data ;
@ -237,7 +240,7 @@ export function getObjectDiff(
options : ObjectOptions = {
options : ObjectOptions = {
ignoreArrayOrder : false ,
ignoreArrayOrder : false ,
showOnly : { statuses : [ ] , granularity : GRANULARITY.BASIC } ,
showOnly : { statuses : [ ] , granularity : GRANULARITY.BASIC } ,
}
} ,
) : ObjectDiff {
) : ObjectDiff {
if ( ! prevData && ! nextData ) {
if ( ! prevData && ! nextData ) {
return {
return {
@ -255,7 +258,7 @@ export function getObjectDiff(
const diff : ObjectDiff [ "diff" ] = [ ] ;
const diff : ObjectDiff [ "diff" ] = [ ] ;
Object . entries ( nextData ) . forEach ( ( [ nextProperty , nextValue ] ) = > {
Object . entries ( nextData ) . forEach ( ( [ nextProperty , nextValue ] ) = > {
const previousValue = prevData [ nextProperty ] ;
const previousValue = prevData [ nextProperty ] ;
if ( ! ! ! previousValue ) {
if ( ! previousValue ) {
return diff . push ( {
return diff . push ( {
property : nextProperty ,
property : nextProperty ,
previousValue ,
previousValue ,
@ -263,15 +266,15 @@ export function getObjectDiff(
status : ! ( nextProperty in prevData )
status : ! ( nextProperty in prevData )
? STATUS . ADDED
? STATUS . ADDED
: previousValue === nextValue
: previousValue === nextValue
? STATUS . EQUAL
? STATUS . EQUAL
: STATUS . UPDATED ,
: STATUS . UPDATED ,
} ) ;
} ) ;
}
}
if ( isObject ( nextValue ) ) {
if ( isObject ( nextValue ) ) {
const subPropertiesDiff : SubProperties [ ] = getSubPropertiesDiff (
const subPropertiesDiff : SubProperties [ ] = getSubPropertiesDiff (
previousValue ,
previousValue ,
nextValue ,
nextValue ,
options
options ,
) ;
) ;
const subPropertyStatus = getPropertyStatus ( subPropertiesDiff ) ;
const subPropertyStatus = getPropertyStatus ( subPropertiesDiff ) ;
return diff . push ( {
return diff . push ( {