index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. };