utils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.emptyObject = emptyObject;
  6. exports.isOneline = exports.isError = exports.partition = exports.sparseArrayEquality = exports.typeEquality = exports.subsetEquality = exports.iterableEquality = exports.getObjectSubset = exports.getPath = void 0;
  7. var _jestGetType = require('jest-get-type');
  8. var _jasmineUtils = require('./jasmineUtils');
  9. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  10. /**
  11. * Checks if `hasOwnProperty(object, key)` up the prototype chain, stopping at `Object.prototype`.
  12. */
  13. const hasPropertyInObject = (object, key) => {
  14. const shouldTerminate =
  15. !object || typeof object !== 'object' || object === Object.prototype;
  16. if (shouldTerminate) {
  17. return false;
  18. }
  19. return (
  20. Object.prototype.hasOwnProperty.call(object, key) ||
  21. hasPropertyInObject(Object.getPrototypeOf(object), key)
  22. );
  23. };
  24. const getPath = (object, propertyPath) => {
  25. if (!Array.isArray(propertyPath)) {
  26. propertyPath = propertyPath.split('.');
  27. }
  28. if (propertyPath.length) {
  29. const lastProp = propertyPath.length === 1;
  30. const prop = propertyPath[0];
  31. const newObject = object[prop];
  32. if (!lastProp && (newObject === null || newObject === undefined)) {
  33. // This is not the last prop in the chain. If we keep recursing it will
  34. // hit a `can't access property X of undefined | null`. At this point we
  35. // know that the chain has broken and we can return right away.
  36. return {
  37. hasEndProp: false,
  38. lastTraversedObject: object,
  39. traversedPath: []
  40. };
  41. }
  42. const result = getPath(newObject, propertyPath.slice(1));
  43. if (result.lastTraversedObject === null) {
  44. result.lastTraversedObject = object;
  45. }
  46. result.traversedPath.unshift(prop);
  47. if (lastProp) {
  48. // Does object have the property with an undefined value?
  49. // Although primitive values support bracket notation (above)
  50. // they would throw TypeError for in operator (below).
  51. result.hasEndProp =
  52. newObject !== undefined ||
  53. (!(0, _jestGetType.isPrimitive)(object) && prop in object);
  54. if (!result.hasEndProp) {
  55. result.traversedPath.shift();
  56. }
  57. }
  58. return result;
  59. }
  60. return {
  61. lastTraversedObject: null,
  62. traversedPath: [],
  63. value: object
  64. };
  65. }; // Strip properties from object that are not present in the subset. Useful for
  66. // printing the diff for toMatchObject() without adding unrelated noise.
  67. /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
  68. exports.getPath = getPath;
  69. const getObjectSubset = (object, subset, seenReferences = new WeakMap()) => {
  70. /* eslint-enable @typescript-eslint/explicit-module-boundary-types */
  71. if (Array.isArray(object)) {
  72. if (Array.isArray(subset) && subset.length === object.length) {
  73. // The map method returns correct subclass of subset.
  74. return subset.map((sub, i) => getObjectSubset(object[i], sub));
  75. }
  76. } else if (object instanceof Date) {
  77. return object;
  78. } else if (isObject(object) && isObject(subset)) {
  79. if (
  80. (0, _jasmineUtils.equals)(object, subset, [
  81. iterableEquality,
  82. subsetEquality
  83. ])
  84. ) {
  85. // Avoid unnecessary copy which might return Object instead of subclass.
  86. return subset;
  87. }
  88. const trimmed = {};
  89. seenReferences.set(object, trimmed);
  90. Object.keys(object)
  91. .filter(key => hasPropertyInObject(subset, key))
  92. .forEach(key => {
  93. trimmed[key] = seenReferences.has(object[key])
  94. ? seenReferences.get(object[key])
  95. : getObjectSubset(object[key], subset[key], seenReferences);
  96. });
  97. if (Object.keys(trimmed).length > 0) {
  98. return trimmed;
  99. }
  100. }
  101. return object;
  102. };
  103. exports.getObjectSubset = getObjectSubset;
  104. const IteratorSymbol = Symbol.iterator;
  105. const hasIterator = object => !!(object != null && object[IteratorSymbol]);
  106. /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
  107. const iterableEquality = (
  108. a,
  109. b,
  110. /* eslint-enable @typescript-eslint/explicit-module-boundary-types */
  111. aStack = [],
  112. bStack = []
  113. ) => {
  114. if (
  115. typeof a !== 'object' ||
  116. typeof b !== 'object' ||
  117. Array.isArray(a) ||
  118. Array.isArray(b) ||
  119. !hasIterator(a) ||
  120. !hasIterator(b)
  121. ) {
  122. return undefined;
  123. }
  124. if (a.constructor !== b.constructor) {
  125. return false;
  126. }
  127. let length = aStack.length;
  128. while (length--) {
  129. // Linear search. Performance is inversely proportional to the number of
  130. // unique nested structures.
  131. // circular references at same depth are equal
  132. // circular reference is not equal to non-circular one
  133. if (aStack[length] === a) {
  134. return bStack[length] === b;
  135. }
  136. }
  137. aStack.push(a);
  138. bStack.push(b);
  139. const iterableEqualityWithStack = (a, b) =>
  140. iterableEquality(a, b, [...aStack], [...bStack]);
  141. if (a.size !== undefined) {
  142. if (a.size !== b.size) {
  143. return false;
  144. } else if (
  145. (0, _jasmineUtils.isA)('Set', a) ||
  146. (0, _jasmineUtils.isImmutableUnorderedSet)(a)
  147. ) {
  148. let allFound = true;
  149. for (const aValue of a) {
  150. if (!b.has(aValue)) {
  151. let has = false;
  152. for (const bValue of b) {
  153. const isEqual = (0, _jasmineUtils.equals)(aValue, bValue, [
  154. iterableEqualityWithStack
  155. ]);
  156. if (isEqual === true) {
  157. has = true;
  158. }
  159. }
  160. if (has === false) {
  161. allFound = false;
  162. break;
  163. }
  164. }
  165. } // Remove the first value from the stack of traversed values.
  166. aStack.pop();
  167. bStack.pop();
  168. return allFound;
  169. } else if (
  170. (0, _jasmineUtils.isA)('Map', a) ||
  171. (0, _jasmineUtils.isImmutableUnorderedKeyed)(a)
  172. ) {
  173. let allFound = true;
  174. for (const aEntry of a) {
  175. if (
  176. !b.has(aEntry[0]) ||
  177. !(0, _jasmineUtils.equals)(aEntry[1], b.get(aEntry[0]), [
  178. iterableEqualityWithStack
  179. ])
  180. ) {
  181. let has = false;
  182. for (const bEntry of b) {
  183. const matchedKey = (0, _jasmineUtils.equals)(aEntry[0], bEntry[0], [
  184. iterableEqualityWithStack
  185. ]);
  186. let matchedValue = false;
  187. if (matchedKey === true) {
  188. matchedValue = (0, _jasmineUtils.equals)(aEntry[1], bEntry[1], [
  189. iterableEqualityWithStack
  190. ]);
  191. }
  192. if (matchedValue === true) {
  193. has = true;
  194. }
  195. }
  196. if (has === false) {
  197. allFound = false;
  198. break;
  199. }
  200. }
  201. } // Remove the first value from the stack of traversed values.
  202. aStack.pop();
  203. bStack.pop();
  204. return allFound;
  205. }
  206. }
  207. const bIterator = b[IteratorSymbol]();
  208. for (const aValue of a) {
  209. const nextB = bIterator.next();
  210. if (
  211. nextB.done ||
  212. !(0, _jasmineUtils.equals)(aValue, nextB.value, [
  213. iterableEqualityWithStack
  214. ])
  215. ) {
  216. return false;
  217. }
  218. }
  219. if (!bIterator.next().done) {
  220. return false;
  221. } // Remove the first value from the stack of traversed values.
  222. aStack.pop();
  223. bStack.pop();
  224. return true;
  225. };
  226. exports.iterableEquality = iterableEquality;
  227. const isObject = a => a !== null && typeof a === 'object';
  228. const isObjectWithKeys = a =>
  229. isObject(a) &&
  230. !(a instanceof Error) &&
  231. !(a instanceof Array) &&
  232. !(a instanceof Date);
  233. const subsetEquality = (object, subset) => {
  234. // subsetEquality needs to keep track of the references
  235. // it has already visited to avoid infinite loops in case
  236. // there are circular references in the subset passed to it.
  237. const subsetEqualityWithContext = (seenReferences = new WeakMap()) => (
  238. object,
  239. subset
  240. ) => {
  241. if (!isObjectWithKeys(subset)) {
  242. return undefined;
  243. }
  244. return Object.keys(subset).every(key => {
  245. if (isObjectWithKeys(subset[key])) {
  246. if (seenReferences.has(subset[key])) {
  247. return (0, _jasmineUtils.equals)(object[key], subset[key], [
  248. iterableEquality
  249. ]);
  250. }
  251. seenReferences.set(subset[key], true);
  252. }
  253. const result =
  254. object != null &&
  255. hasPropertyInObject(object, key) &&
  256. (0, _jasmineUtils.equals)(object[key], subset[key], [
  257. iterableEquality,
  258. subsetEqualityWithContext(seenReferences)
  259. ]); // The main goal of using seenReference is to avoid circular node on tree.
  260. // It will only happen within a parent and its child, not a node and nodes next to it (same level)
  261. // We should keep the reference for a parent and its child only
  262. // Thus we should delete the reference immediately so that it doesn't interfere
  263. // other nodes within the same level on tree.
  264. seenReferences.delete(subset[key]);
  265. return result;
  266. });
  267. };
  268. return subsetEqualityWithContext()(object, subset);
  269. }; // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  270. exports.subsetEquality = subsetEquality;
  271. const typeEquality = (a, b) => {
  272. if (a == null || b == null || a.constructor === b.constructor) {
  273. return undefined;
  274. }
  275. return false;
  276. };
  277. exports.typeEquality = typeEquality;
  278. const sparseArrayEquality = (a, b) => {
  279. if (!Array.isArray(a) || !Array.isArray(b)) {
  280. return undefined;
  281. } // A sparse array [, , 1] will have keys ["2"] whereas [undefined, undefined, 1] will have keys ["0", "1", "2"]
  282. const aKeys = Object.keys(a);
  283. const bKeys = Object.keys(b);
  284. return (
  285. (0, _jasmineUtils.equals)(a, b, [iterableEquality, typeEquality], true) &&
  286. (0, _jasmineUtils.equals)(aKeys, bKeys)
  287. );
  288. };
  289. exports.sparseArrayEquality = sparseArrayEquality;
  290. const partition = (items, predicate) => {
  291. const result = [[], []];
  292. items.forEach(item => result[predicate(item) ? 0 : 1].push(item));
  293. return result;
  294. }; // Copied from https://github.com/graingert/angular.js/blob/a43574052e9775cbc1d7dd8a086752c979b0f020/src/Angular.js#L685-L693
  295. exports.partition = partition;
  296. const isError = value => {
  297. switch (Object.prototype.toString.call(value)) {
  298. case '[object Error]':
  299. return true;
  300. case '[object Exception]':
  301. return true;
  302. case '[object DOMException]':
  303. return true;
  304. default:
  305. return value instanceof Error;
  306. }
  307. };
  308. exports.isError = isError;
  309. function emptyObject(obj) {
  310. return obj && typeof obj === 'object' ? !Object.keys(obj).length : false;
  311. }
  312. const MULTILINE_REGEXP = /[\r\n]/;
  313. const isOneline = (expected, received) =>
  314. typeof expected === 'string' &&
  315. typeof received === 'string' &&
  316. (!MULTILINE_REGEXP.test(expected) || !MULTILINE_REGEXP.test(received));
  317. exports.isOneline = isOneline;