@vitest/utils
Advanced tools
Comparing version 0.27.3 to 0.28.0
declare function formatLine(line: string, outputTruncateLength?: number): string; | ||
type Color = (str: string) => string; | ||
interface DiffOptions { | ||
noColor?: boolean; | ||
outputDiffMaxLines?: number; | ||
@@ -8,2 +8,5 @@ outputTruncateLength?: number; | ||
showLegend?: boolean; | ||
colorSuccess?: Color; | ||
colorError?: Color; | ||
colorDim?: Color; | ||
} | ||
@@ -10,0 +13,0 @@ /** |
@@ -1,2 +0,1 @@ | ||
import c from 'picocolors'; | ||
import * as diff from 'diff'; | ||
@@ -12,3 +11,3 @@ import cliTruncate from 'cli-truncate'; | ||
return ""; | ||
const { outputTruncateLength, outputDiffLines, outputDiffMaxLines, noColor, showLegend = true } = options; | ||
const { outputTruncateLength, outputDiffLines, outputDiffMaxLines, showLegend = true } = options; | ||
const indent = " "; | ||
@@ -24,5 +23,5 @@ const diffLimit = outputDiffLines || 15; | ||
const str = (str2) => str2; | ||
const dim = noColor ? str : c.dim; | ||
const green = noColor ? str : c.green; | ||
const red = noColor ? str : c.red; | ||
const dim = options.colorDim || str; | ||
const green = options.colorSuccess || str; | ||
const red = options.colorError || str; | ||
function preprocess(line) { | ||
@@ -29,0 +28,0 @@ if (!line || line.match(/\\ No newline/)) |
@@ -0,4 +1,19 @@ | ||
import { Nullable, Arrayable } from './types.js'; | ||
declare function assertTypes(value: unknown, name: string, types: string[]): void; | ||
declare function slash(path: string): string; | ||
declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>; | ||
declare function isObject(item: unknown): boolean; | ||
declare function getType(value: unknown): string; | ||
declare function getOwnProperties(obj: any): (string | symbol)[]; | ||
declare function deepClone<T>(val: T): T; | ||
declare function clone<T>(val: T, seen: WeakMap<any, any>): T; | ||
declare function noop(): void; | ||
declare function objectAttr(source: any, path: string, defaultValue?: undefined): any; | ||
type DeferPromise<T> = Promise<T> & { | ||
resolve: (value: T | PromiseLike<T>) => void; | ||
reject: (reason?: any) => void; | ||
}; | ||
declare function createDefer<T>(): DeferPromise<T>; | ||
export { assertTypes, isObject }; | ||
export { assertTypes, clone, createDefer, deepClone, getOwnProperties, getType, isObject, noop, objectAttr, slash, toArray }; |
@@ -7,6 +7,82 @@ function assertTypes(value, name, types) { | ||
} | ||
function slash(path) { | ||
return path.replace(/\\/g, "/"); | ||
} | ||
function toArray(array) { | ||
if (array === null || array === void 0) | ||
array = []; | ||
if (Array.isArray(array)) | ||
return array; | ||
return [array]; | ||
} | ||
function isObject(item) { | ||
return item != null && typeof item === "object" && !Array.isArray(item); | ||
} | ||
function isFinalObj(obj) { | ||
return obj === Object.prototype || obj === Function.prototype || obj === RegExp.prototype; | ||
} | ||
function getType(value) { | ||
return Object.prototype.toString.apply(value).slice(8, -1); | ||
} | ||
function collectOwnProperties(obj, collector) { | ||
const collect = typeof collector === "function" ? collector : (key) => collector.add(key); | ||
Object.getOwnPropertyNames(obj).forEach(collect); | ||
Object.getOwnPropertySymbols(obj).forEach(collect); | ||
} | ||
function getOwnProperties(obj) { | ||
const ownProps = /* @__PURE__ */ new Set(); | ||
if (isFinalObj(obj)) | ||
return []; | ||
collectOwnProperties(obj, ownProps); | ||
return Array.from(ownProps); | ||
} | ||
function deepClone(val) { | ||
const seen = /* @__PURE__ */ new WeakMap(); | ||
return clone(val, seen); | ||
} | ||
function clone(val, seen) { | ||
let k, out; | ||
if (seen.has(val)) | ||
return seen.get(val); | ||
if (Array.isArray(val)) { | ||
out = Array(k = val.length); | ||
seen.set(val, out); | ||
while (k--) | ||
out[k] = clone(val[k], seen); | ||
return out; | ||
} | ||
if (Object.prototype.toString.call(val) === "[object Object]") { | ||
out = Object.create(Object.getPrototypeOf(val)); | ||
seen.set(val, out); | ||
const props = getOwnProperties(val); | ||
for (const k2 of props) | ||
out[k2] = clone(val[k2], seen); | ||
return out; | ||
} | ||
return val; | ||
} | ||
function noop() { | ||
} | ||
function objectAttr(source, path, defaultValue = void 0) { | ||
const paths = path.replace(/\[(\d+)\]/g, ".$1").split("."); | ||
let result = source; | ||
for (const p of paths) { | ||
result = Object(result)[p]; | ||
if (result === void 0) | ||
return defaultValue; | ||
} | ||
return result; | ||
} | ||
function createDefer() { | ||
let resolve = null; | ||
let reject = null; | ||
const p = new Promise((_resolve, _reject) => { | ||
resolve = _resolve; | ||
reject = _reject; | ||
}); | ||
p.resolve = resolve; | ||
p.reject = reject; | ||
return p; | ||
} | ||
export { assertTypes, isObject }; | ||
export { assertTypes, clone, createDefer, deepClone, getOwnProperties, getType, isObject, noop, objectAttr, slash, toArray }; |
@@ -1,5 +0,5 @@ | ||
export { DiffOptions, formatLine, unifiedDiff } from './diff.js'; | ||
export { assertTypes, isObject } from './helpers.js'; | ||
export { assertTypes, clone, createDefer, deepClone, getOwnProperties, getType, isObject, noop, objectAttr, slash, toArray } from './helpers.js'; | ||
export { ArgumentsType, Arrayable, Awaitable, Constructable, DeepMerge, MergeInsertions, MutableArray, Nullable } from './types.js'; | ||
import { PrettyFormatOptions } from 'pretty-format'; | ||
import p from 'picocolors'; | ||
@@ -10,2 +10,22 @@ declare function stringify(object: unknown, maxDepth?: number, { maxLength, ...options }?: PrettyFormatOptions & { | ||
export { stringify }; | ||
declare function getSafeTimers(): { | ||
setTimeout: any; | ||
setInterval: any; | ||
clearInterval: any; | ||
clearTimeout: any; | ||
}; | ||
declare function setSafeTimers(): void; | ||
declare function shuffle<T>(array: T[], seed?: number): T[]; | ||
declare function format(...args: any[]): string; | ||
declare function inspect(obj: unknown): string; | ||
declare function objDisplay(obj: unknown): string; | ||
declare const SAFE_TIMERS_SYMBOL: unique symbol; | ||
declare const SAFE_COLORS_SYMBOL: unique symbol; | ||
declare function getColors(): typeof p; | ||
declare function setColors(colors: typeof p): void; | ||
export { SAFE_COLORS_SYMBOL, SAFE_TIMERS_SYMBOL, format, getColors, getSafeTimers, inspect, objDisplay, setColors, setSafeTimers, shuffle, stringify }; |
@@ -1,7 +0,5 @@ | ||
export { formatLine, unifiedDiff } from './diff.js'; | ||
export { assertTypes, isObject } from './helpers.js'; | ||
import { format, plugins } from 'pretty-format'; | ||
import 'picocolors'; | ||
import 'diff'; | ||
import 'cli-truncate'; | ||
export { assertTypes, clone, createDefer, deepClone, getOwnProperties, getType, isObject, noop, objectAttr, slash, toArray } from './helpers.js'; | ||
import { format as format$1, plugins } from 'pretty-format'; | ||
import util from 'util'; | ||
import loupeImport from 'loupe'; | ||
@@ -28,3 +26,3 @@ const { | ||
try { | ||
result = format(object, { | ||
result = format$1(object, { | ||
maxDepth, | ||
@@ -36,3 +34,3 @@ escapeString: false, | ||
} catch { | ||
result = format(object, { | ||
result = format$1(object, { | ||
callToJSON: false, | ||
@@ -48,2 +46,122 @@ maxDepth, | ||
export { stringify }; | ||
const SAFE_TIMERS_SYMBOL = Symbol("vitest:SAFE_TIMERS"); | ||
const SAFE_COLORS_SYMBOL = Symbol("vitest:SAFE_COLORS"); | ||
function getSafeTimers() { | ||
const { | ||
setTimeout: safeSetTimeout, | ||
setInterval: safeSetInterval, | ||
clearInterval: safeClearInterval, | ||
clearTimeout: safeClearTimeout | ||
} = globalThis[SAFE_TIMERS_SYMBOL] || globalThis; | ||
return { | ||
setTimeout: safeSetTimeout, | ||
setInterval: safeSetInterval, | ||
clearInterval: safeClearInterval, | ||
clearTimeout: safeClearTimeout | ||
}; | ||
} | ||
function setSafeTimers() { | ||
const { | ||
setTimeout: safeSetTimeout, | ||
setInterval: safeSetInterval, | ||
clearInterval: safeClearInterval, | ||
clearTimeout: safeClearTimeout | ||
} = globalThis; | ||
const timers = { | ||
setTimeout: safeSetTimeout, | ||
setInterval: safeSetInterval, | ||
clearInterval: safeClearInterval, | ||
clearTimeout: safeClearTimeout | ||
}; | ||
globalThis[SAFE_TIMERS_SYMBOL] = timers; | ||
} | ||
const RealDate = Date; | ||
function random(seed) { | ||
const x = Math.sin(seed++) * 1e4; | ||
return x - Math.floor(x); | ||
} | ||
function shuffle(array, seed = RealDate.now()) { | ||
let length = array.length; | ||
while (length) { | ||
const index = Math.floor(random(seed) * length--); | ||
const previous = array[length]; | ||
array[length] = array[index]; | ||
array[index] = previous; | ||
++seed; | ||
} | ||
return array; | ||
} | ||
const loupe = typeof loupeImport.default === "function" ? loupeImport.default : loupeImport; | ||
function format(...args) { | ||
return util.format(...args); | ||
} | ||
function inspect(obj) { | ||
return loupe(obj, { | ||
depth: 2, | ||
truncate: 40 | ||
}); | ||
} | ||
function objDisplay(obj) { | ||
const truncateThreshold = 40; | ||
const str = inspect(obj); | ||
const type = Object.prototype.toString.call(obj); | ||
if (str.length >= truncateThreshold) { | ||
if (type === "[object Function]") { | ||
const fn = obj; | ||
return !fn.name || fn.name === "" ? "[Function]" : `[Function: ${fn.name}]`; | ||
} else if (type === "[object Array]") { | ||
return `[ Array(${obj.length}) ]`; | ||
} else if (type === "[object Object]") { | ||
const keys = Object.keys(obj); | ||
const kstr = keys.length > 2 ? `${keys.splice(0, 2).join(", ")}, ...` : keys.join(", "); | ||
return `{ Object (${kstr}) }`; | ||
} else { | ||
return str; | ||
} | ||
} | ||
return str; | ||
} | ||
const colors = [ | ||
"reset", | ||
"bold", | ||
"dim", | ||
"italic", | ||
"underline", | ||
"inverse", | ||
"hidden", | ||
"strikethrough", | ||
"black", | ||
"red", | ||
"green", | ||
"yellow", | ||
"blue", | ||
"magenta", | ||
"cyan", | ||
"white", | ||
"gray", | ||
"bgBlack", | ||
"bgRed", | ||
"bgGreen", | ||
"bgYellow", | ||
"bgBlue", | ||
"bgMagenta", | ||
"bgCyan", | ||
"bgWhite" | ||
]; | ||
const formatter = (str) => String(str); | ||
const defaultColors = colors.reduce((acc, key) => { | ||
acc[key] = formatter; | ||
return acc; | ||
}, { isColorSupported: false }); | ||
function getColors() { | ||
return globalThis[SAFE_COLORS_SYMBOL] || defaultColors; | ||
} | ||
function setColors(colors2) { | ||
globalThis[SAFE_COLORS_SYMBOL] = colors2; | ||
} | ||
export { SAFE_COLORS_SYMBOL, SAFE_TIMERS_SYMBOL, format, getColors, getSafeTimers, inspect, objDisplay, setColors, setSafeTimers, shuffle, stringify }; |
{ | ||
"name": "@vitest/utils", | ||
"type": "module", | ||
"version": "0.27.3", | ||
"version": "0.28.0", | ||
"description": "Shared Vitest utility functions", | ||
@@ -37,2 +37,3 @@ "license": "MIT", | ||
"diff": "^5.1.0", | ||
"loupe": "^2.3.6", | ||
"picocolors": "^1.0.0", | ||
@@ -39,0 +40,0 @@ "pretty-format": "^27.5.1" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15576
416
5
+ Addedloupe@^2.3.6
+ Addedget-func-name@2.0.2(transitive)
+ Addedloupe@2.3.7(transitive)