equals.js 1.6 KB

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