deep-equal.js 7.6 KB

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