index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var pSlice = Array.prototype.slice;
  2. var Object_keys = typeof Object.keys === 'function'
  3. ? Object.keys
  4. : function (obj) {
  5. var keys = [];
  6. for (var key in obj) keys.push(key);
  7. return keys;
  8. }
  9. ;
  10. var deepEqual = module.exports = function (actual, expected) {
  11. // enforce Object.is +0 !== -0
  12. if (actual === 0 && expected === 0) {
  13. return areZerosEqual(actual, expected);
  14. // 7.1. All identical values are equivalent, as determined by ===.
  15. } else if (actual === expected) {
  16. return true;
  17. } else if (actual instanceof Date && expected instanceof Date) {
  18. return actual.getTime() === expected.getTime();
  19. } else if (isNumberNaN(actual)) {
  20. return isNumberNaN(expected);
  21. // 7.3. Other pairs that do not both pass typeof value == 'object',
  22. // equivalence is determined by ==.
  23. } else if (typeof actual != 'object' && typeof expected != 'object') {
  24. return actual == expected;
  25. // 7.4. For all other Object pairs, including Array objects, equivalence is
  26. // determined by having the same number of owned properties (as verified
  27. // with Object.prototype.hasOwnProperty.call), the same set of keys
  28. // (although not necessarily the same order), equivalent values for every
  29. // corresponding key, and an identical 'prototype' property. Note: this
  30. // accounts for both named and indexed properties on Arrays.
  31. } else {
  32. return objEquiv(actual, expected);
  33. }
  34. };
  35. function isUndefinedOrNull(value) {
  36. return value === null || value === undefined;
  37. }
  38. function isArguments(object) {
  39. return Object.prototype.toString.call(object) == '[object Arguments]';
  40. }
  41. function isNumberNaN(value) {
  42. // NaN === NaN -> false
  43. return typeof value == 'number' && value !== value;
  44. }
  45. function areZerosEqual(zeroA, zeroB) {
  46. // (1 / +0|0) -> Infinity, but (1 / -0) -> -Infinity and (Infinity !== -Infinity)
  47. return (1 / zeroA) === (1 / zeroB);
  48. }
  49. function objEquiv(a, b) {
  50. if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
  51. return false;
  52. // an identical 'prototype' property.
  53. if (a.prototype !== b.prototype) return false;
  54. //~~~I've managed to break Object.keys through screwy arguments passing.
  55. // Converting to array solves the problem.
  56. if (isArguments(a)) {
  57. if (!isArguments(b)) {
  58. return false;
  59. }
  60. a = pSlice.call(a);
  61. b = pSlice.call(b);
  62. return deepEqual(a, b);
  63. }
  64. try {
  65. var ka = Object_keys(a),
  66. kb = Object_keys(b),
  67. key, i;
  68. } catch (e) {//happens when one is a string literal and the other isn't
  69. return false;
  70. }
  71. // having the same number of owned properties (keys incorporates
  72. // hasOwnProperty)
  73. if (ka.length != kb.length)
  74. return false;
  75. //the same set of keys (although not necessarily the same order),
  76. ka.sort();
  77. kb.sort();
  78. //~~~cheap key test
  79. for (i = ka.length - 1; i >= 0; i--) {
  80. if (ka[i] != kb[i])
  81. return false;
  82. }
  83. //equivalent values for every corresponding key, and
  84. //~~~possibly expensive deep test
  85. for (i = ka.length - 1; i >= 0; i--) {
  86. key = ka[i];
  87. if (!deepEqual(a[key], b[key])) return false;
  88. }
  89. return true;
  90. }