Repository navigation
Expand file tree
/
Copy pathpolyfill.ts
More file actions
53 lines (48 loc) · 1.44 KB
/
Copy pathpolyfill.ts
File metadata and controls
53 lines (48 loc) · 1.44 KB
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
export function addMissingFunctions() {
if (!Uint8Array.prototype.toHex) {
Uint8Array.prototype.toHex = function () {
return Array.from(this)
.map((b) => b.toString(16).padStart(2, '0'))
.join('')
}
}
if (!Uint8Array.fromHex) {
Uint8Array.fromHex = function (hex: string) {
const bytes = new Uint8Array(hex.length / 2)
for (let i = 0; i < hex.length; i += 2) {
bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16)
}
return bytes
}
}
if (!Uint8Array.fromBase64) {
Uint8Array.fromBase64 = function (str) {
// Strip ASCII whitespace (per spec)
const cleaned = str.replace(/[\t\n\f\r ]/g, '')
const binary = atob(cleaned)
const bytes = new Uint8Array(binary.length)
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i)
}
return bytes
}
}
// for forEach on Map.values(), etc
const IteratorPrototype = getIteratorPrototype()
if (!IteratorPrototype.forEach) {
IteratorPrototype.forEach = function forEach(
callbackFn: (value: unknown, index: number) => void
) {
if (typeof callbackFn !== 'function') {
throw new TypeError('callbackFn must be a function')
}
let index = 0
for (const value of this) {
callbackFn(value, index++)
}
}
}
}
function getIteratorPrototype() {
return Object.getPrototypeOf(Object.getPrototypeOf((function* () {})()))
}