ip-bigint
Advanced tools
Comparing version 7.0.0 to 7.0.1
37
index.js
export const max4 = 2n ** 32n - 1n; | ||
export const max6 = 2n ** 128n - 1n; | ||
const emptyPartsRe = /\b:?(?:0+:?)+/; | ||
const isIP = ip => ip.includes(":") ? 6 : ip.includes(".") ? 4 : 0; | ||
@@ -92,4 +91,5 @@ | ||
} | ||
ip = compressIPv6(ip.split(":")); | ||
} else { | ||
ip = parts.map(n => n.toString(16)).join(":"); | ||
ip = compressIPv6(parts.map(n => n.toString(16))); | ||
} | ||
@@ -101,3 +101,3 @@ | ||
return ip.replace(emptyPartsRe, "::"); | ||
return ip; | ||
} | ||
@@ -109,1 +109,32 @@ } | ||
} | ||
// take the longest or first sequence of "0" segments and replace it with "::" | ||
function compressIPv6(parts) { | ||
let longestSequence; | ||
let currentSequence; | ||
for (const [index, part] of parts.entries()) { | ||
if (part === "0") { | ||
if (!currentSequence) { | ||
currentSequence = new Set([index]); | ||
} else { | ||
currentSequence.add(index); | ||
} | ||
} else { | ||
if (currentSequence) { | ||
if (!longestSequence) { | ||
longestSequence = currentSequence; | ||
} else if (currentSequence.size > longestSequence.size) { | ||
longestSequence = currentSequence; | ||
} | ||
currentSequence = null; | ||
} | ||
} | ||
} | ||
if (!longestSequence && currentSequence) longestSequence = currentSequence; | ||
for (const index of longestSequence || []) { | ||
parts[index] = ":"; | ||
} | ||
return parts.filter(Boolean).join(":").replace(/:{2,}/, "::"); | ||
} |
{ | ||
"name": "ip-bigint", | ||
"version": "7.0.0", | ||
"version": "7.0.1", | ||
"description": "Convert IPv4 and IPv6 addresses to native BigInt and vice-versa", | ||
@@ -5,0 +5,0 @@ "author": "silverwind <me@silverwind.io>", |
5729
118