export function formatIpAddress(ipAddress: string | null | undefined): string | null { if (!ipAddress) { return null } if (!ipAddress.includes(':')) { return ipAddress } const groups = ipAddress.split(':').map((group) => group.replace(/^0+(?=\w)/, '') || '0') let bestStart = -1 let bestLength = 0 let runStart = -1 groups.forEach((group, index) => { if (group === '0') { if (runStart === -1) { runStart = index } const runLength = index - runStart + 1 if (runLength > bestLength) { bestStart = runStart bestLength = runLength } return } runStart = -1 }) if (bestLength < 2) { return groups.join(':') } const head = groups.slice(0, bestStart).join(':') const tail = groups.slice(bestStart + bestLength).join(':') return `${head}::${tail}` }