-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
159 lines (144 loc) · 5.01 KB
/
util.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { fromDec, toDec, fromHex, toHex, fromBase64, toBase64, fromAscii, toAscii } from './bytes.js'
export function getElement(selector) {
const elOrNil = document.querySelector(selector)
if (elOrNil === null) {
throw new Error(`No element found for selector ${selector}`)
}
return elOrNil
}
export function getRadioButtons(name: string): NodeListOf<HTMLInputElement> {
return document.querySelectorAll(`input[type="radio"][name="${name}"]`)
}
export function getRadioButtonValue(radioButtons: NodeListOf<HTMLInputElement>): string {
let val: string | undefined
for (const radioButton of radioButtons) {
if (radioButton.checked) {
val = radioButton.value
break
}
}
if (val === undefined) {
throw new Error('No radio button selected')
}
return val
}
export type DataEncoding = 'dec' | 'hex' | 'base64'
export type MaybeTextEncoding = DataEncoding | 'ascii'
export function getDataEncoding(
label: string, radioButtons: NodeListOf<HTMLInputElement>
): DataEncoding {
const val = getRadioButtonValue(radioButtons)
if (val === 'dec' || val === 'hex' || val === 'base64') {
return val
}
throw new Error(`Invalid ${label} encoding ${val}`)
}
export function getMaybeTextEncoding(
label: string, radioButtons: NodeListOf<HTMLInputElement>
): MaybeTextEncoding {
const val = getRadioButtonValue(radioButtons)
if (val === 'dec' || val === 'hex' || val === 'base64' || val === 'ascii') {
return val
}
throw new Error(`Invalid ${label} encoding ${val}`)
}
export async function getData(
encoding: DataEncoding, el: HTMLInputElement | HTMLTextAreaElement
): Promise<ArrayBuffer> {
switch (encoding) {
case 'dec':
return fromDec(el.value)
case 'hex':
return fromHex(el.value)
case 'base64':
return fromBase64(el.value)
default:
throw new Error(`Invalid encoding "${encoding}"`)
}
}
export async function getMaybeTextData(
encoding: MaybeTextEncoding, el: HTMLInputElement | HTMLTextAreaElement
): Promise<ArrayBuffer> {
switch (encoding) {
case 'dec':
return fromDec(el.value)
case 'hex':
return fromHex(el.value)
case 'base64':
return fromBase64(el.value)
case 'ascii':
return fromAscii(el.value)
default:
throw new Error(`Invalid encoding "${encoding}"`)
}
}
export async function writeData(
encoding: DataEncoding, el: HTMLInputElement | HTMLTextAreaElement, data: ArrayBuffer
) {
switch (encoding) {
case 'dec':
el.value = toDec(data)
break
case 'hex':
el.value = toHex(data)
break
case 'base64':
el.value = await toBase64(data)
break
default:
throw new Error(`Invalid encoding "${encoding}"`)
}
}
export async function writeMaybeTextData(
encoding: MaybeTextEncoding, el: HTMLInputElement | HTMLTextAreaElement, data: ArrayBuffer
): Promise<void> {
switch (encoding) {
case 'dec':
el.value = toDec(data)
break
case 'hex':
el.value = toHex(data)
break
case 'base64':
el.value = await toBase64(data)
break
case 'ascii':
el.value = toAscii(data)
break
default:
throw new Error(`Invalid encoding "${encoding}"`)
}
}
type DataGetter = () => Promise<ArrayBuffer>
type DataWriter = (data: ArrayBuffer) => Promise<void>
type EncodingGetter = () => MaybeTextEncoding
type WireUpReturnValue = [ HTMLTextAreaElement, DataGetter, DataWriter, EncodingGetter ]
type WireUpFnc = (label: string) => WireUpReturnValue
function wireUp(getEncodingFnc, getDataFnc, writeDataFnc, label: string): WireUpReturnValue {
const field: HTMLTextAreaElement = getElement(`textarea#${label}`)
const radioButtons = getRadioButtons(`${label}encoding`)
const getEncoding: () => DataEncoding = getEncodingFnc.bind(null, label, radioButtons)
let currentEncoding = getEncoding()
radioButtons.forEach(x => x.addEventListener('change', async () => {
const newEncoding = getEncoding()
if (newEncoding !== currentEncoding) {
try {
const newData = await getDataFnc(currentEncoding, field)
await writeDataFnc(newEncoding, field, newData)
} catch (e) {
console.log(e)
alert(e)
}
currentEncoding = newEncoding
}
}))
const getData = () => getDataFnc(getEncoding(), field)
const writeData = (data: ArrayBuffer) => writeDataFnc(getEncoding(), field, data)
return [ field, getData, writeData, getEncoding ]
}
export const wireUpMaybeTextField: WireUpFnc = wireUp.bind(
null, getMaybeTextEncoding, getMaybeTextData, writeMaybeTextData
)
export const wireUpBinaryDataField: WireUpFnc = wireUp.bind(
null, getDataEncoding, getData, writeData
)