equals.js 2.0 KB

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