jasmineUtils.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.equals = equals;
  6. exports.isA = isA;
  7. exports.fnNameFor = fnNameFor;
  8. exports.isUndefined = isUndefined;
  9. exports.hasProperty = hasProperty;
  10. exports.isImmutableUnorderedKeyed = isImmutableUnorderedKeyed;
  11. exports.isImmutableUnorderedSet = isImmutableUnorderedSet;
  12. /*
  13. Copyright (c) 2008-2016 Pivotal Labs
  14. Permission is hereby granted, free of charge, to any person obtaining
  15. a copy of this software and associated documentation files (the
  16. "Software"), to deal in the Software without restriction, including
  17. without limitation the rights to use, copy, modify, merge, publish,
  18. distribute, sublicense, and/or sell copies of the Software, and to
  19. permit persons to whom the Software is furnished to do so, subject to
  20. the following conditions:
  21. The above copyright notice and this permission notice shall be
  22. included in all copies or substantial portions of the Software.
  23. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. */
  31. /* eslint-disable */
  32. // Extracted out of jasmine 2.5.2
  33. function equals(a, b, customTesters, strictCheck) {
  34. customTesters = customTesters || [];
  35. return eq(a, b, [], [], customTesters, strictCheck ? hasKey : hasDefinedKey);
  36. }
  37. const functionToString = Function.prototype.toString;
  38. function isAsymmetric(obj) {
  39. return !!obj && isA('Function', obj.asymmetricMatch);
  40. }
  41. function asymmetricMatch(a, b) {
  42. var asymmetricA = isAsymmetric(a),
  43. asymmetricB = isAsymmetric(b);
  44. if (asymmetricA && asymmetricB) {
  45. return undefined;
  46. }
  47. if (asymmetricA) {
  48. return a.asymmetricMatch(b);
  49. }
  50. if (asymmetricB) {
  51. return b.asymmetricMatch(a);
  52. }
  53. } // Equality function lovingly adapted from isEqual in
  54. // [Underscore](http://underscorejs.org)
  55. function eq(a, b, aStack, bStack, customTesters, hasKey) {
  56. var result = true;
  57. var asymmetricResult = asymmetricMatch(a, b);
  58. if (asymmetricResult !== undefined) {
  59. return asymmetricResult;
  60. }
  61. for (var i = 0; i < customTesters.length; i++) {
  62. var customTesterResult = customTesters[i](a, b);
  63. if (customTesterResult !== undefined) {
  64. return customTesterResult;
  65. }
  66. }
  67. if (a instanceof Error && b instanceof Error) {
  68. return a.message == b.message;
  69. }
  70. if (Object.is(a, b)) {
  71. return true;
  72. } // A strict comparison is necessary because `null == undefined`.
  73. if (a === null || b === null) {
  74. return a === b;
  75. }
  76. var className = Object.prototype.toString.call(a);
  77. if (className != Object.prototype.toString.call(b)) {
  78. return false;
  79. }
  80. switch (className) {
  81. // Strings, numbers, dates, and booleans are compared by value.
  82. case '[object String]':
  83. // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
  84. // equivalent to `new String("5")`.
  85. return a == String(b);
  86. case '[object Number]':
  87. return Object.is(Number(a), Number(b));
  88. case '[object Date]':
  89. case '[object Boolean]':
  90. // Coerce dates and booleans to numeric primitive values. Dates are compared by their
  91. // millisecond representations. Note that invalid dates with millisecond representations
  92. // of `NaN` are not equivalent.
  93. return +a == +b;
  94. // RegExps are compared by their source patterns and flags.
  95. case '[object RegExp]':
  96. return (
  97. a.source == b.source &&
  98. a.global == b.global &&
  99. a.multiline == b.multiline &&
  100. a.ignoreCase == b.ignoreCase
  101. );
  102. }
  103. if (typeof a != 'object' || typeof b != 'object') {
  104. return false;
  105. } // Use DOM3 method isEqualNode (IE>=9)
  106. if (isDomNode(a) && isDomNode(b)) {
  107. return a.isEqualNode(b);
  108. } // Used to detect circular references.
  109. var length = aStack.length;
  110. while (length--) {
  111. // Linear search. Performance is inversely proportional to the number of
  112. // unique nested structures.
  113. // circular references at same depth are equal
  114. // circular reference is not equal to non-circular one
  115. if (aStack[length] === a) {
  116. return bStack[length] === b;
  117. } else if (bStack[length] === b) {
  118. return false;
  119. }
  120. } // Add the first object to the stack of traversed objects.
  121. aStack.push(a);
  122. bStack.push(b);
  123. var size = 0; // Recursively compare objects and arrays.
  124. // Compare array lengths to determine if a deep comparison is necessary.
  125. if (className == '[object Array]') {
  126. size = a.length;
  127. if (size !== b.length) {
  128. return false;
  129. }
  130. while (size--) {
  131. result = eq(a[size], b[size], aStack, bStack, customTesters, hasKey);
  132. if (!result) {
  133. return false;
  134. }
  135. }
  136. } // Deep compare objects.
  137. var aKeys = keys(a, className == '[object Array]', hasKey),
  138. key;
  139. size = aKeys.length; // Ensure that both objects contain the same number of properties before comparing deep equality.
  140. if (keys(b, className == '[object Array]', hasKey).length !== size) {
  141. return false;
  142. }
  143. while (size--) {
  144. key = aKeys[size]; // Deep compare each member
  145. result =
  146. hasKey(b, key) &&
  147. eq(a[key], b[key], aStack, bStack, customTesters, hasKey);
  148. if (!result) {
  149. return false;
  150. }
  151. } // Remove the first object from the stack of traversed objects.
  152. aStack.pop();
  153. bStack.pop();
  154. return result;
  155. }
  156. function keys(obj, isArray, hasKey) {
  157. var allKeys = (function(o) {
  158. var keys = [];
  159. for (var key in o) {
  160. if (hasKey(o, key)) {
  161. keys.push(key);
  162. }
  163. }
  164. return keys.concat(
  165. Object.getOwnPropertySymbols(o).filter(
  166. symbol => Object.getOwnPropertyDescriptor(o, symbol).enumerable
  167. )
  168. );
  169. })(obj);
  170. if (!isArray) {
  171. return allKeys;
  172. }
  173. var extraKeys = [];
  174. if (allKeys.length === 0) {
  175. return allKeys;
  176. }
  177. for (var x = 0; x < allKeys.length; x++) {
  178. if (typeof allKeys[x] === 'symbol' || !allKeys[x].match(/^[0-9]+$/)) {
  179. extraKeys.push(allKeys[x]);
  180. }
  181. }
  182. return extraKeys;
  183. }
  184. function hasDefinedKey(obj, key) {
  185. return hasKey(obj, key) && obj[key] !== undefined;
  186. }
  187. function hasKey(obj, key) {
  188. return Object.prototype.hasOwnProperty.call(obj, key);
  189. }
  190. function isA(typeName, value) {
  191. return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
  192. }
  193. function isDomNode(obj) {
  194. return (
  195. obj !== null &&
  196. typeof obj === 'object' &&
  197. typeof obj.nodeType === 'number' &&
  198. typeof obj.nodeName === 'string' &&
  199. typeof obj.isEqualNode === 'function'
  200. );
  201. }
  202. function fnNameFor(func) {
  203. if (func.name) {
  204. return func.name;
  205. }
  206. const matches = functionToString
  207. .call(func)
  208. .match(/^(?:async)?\s*function\s*\*?\s*([\w$]+)\s*\(/);
  209. return matches ? matches[1] : '<anonymous>';
  210. }
  211. function isUndefined(obj) {
  212. return obj === void 0;
  213. }
  214. function getPrototype(obj) {
  215. if (Object.getPrototypeOf) {
  216. return Object.getPrototypeOf(obj);
  217. }
  218. if (obj.constructor.prototype == obj) {
  219. return null;
  220. }
  221. return obj.constructor.prototype;
  222. }
  223. function hasProperty(obj, property) {
  224. if (!obj) {
  225. return false;
  226. }
  227. if (Object.prototype.hasOwnProperty.call(obj, property)) {
  228. return true;
  229. }
  230. return hasProperty(getPrototype(obj), property);
  231. } // SENTINEL constants are from https://github.com/facebook/immutable-js
  232. const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
  233. const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
  234. const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';
  235. function isImmutableUnorderedKeyed(maybeKeyed) {
  236. return !!(
  237. maybeKeyed &&
  238. maybeKeyed[IS_KEYED_SENTINEL] &&
  239. !maybeKeyed[IS_ORDERED_SENTINEL]
  240. );
  241. }
  242. function isImmutableUnorderedSet(maybeSet) {
  243. return !!(
  244. maybeSet &&
  245. maybeSet[IS_SET_SENTINEL] &&
  246. !maybeSet[IS_ORDERED_SENTINEL]
  247. );
  248. }