MCPcopy Create free account
hub / github.com/Effect-TS/effect / formatIp

Function formatIp

packages/effect/src/net/NetAddress.ts:1361–1385  ·  view source on GitHub ↗
(self: IpAddress)

Source from the content-addressed store, hash-verified

1359 * @since 4.0.0
1360 */
1361export const formatIp = (self: IpAddress): string => {
1362 if (isIpv4Address(self)) return ipv4ToOctets(self).join(".")
1363 const segments = ipv6ToSegments(self)
1364 if (isIpv4Mapped(self)) {
1365 return `::ffff:${segments[6] >> 8}.${segments[6] & 0xff}.${segments[7] >> 8}.${segments[7] & 0xff}`
1366 }
1367 let bestStart = -1
1368 let bestLength = 0
1369 let start = -1
1370 for (let index = 0; index <= 8; index++) {
1371 if (index < 8 && segments[index] === 0) {
1372 if (start === -1) start = index
1373 } else if (start !== -1) {
1374 if (index - start > bestLength) {
1375 bestStart = start
1376 bestLength = index - start
1377 }
1378 start = -1
1379 }
1380 }
1381 if (bestLength < 2) return segments.map((segment) => segment.toString(16)).join(":")
1382 const head = segments.slice(0, bestStart).map((segment) => segment.toString(16)).join(":")
1383 const tail = segments.slice(bestStart + bestLength).map((segment) => segment.toString(16)).join(":")
1384 return `${head}::${tail}`
1385}
1386
1387/**
1388 * Returns `true` for the all-zero address of either IP version.

Callers 6

toStringFunction · 0.85
formatHostFunction · 0.85
formatNativeHostFunction · 0.85
formatMulticastInterfaceFunction · 0.85
formatUrlHostFunction · 0.85

Calls 8

isIpv4AddressFunction · 0.85
ipv4ToOctetsFunction · 0.85
ipv6ToSegmentsFunction · 0.85
isIpv4MappedFunction · 0.85
sliceMethod · 0.80
toStringMethod · 0.65
joinMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected