|
1 |
| -import isEqualWith from 'lodash/isEqualWith'; |
| 1 | +import { createCustomEqual, State } from 'fast-equals'; |
2 | 2 |
|
3 |
| -/** Implements a deep equals using the `lodash.isEqualWith` function, that provides a customized comparator that |
| 3 | +/** Check if all parameters are typeof function. |
| 4 | + * |
| 5 | + * @param a - The first element to check typeof |
| 6 | + * @param b - The second element to check typeof |
| 7 | + * @returns - if typeof a and b are equal to function return true, otherwise false |
| 8 | + */ |
| 9 | +function isFunctions(a: any, b: any) { |
| 10 | + return typeof a === 'function' && typeof b === 'function'; |
| 11 | +} |
| 12 | + |
| 13 | +/** Implements a deep equals using the `fast-equal.createCustomEqual` function, that provides a customized comparator that |
| 14 | + * assumes all functions in objects are equivalent. |
| 15 | + * |
| 16 | + * @param a - The first element to compare |
| 17 | + * @param b - The second element to compare |
| 18 | + * @returns - True if the `a` and `b` are deeply equal, false otherwise |
| 19 | + */ |
| 20 | +const customDeepEqual = createCustomEqual({ |
| 21 | + createInternalComparator: (comparator: (a: any, b: any, state: State<any>) => boolean) => { |
| 22 | + return (a: any, b: any, _idxA: any, _idxB: any, _parentA: any, _parentB: any, state: State<any>) => { |
| 23 | + if (isFunctions(a, b)) { |
| 24 | + // Assume all functions are equivalent |
| 25 | + // see https://github.com/rjsf-team/react-jsonschema-form/issues/255 |
| 26 | + return true; |
| 27 | + } |
| 28 | + |
| 29 | + return comparator(a, b, state); |
| 30 | + }; |
| 31 | + }, |
| 32 | +}); |
| 33 | + |
| 34 | +/** Implements a deep equals using the `fast-equal.createCustomEqual` function, that provides a customized comparator that |
4 | 35 | * assumes all functions are equivalent.
|
5 | 36 | *
|
6 | 37 | * @param a - The first element to compare
|
7 | 38 | * @param b - The second element to compare
|
8 | 39 | * @returns - True if the `a` and `b` are deeply equal, false otherwise
|
9 | 40 | */
|
10 | 41 | export default function deepEquals(a: any, b: any): boolean {
|
11 |
| - return isEqualWith(a, b, (obj: any, other: any) => { |
12 |
| - if (typeof obj === 'function' && typeof other === 'function') { |
13 |
| - // Assume all functions are equivalent |
14 |
| - // see https://github.com/rjsf-team/react-jsonschema-form/issues/255 |
15 |
| - return true; |
16 |
| - } |
17 |
| - return undefined; // fallback to default isEquals behavior |
18 |
| - }); |
| 42 | + if (isFunctions(a, b)) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + return customDeepEqual(a, b); |
19 | 46 | }
|
0 commit comments