Rewrote expandIpv6()

This version of the function pads the hex-blocks of IPv6 addresses ("ff0:..." -> "0ff0:...") whereas the original did not.
This commit is contained in:
Michael Rowley 2021-12-30 15:50:41 +00:00
parent 05f51b30d0
commit 680650f86c

View file

@ -396,24 +396,36 @@ export function strToIpv6(ipStr, retArr=true) {
* expandIpv6("5555:126f::0001"); * expandIpv6("5555:126f::0001");
*/ */
export function expandIpv6(ipStr) { export function expandIpv6(ipStr) {
const compactIndex = ipStr.search("::"); const padHex = function(ipStr){
let expandedStr = ipStr.substring(0, compactIndex); // 1234:5678::.. if (ipStr.length === 39) {
const insertOffset = compactIndex === 0; // 0 / 1 return ipStr;
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) {
expandedStr += ":";
continue;
} }
expandedStr += "0"; const blockArray = ipStr.split(':');
let reconstructed = "";
blockArray.forEach((a) => {
for (let i = a.length; i < 4; i++) {
reconstructed += "0";
}
reconstructed += a + ":";
});
return reconstructed.substring(0, reconstructed.length - 1);
};
ipStr = padHex(ipStr);
const doubleColonOffset = ipStr.search("::");
if (doubleColonOffset === -1) {
return ipStr;
} }
if (compactIndex === ipStr.length - 2) { const totalBlocks = ipStr.match(/:/g).length - (ipStr.startsWith(":") || ipStr.endsWith(":")),
expandedStr = expandedStr + "0"; reqBlocks = 8 - totalBlocks;
return expandedStr; let expandedBlocks = (ipStr.startsWith(":") ? "" : ":") + "0000";
for (let i = 1; i < reqBlocks; i++) {
expandedBlocks += ":0000";
} }
expandedStr += ipEnd; ipStr = ipStr.substring(0, doubleColonOffset) + expandedBlocks + ":" + ipStr.substring(doubleColonOffset + 2);
return expandedStr; if (ipStr.endsWith(":")) {
ipStr = ipStr.substring(0, ipStr.length - 1);
}
return padHex(ipStr);
} }
/** /**