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 { ParseCIDR } from 'locutus/golang/net/ParseCIDR'.
Or with CommonJS: const { ParseCIDR } = require('locutus/golang/net/ParseCIDR')
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
ParseCIDR('192.168.0.1/24')
{ip: '192.168.0.1', maskBits: 24}
2
ParseCIDR('2001:db8::1/64')
{ip: '2001:db8::1', maskBits: 64}
3
ParseCIDR('192.168.0.1/99')
null
Notes
Parses CIDR notation and returns normalized IP plus prefix length.
Returns null when either the IP or mask width is invalid.
Dependencies
This function uses the following Locutus functions:
Here's what our current TypeScript equivalent to Go's net.ParseCIDR looks like.
import { ParseIP } from'./ParseIP.ts'
exporttypeParsedCIDR = { ip: string maskBits: number }
exportfunctionParseCIDR(value: string): ParsedCIDR | null { // discuss at: https://locutus.io/golang/net/ParseCIDR/ // parity verified: Go 1.23 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Parses CIDR notation and returns normalized IP plus prefix length. // note 2: Returns null when either the IP or mask width is invalid. // example 1: ParseCIDR('192.168.0.1/24') // returns 1: {ip: '192.168.0.1', maskBits: 24} // example 2: ParseCIDR('2001:db8::1/64') // returns 2: {ip: '2001:db8::1', maskBits: 64} // example 3: ParseCIDR('192.168.0.1/99') // returns 3: null
exportfunctionParseCIDR(value) { // discuss at: https://locutus.io/golang/net/ParseCIDR/ // parity verified: Go 1.23 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Parses CIDR notation and returns normalized IP plus prefix length. // note 2: Returns null when either the IP or mask width is invalid. // example 1: ParseCIDR('192.168.0.1/24') // returns 1: {ip: '192.168.0.1', maskBits: 24} // example 2: ParseCIDR('2001:db8::1/64') // returns 2: {ip: '2001:db8::1', maskBits: 64} // example 3: ParseCIDR('192.168.0.1/99') // returns 3: null
functionParseIP(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 }
// golang/net/ParseCIDR (target function module) typeParsedCIDR = { ip: string maskBits: number }
functionParseCIDR(value: string): ParsedCIDR | null { // discuss at: https://locutus.io/golang/net/ParseCIDR/ // parity verified: Go 1.23 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Parses CIDR notation and returns normalized IP plus prefix length. // note 2: Returns null when either the IP or mask width is invalid. // example 1: ParseCIDR('192.168.0.1/24') // returns 1: {ip: '192.168.0.1', maskBits: 24} // example 2: ParseCIDR('2001:db8::1/64') // returns 2: {ip: '2001:db8::1', maskBits: 64} // example 3: ParseCIDR('192.168.0.1/99') // 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 (!/^[0-9a-fA-F]{1,4}$/.test(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 (!/^[0-9a-fA-F]{1,4}$/.test(group)) { returnfalse } groupCount += 1 }
if (hasCompression) { return groupCount < 8 }
return groupCount === 8 }
// golang/net/ParseCIDR (target function module) functionParseCIDR(value) { // discuss at: https://locutus.io/golang/net/ParseCIDR/ // parity verified: Go 1.23 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Parses CIDR notation and returns normalized IP plus prefix length. // note 2: Returns null when either the IP or mask width is invalid. // example 1: ParseCIDR('192.168.0.1/24') // returns 1: {ip: '192.168.0.1', maskBits: 24} // example 2: ParseCIDR('2001:db8::1/64') // returns 2: {ip: '2001:db8::1', maskBits: 64} // example 3: ParseCIDR('192.168.0.1/99') // returns 3: null
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.