index.js 3.0 KB

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