equals.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = equals;
  7. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _ParseACL = _interopRequireDefault(require("./ParseACL"));
  9. var _ParseFile = _interopRequireDefault(require("./ParseFile"));
  10. var _ParseGeoPoint = _interopRequireDefault(require("./ParseGeoPoint"));
  11. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  12. /**
  13. * Copyright (c) 2015-present, Parse, LLC.
  14. * All rights reserved.
  15. *
  16. * This source code is licensed under the BSD-style license found in the
  17. * LICENSE file in the root directory of this source tree. An additional grant
  18. * of patent rights can be found in the PATENTS file in the same directory.
  19. */
  20. var toString = Object.prototype.toString;
  21. function equals(a, b) {
  22. if (toString.call(a) === '[object Date]' || toString.call(b) === '[object Date]') {
  23. var dateA = new Date(a);
  24. var dateB = new Date(b);
  25. return +dateA === +dateB;
  26. }
  27. if ((0, _typeof2.default)(a) !== (0, _typeof2.default)(b)) {
  28. return false;
  29. }
  30. if (!a || (0, _typeof2.default)(a) !== 'object') {
  31. // a is a primitive
  32. return a === b;
  33. }
  34. if (Array.isArray(a) || Array.isArray(b)) {
  35. if (!Array.isArray(a) || !Array.isArray(b)) {
  36. return false;
  37. }
  38. if (a.length !== b.length) {
  39. return false;
  40. }
  41. for (var i = a.length; i--;) {
  42. if (!equals(a[i], b[i])) {
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48. if (a instanceof _ParseACL.default || a instanceof _ParseFile.default || a instanceof _ParseGeoPoint.default || a instanceof _ParseObject.default) {
  49. return a.equals(b);
  50. }
  51. if (b instanceof _ParseObject.default) {
  52. if (a.__type === 'Object' || a.__type === 'Pointer') {
  53. return a.objectId === b.id && a.className === b.className;
  54. }
  55. }
  56. if (Object.keys(a).length !== Object.keys(b).length) {
  57. return false;
  58. }
  59. for (var k in a) {
  60. if (!equals(a[k], b[k])) {
  61. return false;
  62. }
  63. }
  64. return true;
  65. }