deepEqual.js 2.1 KB

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