deepEqual.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { factory } from '../../utils/factory.js';
  2. var name = 'deepEqual';
  3. var dependencies = ['typed', 'equal'];
  4. export var createDeepEqual = /* #__PURE__ */factory(name, dependencies, _ref => {
  5. var {
  6. typed,
  7. equal
  8. } = _ref;
  9. /**
  10. * Test element wise whether two matrices are equal.
  11. * The function accepts both matrices and scalar values.
  12. *
  13. * Strings are compared by their numerical value.
  14. *
  15. * Syntax:
  16. *
  17. * math.deepEqual(x, y)
  18. *
  19. * Examples:
  20. *
  21. * math.deepEqual(2, 4) // returns false
  22. *
  23. * a = [2, 5, 1]
  24. * b = [2, 7, 1]
  25. *
  26. * math.deepEqual(a, b) // returns false
  27. * math.equal(a, b) // returns [true, false, true]
  28. *
  29. * See also:
  30. *
  31. * equal, unequal
  32. *
  33. * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First matrix to compare
  34. * @param {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second matrix to compare
  35. * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix}
  36. * Returns true when the input matrices have the same size and each of their elements is equal.
  37. */
  38. return typed(name, {
  39. 'any, any': function anyAny(x, y) {
  40. return _deepEqual(x.valueOf(), y.valueOf());
  41. }
  42. });
  43. /**
  44. * Test whether two arrays have the same size and all elements are equal
  45. * @param {Array | *} x
  46. * @param {Array | *} y
  47. * @return {boolean} Returns true if both arrays are deep equal
  48. */
  49. function _deepEqual(x, y) {
  50. if (Array.isArray(x)) {
  51. if (Array.isArray(y)) {
  52. var len = x.length;
  53. if (len !== y.length) {
  54. return false;
  55. }
  56. for (var i = 0; i < len; i++) {
  57. if (!_deepEqual(x[i], y[i])) {
  58. return false;
  59. }
  60. }
  61. return true;
  62. } else {
  63. return false;
  64. }
  65. } else {
  66. if (Array.isArray(y)) {
  67. return false;
  68. } else {
  69. return equal(x, y);
  70. }
  71. }
  72. }
  73. });