index.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // src/helpers.ts
  2. function isOfType(type) {
  3. return (value) => typeof value === type;
  4. }
  5. var isFunction = isOfType("function");
  6. var isNull = (value) => {
  7. return value === null;
  8. };
  9. var isRegex = (value) => {
  10. return Object.prototype.toString.call(value).slice(8, -1) === "RegExp";
  11. };
  12. var isObject = (value) => {
  13. return !isUndefined(value) && !isNull(value) && (isFunction(value) || typeof value === "object");
  14. };
  15. var isUndefined = isOfType("undefined");
  16. // src/index.ts
  17. function equalArray(left, right) {
  18. const { length } = left;
  19. if (length !== right.length) {
  20. return false;
  21. }
  22. for (let index = length; index-- !== 0; ) {
  23. if (!equal(left[index], right[index])) {
  24. return false;
  25. }
  26. }
  27. return true;
  28. }
  29. function equalArrayBuffer(left, right) {
  30. if (left.byteLength !== right.byteLength) {
  31. return false;
  32. }
  33. const view1 = new DataView(left.buffer);
  34. const view2 = new DataView(right.buffer);
  35. let index = left.byteLength;
  36. while (index--) {
  37. if (view1.getUint8(index) !== view2.getUint8(index)) {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. function equalMap(left, right) {
  44. if (left.size !== right.size) {
  45. return false;
  46. }
  47. for (const index of left.entries()) {
  48. if (!right.has(index[0])) {
  49. return false;
  50. }
  51. }
  52. for (const index of left.entries()) {
  53. if (!equal(index[1], right.get(index[0]))) {
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59. function equalSet(left, right) {
  60. if (left.size !== right.size) {
  61. return false;
  62. }
  63. for (const index of left.entries()) {
  64. if (!right.has(index[0])) {
  65. return false;
  66. }
  67. }
  68. return true;
  69. }
  70. function equal(left, right) {
  71. if (left === right) {
  72. return true;
  73. }
  74. if (left && isObject(left) && right && isObject(right)) {
  75. if (left.constructor !== right.constructor) {
  76. return false;
  77. }
  78. if (Array.isArray(left) && Array.isArray(right)) {
  79. return equalArray(left, right);
  80. }
  81. if (left instanceof Map && right instanceof Map) {
  82. return equalMap(left, right);
  83. }
  84. if (left instanceof Set && right instanceof Set) {
  85. return equalSet(left, right);
  86. }
  87. if (ArrayBuffer.isView(left) && ArrayBuffer.isView(right)) {
  88. return equalArrayBuffer(left, right);
  89. }
  90. if (isRegex(left) && isRegex(right)) {
  91. return left.source === right.source && left.flags === right.flags;
  92. }
  93. if (left.valueOf !== Object.prototype.valueOf) {
  94. return left.valueOf() === right.valueOf();
  95. }
  96. if (left.toString !== Object.prototype.toString) {
  97. return left.toString() === right.toString();
  98. }
  99. const leftKeys = Object.keys(left);
  100. const rightKeys = Object.keys(right);
  101. if (leftKeys.length !== rightKeys.length) {
  102. return false;
  103. }
  104. for (let index = leftKeys.length; index-- !== 0; ) {
  105. if (!Object.prototype.hasOwnProperty.call(right, leftKeys[index])) {
  106. return false;
  107. }
  108. }
  109. for (let index = leftKeys.length; index-- !== 0; ) {
  110. const key = leftKeys[index];
  111. if (key === "_owner" && left.$$typeof) {
  112. continue;
  113. }
  114. if (!equal(left[key], right[key])) {
  115. return false;
  116. }
  117. }
  118. return true;
  119. }
  120. if (Number.isNaN(left) && Number.isNaN(right)) {
  121. return true;
  122. }
  123. return left === right;
  124. }
  125. export {
  126. equal as default
  127. };
  128. //# sourceMappingURL=index.mjs.map