deep-compare-strict.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.deepCompareStrict = deepCompareStrict;
  4. function deepCompareStrict(a, b) {
  5. const typeofa = typeof a;
  6. if (typeofa !== typeof b) {
  7. return false;
  8. }
  9. if (Array.isArray(a)) {
  10. if (!Array.isArray(b)) {
  11. return false;
  12. }
  13. const length = a.length;
  14. if (length !== b.length) {
  15. return false;
  16. }
  17. for (let i = 0; i < length; i++) {
  18. if (!deepCompareStrict(a[i], b[i])) {
  19. return false;
  20. }
  21. }
  22. return true;
  23. }
  24. if (typeofa === 'object') {
  25. if (!a || !b) {
  26. return a === b;
  27. }
  28. const aKeys = Object.keys(a);
  29. const bKeys = Object.keys(b);
  30. const length = aKeys.length;
  31. if (length !== bKeys.length) {
  32. return false;
  33. }
  34. for (const k of aKeys) {
  35. if (!deepCompareStrict(a[k], b[k])) {
  36. return false;
  37. }
  38. }
  39. return true;
  40. }
  41. return a === b;
  42. }