equal.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. /*eslint complexity: 0*/
  3. module.exports = function equal(a, b) {
  4. if (a === b) return true;
  5. var arrA = Array.isArray(a)
  6. , arrB = Array.isArray(b)
  7. , i;
  8. if (arrA && arrB) {
  9. if (a.length != b.length) return false;
  10. for (i = 0; i < a.length; i++)
  11. if (!equal(a[i], b[i])) return false;
  12. return true;
  13. }
  14. if (arrA != arrB) return false;
  15. if (a && b && typeof a === 'object' && typeof b === 'object') {
  16. var keys = Object.keys(a);
  17. if (keys.length !== Object.keys(b).length) return false;
  18. var dateA = a instanceof Date
  19. , dateB = b instanceof Date;
  20. if (dateA && dateB) return a.getTime() == b.getTime();
  21. if (dateA != dateB) return false;
  22. var regexpA = a instanceof RegExp
  23. , regexpB = b instanceof RegExp;
  24. if (regexpA && regexpB) return a.toString() == b.toString();
  25. if (regexpA != regexpB) return false;
  26. for (i = 0; i < keys.length; i++)
  27. if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
  28. for (i = 0; i < keys.length; i++)
  29. if(!equal(a[keys[i]], b[keys[i]])) return false;
  30. return true;
  31. }
  32. return false;
  33. };