* Return true if two objects have equal contents. * * @method equalsDeep * @param object1 {Object} The object to compare from * @param object2 {Object} The object to compare with * @param depth {number} An optional depth to prevent recursion. Default: 20. * @return {boolean} True
(object1, object2, depth)
| 661 | * @return {boolean} True if both objects have equivalent contents |
| 662 | */ |
| 663 | static equalsDeep(object1, object2, depth) { |
| 664 | // Recursion detection |
| 665 | depth = (depth === null ? DEFAULT_CLONE_DEPTH : depth); |
| 666 | if (depth < 0) { |
| 667 | return {}; |
| 668 | } |
| 669 | |
| 670 | // Fast comparisons |
| 671 | if (!object1 || !object2) { |
| 672 | return false; |
| 673 | } |
| 674 | if (object1 === object2) { |
| 675 | return true; |
| 676 | } |
| 677 | if (typeof (object1) != 'object' || typeof (object2) != 'object') { |
| 678 | return false; |
| 679 | } |
| 680 | |
| 681 | // They must have the same keys. If their length isn't the same |
| 682 | // then they're not equal. If the keys aren't the same, the value |
| 683 | // comparisons will fail. |
| 684 | if (Object.keys(object1).length !== Object.keys(object2).length) { |
| 685 | return false; |
| 686 | } |
| 687 | |
| 688 | // Compare the values |
| 689 | for (const prop in object1) { |
| 690 | |
| 691 | // Call recursively if an object or array |
| 692 | if (object1[prop] && typeof (object1[prop]) === 'object') { |
| 693 | if (!this.equalsDeep(object1[prop], object2[prop], depth - 1)) { |
| 694 | return false; |
| 695 | } |
| 696 | } else { |
| 697 | if (object1[prop] !== object2[prop]) { |
| 698 | return false; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | // Test passed. |
| 704 | return true; |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Extend an object, and any object it contains. |