chore: remove some code duplication

This commit is contained in:
fryorcraken.eth 2023-03-10 13:52:44 +11:00
parent a2966c2e9b
commit 5a8de54d50
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
1 changed files with 54 additions and 66 deletions

View File

@ -46,105 +46,51 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
}
get ip(): string | undefined {
const raw = this.get("ip");
if (raw) {
return convertToString("ip4", raw) as string;
} else {
return undefined;
}
return getStringValue(this, "ip", "ip4");
}
set ip(ip: string | undefined) {
if (ip) {
this.set("ip", convertToBytes("ip4", ip));
} else {
this.delete("ip");
}
setStringValue(this, "ip", "ip4", ip);
}
get tcp(): number | undefined {
const raw = this.get("tcp");
if (raw) {
return Number(convertToString("tcp", raw));
} else {
return undefined;
}
return getNumberAsStringValue(this, "tcp", "tcp");
}
set tcp(port: number | undefined) {
if (port === undefined) {
this.delete("tcp");
} else {
this.set("tcp", convertToBytes("tcp", port.toString(10)));
}
setNumberAsStringValue(this, "tcp", "tcp", port);
}
get udp(): number | undefined {
const raw = this.get("udp");
if (raw) {
return Number(convertToString("udp", raw));
} else {
return undefined;
}
return getNumberAsStringValue(this, "udp", "udp");
}
set udp(port: number | undefined) {
if (port === undefined) {
this.delete("udp");
} else {
this.set("udp", convertToBytes("udp", port.toString(10)));
}
setNumberAsStringValue(this, "udp", "udp", port);
}
get ip6(): string | undefined {
const raw = this.get("ip6");
if (raw) {
return convertToString("ip6", raw) as string;
} else {
return undefined;
}
return getStringValue(this, "ip6", "ip6");
}
set ip6(ip: string | undefined) {
if (ip) {
this.set("ip6", convertToBytes("ip6", ip));
} else {
this.delete("ip6");
}
setStringValue(this, "ip6", "ip6", ip);
}
get tcp6(): number | undefined {
const raw = this.get("tcp6");
if (raw) {
return Number(convertToString("tcp", raw));
} else {
return undefined;
}
return getNumberAsStringValue(this, "tcp6", "tcp");
}
set tcp6(port: number | undefined) {
if (port === undefined) {
this.delete("tcp6");
} else {
this.set("tcp6", convertToBytes("tcp", port.toString(10)));
}
setNumberAsStringValue(this, "tcp6", "tcp", port);
}
get udp6(): number | undefined {
const raw = this.get("udp6");
if (raw) {
return Number(convertToString("udp", raw));
} else {
return undefined;
}
return getNumberAsStringValue(this, "udp6", "udp");
}
set udp6(port: number | undefined) {
if (port === undefined) {
this.delete("udp6");
} else {
this.set("udp6", convertToBytes("udp", port.toString(10)));
}
setNumberAsStringValue(this, "udp6", "udp", port);
}
/**
@ -210,3 +156,45 @@ export class RawEnr extends Map<ENRKey, ENRValue> {
}
}
}
function getStringValue(
map: Map<ENRKey, ENRValue>,
key: ENRKey,
proto: string
): string | undefined {
const raw = map.get(key);
if (!raw) return;
return convertToString(proto, raw);
}
function getNumberAsStringValue(
map: Map<ENRKey, ENRValue>,
key: ENRKey,
proto: string
): number | undefined {
const raw = map.get(key);
if (!raw) return;
return Number(convertToString(proto, raw));
}
function setStringValue(
map: Map<ENRKey, ENRValue>,
key: ENRKey,
proto: string,
value: string | undefined
): void {
if (value !== undefined) {
map.set(key, convertToBytes(proto, value));
} else {
map.delete(key);
}
}
function setNumberAsStringValue(
map: Map<ENRKey, ENRValue>,
key: ENRKey,
proto: string,
value: number | undefined
): void {
setStringValue(map, key, proto, value?.toString(10));
}