index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. // do not edit .js files directly - edit src/index.jst
  3. module.exports = function equal(a, b) {
  4. if (a === b) return true;
  5. if (a && b && typeof a == 'object' && typeof b == 'object') {
  6. if (a.constructor !== b.constructor) return false;
  7. var length, i, keys;
  8. if (Array.isArray(a)) {
  9. length = a.length;
  10. if (length != b.length) return false;
  11. for (i = length; i-- !== 0;)
  12. if (!equal(a[i], b[i])) return false;
  13. return true;
  14. }
  15. if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
  16. if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
  17. if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
  18. keys = Object.keys(a);
  19. length = keys.length;
  20. if (length !== Object.keys(b).length) return false;
  21. for (i = length; i-- !== 0;)
  22. if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
  23. for (i = length; i-- !== 0;) {
  24. var key = keys[i];
  25. if (!equal(a[key], b[key])) return false;
  26. }
  27. return true;
  28. }
  29. // true if both NaN, false otherwise
  30. return a!==a && b!==b;
  31. };