Accounted for compact endings in expandIpv6

When an address ended in '::' the function would return an invalid string, this commit fixes that issue.
This commit is contained in:
Michael Rowley 2021-12-30 14:33:43 +00:00
parent 444065c926
commit ea8acc6035

View file

@ -397,14 +397,9 @@ export function strToIpv6(ipStr, retArr=true) {
*/
export function expandIpv6(ipStr) {
const compactIndex = ipStr.search("::");
if (compactIndex === -1) {
// There were no occurances of '::'
// in ipStr.
return ipStr;
}
let expandedStr = ipStr.substring(0, compactIndex); // 1234:5678::..
const insertOffset = compactIndex == 0;
const ipEnd = ipStr.substring(compactIndex + 1);
const ipEnd = ipStr.substring(compactIndex + 1); // :7ABC:DEFG:...
const missingChars = 39 - (expandedStr.length + ipEnd.length);
for (let i = insertOffset; i < missingChars + insertOffset; i++) {
if (i % 5 == 0) {
@ -413,6 +408,10 @@ export function strToIpv6(ipStr, retArr=true) {
}
expandedStr += "0";
}
if (compactIndex === ipStr.length - 2) {
expandedStr = expandedStr + "0";
return expandedStr;
}
expandedStr += ipEnd;
return expandedStr;
}