index.js 4.1 KB

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