-
Notifications
You must be signed in to change notification settings - Fork 2
/
log-everything.ts
41 lines (30 loc) · 1 KB
/
log-everything.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
// Creates a Proxy around an object that logs any time an action involving it
// happens. This can be useful for testing ECMAScript internal methods and other
// making sure polyfills emulate the exact behavior of their standardized
// counterparts.
function toString(object: unknown) {
return String(object).slice(0, 15)
}
function createTrap<A extends [any, ...any[]], R>(
name: string,
fn: (...args: A) => R,
): (...args: A) => R {
return (...args: A): R => {
console.group(`Reflect.${name}()`)
const value = fn(...args)
console.log(args)
console.log(value)
console.groupEnd()
return createProxy(value)
}
}
const handler: Required<ProxyHandler<any>> = Object.create(null)
for (const key of Object.getOwnPropertyNames(Reflect)) {
;(handler as any)[key] = createTrap(key, (Reflect as any)[key])
}
export function createProxy<T>(value: T, hint = "value"): T {
if (typeof value == "object" || typeof value == "function") {
return new Proxy(value, handler)
}
return value
}