deepEqual.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. 'use strict';
  2. const Types = require('./types');
  3. const internals = {
  4. mismatched: null
  5. };
  6. module.exports = function (obj, ref, options) {
  7. options = Object.assign({ prototype: true }, options);
  8. return !!internals.isDeepEqual(obj, ref, options, []);
  9. };
  10. internals.isDeepEqual = function (obj, ref, options, seen) {
  11. if (obj === ref) { // Copied from Deep-eql, copyright(c) 2013 Jake Luer, jake@alogicalparadox.com, MIT Licensed, https://github.com/chaijs/deep-eql
  12. return obj !== 0 || 1 / obj === 1 / ref;
  13. }
  14. const type = typeof obj;
  15. if (type !== typeof ref) {
  16. return false;
  17. }
  18. if (obj === null ||
  19. ref === null) {
  20. return false;
  21. }
  22. if (type === 'function') {
  23. if (!options.deepFunction ||
  24. obj.toString() !== ref.toString()) {
  25. return false;
  26. }
  27. // Continue as object
  28. }
  29. else if (type !== 'object') {
  30. return obj !== obj && ref !== ref; // NaN
  31. }
  32. const instanceType = internals.getSharedType(obj, ref, !!options.prototype);
  33. switch (instanceType) {
  34. case Types.buffer:
  35. return Buffer && Buffer.prototype.equals.call(obj, ref); // $lab:coverage:ignore$
  36. case Types.promise:
  37. return obj === ref;
  38. case Types.regex:
  39. return obj.toString() === ref.toString();
  40. case internals.mismatched:
  41. return false;
  42. }
  43. for (let i = seen.length - 1; i >= 0; --i) {
  44. if (seen[i].isSame(obj, ref)) {
  45. return true; // If previous comparison failed, it would have stopped execution
  46. }
  47. }
  48. seen.push(new internals.SeenEntry(obj, ref));
  49. try {
  50. return !!internals.isDeepEqualObj(instanceType, obj, ref, options, seen);
  51. }
  52. finally {
  53. seen.pop();
  54. }
  55. };
  56. internals.getSharedType = function (obj, ref, checkPrototype) {
  57. if (checkPrototype) {
  58. if (Object.getPrototypeOf(obj) !== Object.getPrototypeOf(ref)) {
  59. return internals.mismatched;
  60. }
  61. return Types.getInternalProto(obj);
  62. }
  63. const type = Types.getInternalProto(obj);
  64. if (type !== Types.getInternalProto(ref)) {
  65. return internals.mismatched;
  66. }
  67. return type;
  68. };
  69. internals.valueOf = function (obj) {
  70. const objValueOf = obj.valueOf;
  71. if (objValueOf === undefined) {
  72. return obj;
  73. }
  74. try {
  75. return objValueOf.call(obj);
  76. }
  77. catch (err) {
  78. return err;
  79. }
  80. };
  81. internals.hasOwnEnumerableProperty = function (obj, key) {
  82. return Object.prototype.propertyIsEnumerable.call(obj, key);
  83. };
  84. internals.isSetSimpleEqual = function (obj, ref) {
  85. for (const entry of obj) {
  86. if (!ref.has(entry)) {
  87. return false;
  88. }
  89. }
  90. return true;
  91. };
  92. internals.isDeepEqualObj = function (instanceType, obj, ref, options, seen) {
  93. const { isDeepEqual, valueOf, hasOwnEnumerableProperty } = internals;
  94. const { keys, getOwnPropertySymbols } = Object;
  95. if (instanceType === Types.array) {
  96. if (options.part) {
  97. // Check if any index match any other index
  98. for (const objValue of obj) {
  99. for (const refValue of ref) {
  100. if (isDeepEqual(objValue, refValue, options, seen)) {
  101. return true;
  102. }
  103. }
  104. }
  105. }
  106. else {
  107. if (obj.length !== ref.length) {
  108. return false;
  109. }
  110. for (let i = 0; i < obj.length; ++i) {
  111. if (!isDeepEqual(obj[i], ref[i], options, seen)) {
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. }
  118. else if (instanceType === Types.set) {
  119. if (obj.size !== ref.size) {
  120. return false;
  121. }
  122. if (!internals.isSetSimpleEqual(obj, ref)) {
  123. // Check for deep equality
  124. const ref2 = new Set(ref);
  125. for (const objEntry of obj) {
  126. if (ref2.delete(objEntry)) {
  127. continue;
  128. }
  129. let found = false;
  130. for (const refEntry of ref2) {
  131. if (isDeepEqual(objEntry, refEntry, options, seen)) {
  132. ref2.delete(refEntry);
  133. found = true;
  134. break;
  135. }
  136. }
  137. if (!found) {
  138. return false;
  139. }
  140. }
  141. }
  142. }
  143. else if (instanceType === Types.map) {
  144. if (obj.size !== ref.size) {
  145. return false;
  146. }
  147. for (const [key, value] of obj) {
  148. if (value === undefined && !ref.has(key)) {
  149. return false;
  150. }
  151. if (!isDeepEqual(value, ref.get(key), options, seen)) {
  152. return false;
  153. }
  154. }
  155. }
  156. else if (instanceType === Types.error) {
  157. // Always check name and message
  158. if (obj.name !== ref.name ||
  159. obj.message !== ref.message) {
  160. return false;
  161. }
  162. }
  163. // Check .valueOf()
  164. const valueOfObj = valueOf(obj);
  165. const valueOfRef = valueOf(ref);
  166. if ((obj !== valueOfObj || ref !== valueOfRef) &&
  167. !isDeepEqual(valueOfObj, valueOfRef, options, seen)) {
  168. return false;
  169. }
  170. // Check properties
  171. const objKeys = keys(obj);
  172. if (!options.part &&
  173. objKeys.length !== keys(ref).length &&
  174. !options.skip) {
  175. return false;
  176. }
  177. let skipped = 0;
  178. for (const key of objKeys) {
  179. if (options.skip &&
  180. options.skip.includes(key)) {
  181. if (ref[key] === undefined) {
  182. ++skipped;
  183. }
  184. continue;
  185. }
  186. if (!hasOwnEnumerableProperty(ref, key)) {
  187. return false;
  188. }
  189. if (!isDeepEqual(obj[key], ref[key], options, seen)) {
  190. return false;
  191. }
  192. }
  193. if (!options.part &&
  194. objKeys.length - skipped !== keys(ref).length) {
  195. return false;
  196. }
  197. // Check symbols
  198. if (options.symbols !== false) { // Defaults to true
  199. const objSymbols = getOwnPropertySymbols(obj);
  200. const refSymbols = new Set(getOwnPropertySymbols(ref));
  201. for (const key of objSymbols) {
  202. if (!options.skip ||
  203. !options.skip.includes(key)) {
  204. if (hasOwnEnumerableProperty(obj, key)) {
  205. if (!hasOwnEnumerableProperty(ref, key)) {
  206. return false;
  207. }
  208. if (!isDeepEqual(obj[key], ref[key], options, seen)) {
  209. return false;
  210. }
  211. }
  212. else if (hasOwnEnumerableProperty(ref, key)) {
  213. return false;
  214. }
  215. }
  216. refSymbols.delete(key);
  217. }
  218. for (const key of refSymbols) {
  219. if (hasOwnEnumerableProperty(ref, key)) {
  220. return false;
  221. }
  222. }
  223. }
  224. return true;
  225. };
  226. internals.SeenEntry = class {
  227. constructor(obj, ref) {
  228. this.obj = obj;
  229. this.ref = ref;
  230. }
  231. isSame(obj, ref) {
  232. return this.obj === obj && this.ref === ref;
  233. }
  234. };