areEqual.js.flow 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @providesModule areEqual
  8. * @flow
  9. */
  10. const aStackPool = [];
  11. const bStackPool = [];
  12. /**
  13. * Checks if two values are equal. Values may be primitives, arrays, or objects.
  14. * Returns true if both arguments have the same keys and values.
  15. *
  16. * @see http://underscorejs.org
  17. * @copyright 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
  18. * @license MIT
  19. */
  20. function areEqual(a: any, b: any): boolean {
  21. const aStack = aStackPool.length ? aStackPool.pop() : [];
  22. const bStack = bStackPool.length ? bStackPool.pop() : [];
  23. const result = eq(a, b, aStack, bStack);
  24. aStack.length = 0;
  25. bStack.length = 0;
  26. aStackPool.push(aStack);
  27. bStackPool.push(bStack);
  28. return result;
  29. }
  30. function eq(a: any, b: any, aStack: Array<any>, bStack: Array<any>): boolean {
  31. if (a === b) {
  32. // Identical objects are equal. `0 === -0`, but they aren't identical.
  33. return a !== 0 || 1 / a == 1 / b;
  34. }
  35. if (a == null || b == null) {
  36. // a or b can be `null` or `undefined`
  37. return false;
  38. }
  39. if (typeof a != 'object' || typeof b != 'object') {
  40. return false;
  41. }
  42. const objToStr = Object.prototype.toString;
  43. const className = objToStr.call(a);
  44. if (className != objToStr.call(b)) {
  45. return false;
  46. }
  47. switch (className) {
  48. case '[object String]':
  49. return a == String(b);
  50. case '[object Number]':
  51. return isNaN(a) || isNaN(b) ? false : a == Number(b);
  52. case '[object Date]':
  53. case '[object Boolean]':
  54. return +a == +b;
  55. case '[object RegExp]':
  56. return a.source == b.source && a.global == b.global && a.multiline == b.multiline && a.ignoreCase == b.ignoreCase;
  57. }
  58. // Assume equality for cyclic structures.
  59. let length = aStack.length;
  60. while (length--) {
  61. if (aStack[length] == a) {
  62. return bStack[length] == b;
  63. }
  64. }
  65. aStack.push(a);
  66. bStack.push(b);
  67. let size = 0;
  68. // Recursively compare objects and arrays.
  69. if (className === '[object Array]') {
  70. size = a.length;
  71. if (size !== b.length) {
  72. return false;
  73. }
  74. // Deep compare the contents, ignoring non-numeric properties.
  75. while (size--) {
  76. if (!eq(a[size], b[size], aStack, bStack)) {
  77. return false;
  78. }
  79. }
  80. } else {
  81. if (a.constructor !== b.constructor) {
  82. return false;
  83. }
  84. if (a.hasOwnProperty('valueOf') && b.hasOwnProperty('valueOf')) {
  85. return a.valueOf() == b.valueOf();
  86. }
  87. const keys = Object.keys(a);
  88. if (keys.length != Object.keys(b).length) {
  89. return false;
  90. }
  91. for (let i = 0; i < keys.length; i++) {
  92. if (!eq(a[keys[i]], b[keys[i]], aStack, bStack)) {
  93. return false;
  94. }
  95. }
  96. }
  97. aStack.pop();
  98. bStack.pop();
  99. return true;
  100. }
  101. module.exports = areEqual;