/
base.ts
145 lines (120 loc) · 4.37 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import type { Arrayable, Nullable, ResolvedConfig, VitestEnvironment } from '../types'
export { notNullish, getCallLastIndex } from '@vitest/utils'
function isFinalObj(obj: any) {
return obj === Object.prototype || obj === Function.prototype || obj === RegExp.prototype
}
function collectOwnProperties(obj: any, collector: Set<string | symbol> | ((key: string | symbol) => void)) {
const collect = typeof collector === 'function' ? collector : (key: string | symbol) => collector.add(key)
Object.getOwnPropertyNames(obj).forEach(collect)
Object.getOwnPropertySymbols(obj).forEach(collect)
}
export function groupBy<T, K extends string | number | symbol>(collection: T[], iteratee: (item: T) => K) {
return collection.reduce((acc, item) => {
const key = iteratee(item)
acc[key] ||= []
acc[key].push(item)
return acc
}, {} as Record<K, T[]>)
}
export function isPrimitive(value: unknown) {
return value === null || (typeof value !== 'function' && typeof value !== 'object')
}
export function getAllMockableProperties(obj: any, isModule: boolean) {
const allProps = new Map<string | symbol, { key: string | symbol; descriptor: PropertyDescriptor }>()
let curr = obj
do {
// we don't need properties from these
if (isFinalObj(curr))
break
collectOwnProperties(curr, (key) => {
const descriptor = Object.getOwnPropertyDescriptor(curr, key)
if (descriptor)
allProps.set(key, { key, descriptor })
})
// eslint-disable-next-line no-cond-assign
} while (curr = Object.getPrototypeOf(curr))
// default is not specified in ownKeys, if module is interoped
if (isModule && !allProps.has('default') && 'default' in obj) {
const descriptor = Object.getOwnPropertyDescriptor(obj, 'default')
if (descriptor)
allProps.set('default', { key: 'default', descriptor })
}
return Array.from(allProps.values())
}
export function slash(str: string) {
return str.replace(/\\/g, '/')
}
export function noop() { }
export function getType(value: unknown): string {
return Object.prototype.toString.apply(value).slice(8, -1)
}
/**
* Convert `Arrayable<T>` to `Array<T>`
*
* @category Array
*/
export function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
if (array === null || array === undefined)
array = []
if (Array.isArray(array))
return array
return [array]
}
export function toString(v: any) {
return Object.prototype.toString.call(v)
}
export function isPlainObject(val: any): val is object {
return toString(val) === '[object Object]' && (!val.constructor || val.constructor.name === 'Object')
}
export function isObject(item: unknown): boolean {
return item != null && typeof item === 'object' && !Array.isArray(item)
}
/**
* Deep merge :P
*
* Will merge objects only if they are plain
*
* Do not merge types - it is very expensive and usually it's better to case a type here
*/
export function deepMerge<T extends object = object>(target: T, ...sources: any[]): T {
if (!sources.length)
return target as any
const source = sources.shift()
if (source === undefined)
return target as any
if (isMergeableObject(target) && isMergeableObject(source)) {
(Object.keys(source) as (keyof T)[]).forEach((key) => {
if (isMergeableObject(source[key])) {
if (!target[key])
target[key] = {} as any
deepMerge(target[key] as any, source[key] as any)
}
else {
target[key] = source[key] as any
}
})
}
return deepMerge(target, ...sources)
}
function isMergeableObject(item: any): item is Object {
return isPlainObject(item) && !Array.isArray(item)
}
export function stdout(): NodeJS.WriteStream {
// @ts-expect-error Node.js maps process.stdout to console._stdout
// eslint-disable-next-line no-console
return console._stdout || process.stdout
}
export function getEnvironmentTransformMode(config: ResolvedConfig, environment: VitestEnvironment) {
if (!config.deps?.experimentalOptimizer?.ssr?.enabled && !config.deps?.experimentalOptimizer?.web?.enabled)
return undefined
return (environment === 'happy-dom' || environment === 'jsdom') ? 'web' : 'ssr'
}
// AggregateError is supported in Node.js 15.0.0+
class AggregateErrorPonyfill extends Error {
errors: unknown[]
constructor(errors: Iterable<unknown>, message = '') {
super(message)
this.errors = [...errors]
}
}
export { AggregateErrorPonyfill as AggregateError }