版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

229 líneas
7.2 KiB

  1. export { assertTypes, clone, createDefer, deepClone, getOwnProperties, getType, isObject, noop, objectAttr, parseRegexp, slash, toArray } from './helpers.js';
  2. import { format as format$1, plugins } from 'pretty-format';
  3. import util from 'util';
  4. import loupeImport from 'loupe';
  5. const {
  6. AsymmetricMatcher,
  7. DOMCollection,
  8. DOMElement,
  9. Immutable,
  10. ReactElement,
  11. ReactTestComponent
  12. } = plugins;
  13. const PLUGINS = [
  14. ReactTestComponent,
  15. ReactElement,
  16. DOMElement,
  17. DOMCollection,
  18. Immutable,
  19. AsymmetricMatcher
  20. ];
  21. function stringify(object, maxDepth = 10, { maxLength, ...options } = {}) {
  22. const MAX_LENGTH = maxLength ?? 1e4;
  23. let result;
  24. try {
  25. result = format$1(object, {
  26. maxDepth,
  27. escapeString: false,
  28. plugins: PLUGINS,
  29. ...options
  30. });
  31. } catch {
  32. result = format$1(object, {
  33. callToJSON: false,
  34. maxDepth,
  35. escapeString: false,
  36. plugins: PLUGINS,
  37. ...options
  38. });
  39. }
  40. return result.length >= MAX_LENGTH && maxDepth > 1 ? stringify(object, Math.floor(maxDepth / 2)) : result;
  41. }
  42. const SAFE_TIMERS_SYMBOL = Symbol("vitest:SAFE_TIMERS");
  43. const SAFE_COLORS_SYMBOL = Symbol("vitest:SAFE_COLORS");
  44. function getSafeTimers() {
  45. const {
  46. setTimeout: safeSetTimeout,
  47. setInterval: safeSetInterval,
  48. clearInterval: safeClearInterval,
  49. clearTimeout: safeClearTimeout,
  50. setImmediate: safeSetImmediate,
  51. clearImmediate: safeClearImmediate
  52. } = globalThis[SAFE_TIMERS_SYMBOL] || globalThis;
  53. const {
  54. nextTick: safeNextTick
  55. } = globalThis[SAFE_TIMERS_SYMBOL] || globalThis.process || { nextTick: (cb) => cb() };
  56. return {
  57. nextTick: safeNextTick,
  58. setTimeout: safeSetTimeout,
  59. setInterval: safeSetInterval,
  60. clearInterval: safeClearInterval,
  61. clearTimeout: safeClearTimeout,
  62. setImmediate: safeSetImmediate,
  63. clearImmediate: safeClearImmediate
  64. };
  65. }
  66. function setSafeTimers() {
  67. const {
  68. setTimeout: safeSetTimeout,
  69. setInterval: safeSetInterval,
  70. clearInterval: safeClearInterval,
  71. clearTimeout: safeClearTimeout,
  72. setImmediate: safeSetImmediate,
  73. clearImmediate: safeClearImmediate
  74. } = globalThis;
  75. const {
  76. nextTick: safeNextTick
  77. } = globalThis.process || { nextTick: (cb) => cb() };
  78. const timers = {
  79. nextTick: safeNextTick,
  80. setTimeout: safeSetTimeout,
  81. setInterval: safeSetInterval,
  82. clearInterval: safeClearInterval,
  83. clearTimeout: safeClearTimeout,
  84. setImmediate: safeSetImmediate,
  85. clearImmediate: safeClearImmediate
  86. };
  87. globalThis[SAFE_TIMERS_SYMBOL] = timers;
  88. }
  89. const RealDate = Date;
  90. function random(seed) {
  91. const x = Math.sin(seed++) * 1e4;
  92. return x - Math.floor(x);
  93. }
  94. function shuffle(array, seed = RealDate.now()) {
  95. let length = array.length;
  96. while (length) {
  97. const index = Math.floor(random(seed) * length--);
  98. const previous = array[length];
  99. array[length] = array[index];
  100. array[index] = previous;
  101. ++seed;
  102. }
  103. return array;
  104. }
  105. const loupe = typeof loupeImport.default === "function" ? loupeImport.default : loupeImport;
  106. function format(...args) {
  107. return util.format(...args);
  108. }
  109. function utilInspect(item, options) {
  110. return util.inspect(item, options);
  111. }
  112. function loupeInspect(obj) {
  113. return loupe(obj, {
  114. depth: 2,
  115. truncate: 40
  116. });
  117. }
  118. function objDisplay(obj) {
  119. const truncateThreshold = 40;
  120. const str = loupeInspect(obj);
  121. const type = Object.prototype.toString.call(obj);
  122. if (str.length >= truncateThreshold) {
  123. if (type === "[object Function]") {
  124. const fn = obj;
  125. return !fn.name || fn.name === "" ? "[Function]" : `[Function: ${fn.name}]`;
  126. } else if (type === "[object Array]") {
  127. return `[ Array(${obj.length}) ]`;
  128. } else if (type === "[object Object]") {
  129. const keys = Object.keys(obj);
  130. const kstr = keys.length > 2 ? `${keys.splice(0, 2).join(", ")}, ...` : keys.join(", ");
  131. return `{ Object (${kstr}) }`;
  132. } else {
  133. return str;
  134. }
  135. }
  136. return str;
  137. }
  138. const colorsMap = {
  139. bold: ["\x1B[1m", "\x1B[22m", "\x1B[22m\x1B[1m"],
  140. dim: ["\x1B[2m", "\x1B[22m", "\x1B[22m\x1B[2m"],
  141. italic: ["\x1B[3m", "\x1B[23m"],
  142. underline: ["\x1B[4m", "\x1B[24m"],
  143. inverse: ["\x1B[7m", "\x1B[27m"],
  144. hidden: ["\x1B[8m", "\x1B[28m"],
  145. strikethrough: ["\x1B[9m", "\x1B[29m"],
  146. black: ["\x1B[30m", "\x1B[39m"],
  147. red: ["\x1B[31m", "\x1B[39m"],
  148. green: ["\x1B[32m", "\x1B[39m"],
  149. yellow: ["\x1B[33m", "\x1B[39m"],
  150. blue: ["\x1B[34m", "\x1B[39m"],
  151. magenta: ["\x1B[35m", "\x1B[39m"],
  152. cyan: ["\x1B[36m", "\x1B[39m"],
  153. white: ["\x1B[37m", "\x1B[39m"],
  154. gray: ["\x1B[90m", "\x1B[39m"],
  155. bgBlack: ["\x1B[40m", "\x1B[49m"],
  156. bgRed: ["\x1B[41m", "\x1B[49m"],
  157. bgGreen: ["\x1B[42m", "\x1B[49m"],
  158. bgYellow: ["\x1B[43m", "\x1B[49m"],
  159. bgBlue: ["\x1B[44m", "\x1B[49m"],
  160. bgMagenta: ["\x1B[45m", "\x1B[49m"],
  161. bgCyan: ["\x1B[46m", "\x1B[49m"],
  162. bgWhite: ["\x1B[47m", "\x1B[49m"]
  163. };
  164. const colorsEntries = Object.entries(colorsMap);
  165. const string = (str) => String(str);
  166. string.open = "";
  167. string.close = "";
  168. const defaultColors = colorsEntries.reduce((acc, [key]) => {
  169. acc[key] = string;
  170. return acc;
  171. }, { isColorSupported: false });
  172. function getDefaultColors() {
  173. return { ...defaultColors };
  174. }
  175. function getColors() {
  176. return globalThis[SAFE_COLORS_SYMBOL] || defaultColors;
  177. }
  178. function createColors(isTTY = false) {
  179. const enabled = typeof process !== "undefined" && !("NO_COLOR" in process.env || process.argv.includes("--no-color")) && !("GITHUB_ACTIONS" in process.env) && ("FORCE_COLOR" in process.env || process.argv.includes("--color") || process.platform === "win32" || isTTY && process.env.TERM !== "dumb" || "CI" in process.env);
  180. const replaceClose = (string2, close, replace, index) => {
  181. const start = string2.substring(0, index) + replace;
  182. const end = string2.substring(index + close.length);
  183. const nextIndex = end.indexOf(close);
  184. return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end;
  185. };
  186. const formatter = (open, close, replace = open) => {
  187. const fn = (input) => {
  188. const string2 = String(input);
  189. const index = string2.indexOf(close, open.length);
  190. return ~index ? open + replaceClose(string2, close, replace, index) + close : open + string2 + close;
  191. };
  192. fn.open = open;
  193. fn.close = close;
  194. return fn;
  195. };
  196. const colorsObject = {
  197. isColorSupported: enabled,
  198. reset: enabled ? (s) => `\x1B[0m${s}\x1B[0m` : string
  199. };
  200. for (const [name, formatterArgs] of colorsEntries) {
  201. colorsObject[name] = enabled ? formatter(...formatterArgs) : string;
  202. }
  203. return colorsObject;
  204. }
  205. function setupColors(colors) {
  206. globalThis[SAFE_COLORS_SYMBOL] = colors;
  207. }
  208. function createSimpleStackTrace(options) {
  209. const { message = "error", stackTraceLimit = 1 } = options || {};
  210. const limit = Error.stackTraceLimit;
  211. const prepareStackTrace = Error.prepareStackTrace;
  212. Error.stackTraceLimit = stackTraceLimit;
  213. Error.prepareStackTrace = (e) => e.stack;
  214. const err = new Error(message);
  215. const stackTrace = err.stack || "";
  216. Error.prepareStackTrace = prepareStackTrace;
  217. Error.stackTraceLimit = limit;
  218. return stackTrace;
  219. }
  220. export { SAFE_COLORS_SYMBOL, SAFE_TIMERS_SYMBOL, createColors, createSimpleStackTrace, format, getColors, getDefaultColors, getSafeTimers, loupeInspect, objDisplay, setSafeTimers, setupColors, shuffle, stringify, utilInspect };