parseIPv4

import { parseIPv4 } from "jsr:@ende/net/unstable-ip";

function ยท experimental

function parseIPv4(addr: string): Uint8Array | undefined

Parses a string as an IPv4 address.

The accepted syntax is the URL standard's valid IPv4-address string: four decimal octets separated by ., each written as the shortest possible string of ASCII digits. Hexadecimal, octal and fewer-than-four-part forms are rejected, as is surrounding whitespace.

addr: string

IPv4 address in a string format (e.g., "192.168.0.1").

Returns

The four address bytes, or undefined if the string is not a valid IPv4 address.

Example: Parse an IPv4 address

import { parseIPv4 } from "@ende/net/unstable-ip"
import { assertEquals } from "@ende/assert"

assertEquals(parseIPv4("192.168.0.1"), new Uint8Array([192, 168, 0, 1]))
assertEquals(parseIPv4("0x7f.0.0.1"), undefined)

Source