utils.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.partition = exports.subsetEquality = exports.iterableEquality = exports.getObjectSubset = exports.getPath = exports.hasOwnProperty = undefined;
  6. var _jasmine_utils = require('./jasmine_utils');
  7. const hasOwnProperty = exports.hasOwnProperty = (object, value) => Object.prototype.hasOwnProperty.call(object, value) || Object.prototype.hasOwnProperty.call(object.constructor.prototype, value); /**
  8. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  9. *
  10. * This source code is licensed under the MIT license found in the
  11. * LICENSE file in the root directory of this source tree.
  12. *
  13. *
  14. */
  15. const getPath = exports.getPath = (object, propertyPath) => {
  16. if (!Array.isArray(propertyPath)) {
  17. propertyPath = propertyPath.split('.');
  18. }
  19. if (propertyPath.length) {
  20. const lastProp = propertyPath.length === 1;
  21. const prop = propertyPath[0];
  22. const newObject = object[prop];
  23. if (!lastProp && (newObject === null || newObject === undefined)) {
  24. // This is not the last prop in the chain. If we keep recursing it will
  25. // hit a `can't access property X of undefined | null`. At this point we
  26. // know that the chain has broken and we can return right away.
  27. return {
  28. hasEndProp: false,
  29. lastTraversedObject: object,
  30. traversedPath: []
  31. };
  32. }
  33. const result = getPath(newObject, propertyPath.slice(1));
  34. if (result.lastTraversedObject === null) {
  35. result.lastTraversedObject = object;
  36. }
  37. result.traversedPath.unshift(prop);
  38. if (lastProp) {
  39. result.hasEndProp = hasOwnProperty(object, prop);
  40. if (!result.hasEndProp) {
  41. result.traversedPath.shift();
  42. }
  43. }
  44. return result;
  45. }
  46. return {
  47. lastTraversedObject: null,
  48. traversedPath: [],
  49. value: object
  50. };
  51. };
  52. // Strip properties from object that are not present in the subset. Useful for
  53. // printing the diff for toMatchObject() without adding unrelated noise.
  54. const getObjectSubset = exports.getObjectSubset = (object, subset) => {
  55. if (Array.isArray(object)) {
  56. if (Array.isArray(subset) && subset.length === object.length) {
  57. return subset.map((sub, i) => getObjectSubset(object[i], sub));
  58. }
  59. } else if (object instanceof Date) {
  60. return object;
  61. } else if (typeof object === 'object' && object !== null && typeof subset === 'object' && subset !== null) {
  62. const trimmed = {};
  63. Object.keys(subset).filter(key => hasOwnProperty(object, key)).forEach(key => trimmed[key] = getObjectSubset(object[key], subset[key]));
  64. if (Object.keys(trimmed).length > 0) {
  65. return trimmed;
  66. }
  67. }
  68. return object;
  69. };
  70. const IteratorSymbol = Symbol.iterator;
  71. const hasIterator = object => !!(object != null && object[IteratorSymbol]);
  72. const iterableEquality = exports.iterableEquality = (a, b) => {
  73. if (typeof a !== 'object' || typeof b !== 'object' || Array.isArray(a) || Array.isArray(b) || !hasIterator(a) || !hasIterator(b)) {
  74. return undefined;
  75. }
  76. if (a.constructor !== b.constructor) {
  77. return false;
  78. }
  79. if (a.size !== undefined) {
  80. if (a.size !== b.size) {
  81. return false;
  82. } else if ((0, _jasmine_utils.isA)('Set', a) || (0, _jasmine_utils.isImmutableUnorderedSet)(a)) {
  83. let allFound = true;
  84. for (const aValue of a) {
  85. if (!b.has(aValue)) {
  86. allFound = false;
  87. break;
  88. }
  89. }
  90. if (allFound) {
  91. return true;
  92. }
  93. } else if ((0, _jasmine_utils.isA)('Map', a) || (0, _jasmine_utils.isImmutableUnorderedKeyed)(a)) {
  94. let allFound = true;
  95. for (const aEntry of a) {
  96. if (!b.has(aEntry[0]) || !(0, _jasmine_utils.equals)(aEntry[1], b.get(aEntry[0]), [iterableEquality])) {
  97. allFound = false;
  98. break;
  99. }
  100. }
  101. if (allFound) {
  102. return true;
  103. }
  104. }
  105. }
  106. const bIterator = b[IteratorSymbol]();
  107. for (const aValue of a) {
  108. const nextB = bIterator.next();
  109. if (nextB.done || !(0, _jasmine_utils.equals)(aValue, nextB.value, [iterableEquality])) {
  110. return false;
  111. }
  112. }
  113. if (!bIterator.next().done) {
  114. return false;
  115. }
  116. return true;
  117. };
  118. const isObjectWithKeys = a => a !== null && typeof a === 'object' && !(a instanceof Error) && !(a instanceof Array) && !(a instanceof Date);
  119. const subsetEquality = exports.subsetEquality = (object, subset) => {
  120. if (!isObjectWithKeys(subset)) {
  121. return undefined;
  122. }
  123. return Object.keys(subset).every(key => hasOwnProperty(object, key) && (0, _jasmine_utils.equals)(object[key], subset[key], [iterableEquality, subsetEquality]));
  124. };
  125. const partition = exports.partition = (items, predicate) => {
  126. const result = [[], []];
  127. items.forEach(item => result[predicate(item) ? 0 : 1].push(item));
  128. return result;
  129. };