index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. var isArray = Array.isArray;
  3. var keyList = Object.keys;
  4. var hasProp = Object.prototype.hasOwnProperty;
  5. module.exports = function equal(a, b) {
  6. if (a === b) return true;
  7. var arrA = isArray(a)
  8. , arrB = isArray(b)
  9. , i
  10. , length
  11. , key;
  12. if (arrA && arrB) {
  13. length = a.length;
  14. if (length != b.length) return false;
  15. for (i = 0; i < length; i++)
  16. if (!equal(a[i], b[i])) return false;
  17. return true;
  18. }
  19. if (arrA != arrB) return false;
  20. var dateA = a instanceof Date
  21. , dateB = b instanceof Date;
  22. if (dateA != dateB) return false;
  23. if (dateA && dateB) return a.getTime() == b.getTime();
  24. var regexpA = a instanceof RegExp
  25. , regexpB = b instanceof RegExp;
  26. if (regexpA != regexpB) return false;
  27. if (regexpA && regexpB) return a.toString() == b.toString();
  28. if (a instanceof Object && b instanceof Object) {
  29. var keys = keyList(a);
  30. length = keys.length;
  31. if (length !== keyList(b).length)
  32. return false;
  33. for (i = 0; i < length; i++)
  34. if (!hasProp.call(b, keys[i])) return false;
  35. for (i = 0; i < length; i++) {
  36. key = keys[i];
  37. if (!equal(a[key], b[key])) return false;
  38. }
  39. return true;
  40. }
  41. return false;
  42. };