-
Notifications
You must be signed in to change notification settings - Fork 216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attempting to use if mirror has a toJSON and object does not breaks #318
Comments
This issue persists since 2021 which is really bad for such a far reaching library. Workaround: when creating the objects to compare, use toIsoString beforehand. e.g. bug = new Date('2032-01-04').toIsoString(); Error Source:
Possible Solution:
|
Here's a little helper function which patches /**
* The function takes an input value and applies data type patches if the input value is:
* - `Date` --> JSON representation as fast-json-patch cannot handle Dates
* - an `object` --> traverses the object tree applying patches to properties where necessary
* - an `array` --> traverses the array elements applying patches to properties where necessary
*
* All other data types - e.g. `string`, `number` will stay unpatched.
*
* @returns patched input when necessary
*/
export function patchDataTypes<T>(input: T): any {
if (input instanceof Date) {
return input.toJSON() as string;
} else if (typeof input === 'object' && input !== null) {
if (Array.isArray(input)) {
// If it's an array, recursively convert each element
return input.map(patchDataTypes);
} else {
// If it's an object, recursively convert each property
const result: Record<string, unknown> = {};
for (const key in input) {
result[key] = patchDataTypes(input[key]);
}
return result;
}
} else {
return input;
}
} |
For example, suppose you have the following objects:
Then
compare(mirror, obj);
returns the following array:The text was updated successfully, but these errors were encountered: