Improved ParseIPv6Address formatting

This commit is contained in:
Michael Rowley 2021-12-30 14:25:12 +00:00
parent 7af7b4df5e
commit 444065c926
2 changed files with 220 additions and 222 deletions

View file

@ -403,7 +403,7 @@ export function strToIpv6(ipStr, retArr=true) {
return ipStr; return ipStr;
} }
let expandedStr = ipStr.substring(0, compactIndex); // 1234:5678::.. let expandedStr = ipStr.substring(0, compactIndex); // 1234:5678::..
const insertOffset = compactIndex == 0 ? 1 : 0; const insertOffset = compactIndex == 0;
const ipEnd = ipStr.substring(compactIndex + 1); const ipEnd = ipStr.substring(compactIndex + 1);
const missingChars = 39 - (expandedStr.length + ipEnd.length); const missingChars = 39 - (expandedStr.length + ipEnd.length);
for (let i = insertOffset; i < missingChars + insertOffset; i++) { for (let i = insertOffset; i < missingChars + insertOffset; i++) {

View file

@ -38,8 +38,9 @@ class ParseIPv6Address extends Operation {
run(input, args) { run(input, args) {
let match, let match,
output = ""; output = "";
if (!(match = IPV6_REGEX.exec(input))) {
if ((match = IPV6_REGEX.exec(input))) { throw new OperationError("Invalid IPv6 address.");
}
const ipv6 = strToIpv6(match[1]), const ipv6 = strToIpv6(match[1]),
longhand = ipv6ToStr(ipv6), longhand = ipv6ToStr(ipv6),
shorthand = ipv6ToStr(ipv6, true); shorthand = ipv6ToStr(ipv6, true);
@ -265,9 +266,6 @@ class ParseIPv6Address extends Operation {
output += "\nInterface identifier: " + intIdent + output += "\nInterface identifier: " + intIdent +
"\nMAC address: " + mac; "\nMAC address: " + mac;
} }
} else {
throw new OperationError("Invalid IPv6 address");
}
return output; return output;
} }