-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added deep equals to object comparison in handle method to support co…
…rrectly mutable entities
- Loading branch information
1 parent
5e11545
commit 400bfb9
Showing
5 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
export const deepEquals = <T>(left: T, right: T): boolean => { | ||
if (isEquatable(left)) { | ||
return left.equals(right); | ||
} | ||
|
||
if (Array.isArray(left)) { | ||
return ( | ||
Array.isArray(right) && | ||
left.length === right.length && | ||
left.every((val, index) => deepEquals(val, right[index])) | ||
); | ||
} | ||
|
||
if ( | ||
typeof left !== 'object' || | ||
typeof right !== 'object' || | ||
left === null || | ||
right === null | ||
) { | ||
return left === right; | ||
} | ||
|
||
if (Array.isArray(right)) return false; | ||
|
||
const keys1 = Object.keys(left); | ||
const keys2 = Object.keys(right); | ||
|
||
if ( | ||
keys1.length !== keys2.length || | ||
!keys1.every((key) => keys2.includes(key)) | ||
) | ||
return false; | ||
|
||
for (const key in left) { | ||
if (left[key] instanceof Function && right[key] instanceof Function) | ||
continue; | ||
|
||
const isEqual = deepEquals(left[key], right[key]); | ||
if (!isEqual) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
export type Equatable<T> = { equals: (right: T) => boolean } & T; | ||
|
||
export const isEquatable = <T>(left: T): left is Equatable<T> => { | ||
return ( | ||
left && | ||
typeof left === 'object' && | ||
'equals' in left && | ||
typeof left['equals'] === 'function' | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './deepEquals'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters