Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { ParseIP } from 'locutus/golang/net/ParseIP'.
Or with CommonJS: const { ParseIP } = require('locutus/golang/net/ParseIP')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
ParseIP('127.0.0.1')
'127.0.0.1'
2
ParseIP('2001:DB8::1')
'2001:db8::1'
3
ParseIP('999.1.2.3')
null
Notes
Parses IPv4/IPv6 textual addresses and returns a normalized string or null when invalid.
IPv6 output is normalized to lowercase but not fully canonicalized.
Here's what our current TypeScript equivalent to Go's net.ParseIP looks like.
typeIPv4Bytes = [number, number, number, number]
exportfunctionParseIP(value: string): string | null { // discuss at: https://locutus.io/golang/net/ParseIP/ // parity verified: Go 1.23 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Parses IPv4/IPv6 textual addresses and returns a normalized string or null when invalid. // note 2: IPv6 output is normalized to lowercase but not fully canonicalized. // example 1: ParseIP('127.0.0.1') // returns 1: '127.0.0.1' // example 2: ParseIP('2001:DB8::1') // returns 2: '2001:db8::1' // example 3: ParseIP('999.1.2.3') // returns 3: null
if (isValidIPv6(input)) { return input.toLowerCase() }
returnnull }
functionparseIPv4(value: string): IPv4Bytes | null { const parts = value.split('.') if (parts.length !== 4) { returnnull }
constbytes: number[] = [] for (const part of parts) { if (!/^\d+$/.test(part)) { returnnull } if (part.length > 1 && part.startsWith('0')) { returnnull } const n = Number(part) if (!Number.isInteger(n) || n < 0 || n > 255) { returnnull } bytes.push(n) }
const hasCompression = doubleColonParts.length === 2 const left = doubleColonParts[0] ?? '' const right = doubleColonParts[1] ?? ''
const leftGroups = left === '' ? [] : left.split(':') const rightGroups = right === '' ? [] : right.split(':')
if (leftGroups.some((group) => group === '') || rightGroups.some((group) => group === '')) { returnfalse }
let groupCount = 0
for (let i = 0; i < leftGroups.length; i++) { const group = leftGroups[i] ?? '' if (group.includes('.')) { if (i !== leftGroups.length - 1 || rightGroups.length > 0) { returnfalse } if (!parseIPv4(group)) { returnfalse } groupCount += 2 continue }
if (!isValidHextet(group)) { returnfalse } groupCount += 1 }
for (let i = 0; i < rightGroups.length; i++) { const group = rightGroups[i] ?? '' if (group.includes('.')) { if (i !== rightGroups.length - 1) { returnfalse } if (!parseIPv4(group)) { returnfalse } groupCount += 2 continue }
if (!isValidHextet(group)) { returnfalse } groupCount += 1 }
exportfunctionParseIP(value) { // discuss at: https://locutus.io/golang/net/ParseIP/ // parity verified: Go 1.23 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Parses IPv4/IPv6 textual addresses and returns a normalized string or null when invalid. // note 2: IPv6 output is normalized to lowercase but not fully canonicalized. // example 1: ParseIP('127.0.0.1') // returns 1: '127.0.0.1' // example 2: ParseIP('2001:DB8::1') // returns 2: '2001:db8::1' // example 3: ParseIP('999.1.2.3') // returns 3: null
if (isValidIPv6(input)) { return input.toLowerCase() }
returnnull }
functionparseIPv4(value) { const parts = value.split('.') if (parts.length !== 4) { returnnull }
const bytes = [] for (const part of parts) { if (!/^\d+$/.test(part)) { returnnull } if (part.length > 1 && part.startsWith('0')) { returnnull } const n = Number(part) if (!Number.isInteger(n) || n < 0 || n > 255) { returnnull } bytes.push(n) }
const hasCompression = doubleColonParts.length === 2 const left = doubleColonParts[0] ?? '' const right = doubleColonParts[1] ?? ''
const leftGroups = left === '' ? [] : left.split(':') const rightGroups = right === '' ? [] : right.split(':')
if (leftGroups.some((group) => group === '') || rightGroups.some((group) => group === '')) { returnfalse }
let groupCount = 0
for (let i = 0; i < leftGroups.length; i++) { const group = leftGroups[i] ?? '' if (group.includes('.')) { if (i !== leftGroups.length - 1 || rightGroups.length > 0) { returnfalse } if (!parseIPv4(group)) { returnfalse } groupCount += 2 continue }
if (!isValidHextet(group)) { returnfalse } groupCount += 1 }
for (let i = 0; i < rightGroups.length; i++) { const group = rightGroups[i] ?? '' if (group.includes('.')) { if (i !== rightGroups.length - 1) { returnfalse } if (!parseIPv4(group)) { returnfalse } groupCount += 2 continue }
if (!isValidHextet(group)) { returnfalse } groupCount += 1 }
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
Click "New file" in the appropriate folder
on GitHub.
This will fork the project to your account, directly add the file to it, and send a
Pull Request to us.
We will then review it. If it's useful to the project and in line with our
contributing guidelines
your work will become part of Locutus and you'll be automatically credited
in the authors
section accordingly.