Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.js 11KB

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