index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. // do not edit .js files directly - edit src/index.jst
  3. var envHasBigInt64Array = typeof BigInt64Array !== 'undefined';
  4. module.exports = function equal(a, b) {
  5. if (a === b) return true;
  6. if (a && b && typeof a == 'object' && typeof b == 'object') {
  7. if (a.constructor !== b.constructor) return false;
  8. var length, i, keys;
  9. if (Array.isArray(a)) {
  10. length = a.length;
  11. if (length != b.length) return false;
  12. for (i = length; i-- !== 0;)
  13. if (!equal(a[i], b[i])) return false;
  14. return true;
  15. }
  16. if ((a instanceof Map) && (b instanceof Map)) {
  17. if (a.size !== b.size) return false;
  18. for (i of a.entries())
  19. if (!b.has(i[0])) return false;
  20. for (i of a.entries())
  21. if (!equal(i[1], b.get(i[0]))) return false;
  22. return true;
  23. }
  24. if ((a instanceof Set) && (b instanceof Set)) {
  25. if (a.size !== b.size) return false;
  26. for (i of a.entries())
  27. if (!b.has(i[0])) return false;
  28. return true;
  29. }
  30. if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
  31. length = a.length;
  32. if (length != b.length) return false;
  33. for (i = length; i-- !== 0;)
  34. if (a[i] !== b[i]) return false;
  35. return true;
  36. }
  37. if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
  38. if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
  39. if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
  40. keys = Object.keys(a);
  41. length = keys.length;
  42. if (length !== Object.keys(b).length) return false;
  43. for (i = length; i-- !== 0;)
  44. if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
  45. for (i = length; i-- !== 0;) {
  46. var key = keys[i];
  47. if (!equal(a[key], b[key])) return false;
  48. }
  49. return true;
  50. }
  51. // true if both NaN, false otherwise
  52. return a!==a && b!==b;
  53. };