aboutsummaryrefslogtreecommitdiff
path: root/packages/webapi/src/lib/utils.ts
blob: 0086f75b674265883a7492f3fea0bababcd69770 (plain) (blame)
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
import { performance } from 'node:perf_hooks'

/** Returns the milliseconds elapsed since January 1, 1970 00:00:00 UTC. */
export const __date_now = Date.now

/** Returns the function bound to the given object. */
export const __function_bind = Function.bind.bind(
	Function.call as unknown as any
) as <TArgs extends any[], TFunc extends (...args: TArgs) => any>(
	callback: TFunc,
	thisArg: unknown,
	...args: TArgs
) => TFunc

/** Returns the function called with the specified values. */
export const __function_call = Function.call.bind(
	Function.call as unknown as any
) as <TArgs extends any, TFunc extends (...args: TArgs[]) => any>(
	callback: TFunc,
	thisArg: unknown,
	...args: TArgs[]
) => ReturnType<TFunc>

/** Returns an object with the specified prototype. */
export const __object_create = Object.create as {
	<T extends any = any>(value: T): any extends T ? Record<any, any> : T
}

/** Returns whether an object has a property with the specified name. */
export const __object_hasOwnProperty = Function.call.bind(
	Object.prototype.hasOwnProperty
) as {
	<T1 extends object, T2>(object: T1, key: T2): T2 extends keyof T1
		? true
		: false
}

/** Returns a string representation of an object. */
export const __object_toString = Function.call.bind(
	Object.prototype.toString
) as { (value: any): string }

/** Returns whether the object prototype exists in another object. */
export const __object_isPrototypeOf = Function.call.bind(
	Object.prototype.isPrototypeOf
) as { <T1 extends object, T2>(p: T1, v: T2): T2 extends T1 ? true : false }

/** Current high resolution millisecond timestamp. */
export const __performance_now = performance.now as () => number

/** Returns the string escaped for use inside regular expressions. */
export const __string_escapeRegExp = (value: string) =>
	value.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&')

// @ts-ignore
export const INTERNALS = new WeakMap<unknown, any>()

export const internalsOf = <T extends object>(
	target: T | object,
	className: string,
	propName: string
): T => {
	const internals: T = INTERNALS.get(target)

	if (!internals)
		throw new TypeError(
			`${className}.${propName} can only be used on instances of ${className}`
		)

	return internals
}

export const allowStringTag = (value: any) =>
	(value.prototype[Symbol.toStringTag] = value.name)

/** Returns any kind of path as a posix path. */
export const pathToPosix = (pathname: any) =>
	String(pathname == null ? '' : pathname)
		.replace(
			// convert slashes
			/\\+/g,
			'/'
		)
		.replace(
			// prefix a slash to drive letters
			/^(?=[A-Za-z]:\/)/,
			'/'
		)
		.replace(
			// encode path characters
			/%/g,
			'%25'
		)
		.replace(/\n/g, '%0A')
		.replace(/\r/g, '%0D')
		.replace(/\t/g, '%09')