function parseAddressv4
parseAddressv4(address: string): ParsedAddressv4

Parses an IPv4 address in dotted decimal notation, with an optional zone ID, to its numeric value.

The notation is address [ "%" zoneId ] (ADR 0003). An octet is decimal digits and nothing else: no leading zeros (except "0" itself), no surrounding or embedded whitespace, no sign, no radix prefix, and no trailing text. The zone ID, when present, is carried verbatim: it may not contain %, / or whitespace, is never percent-decoded, and never touches the numeric value. RouterOS writes gateway=10.155.101.1%ether1, so IPv4 zones are accepted on the same terms as IPv6 ones.

A prefix is not accepted; that is parseCidrv4's slot. An IPv4-mapped IPv6 address such as ::ffff:1.2.3.4 is IPv6 notation and is rejected here; parseAddress unmaps it.

Examples

Basic parsing

import { assertEquals } from "@std/assert";
import { parseAddressv4 } from "@hertzg/ip/addressv4";

assertEquals(parseAddressv4("192.168.1.1"), { address: 3232235777 });
assertEquals(parseAddressv4("10.0.0.1"), { address: 167772161 });
assertEquals(parseAddressv4("0.0.0.0"), { address: 0 });
assertEquals(parseAddressv4("255.255.255.255"), { address: 4294967295 });

A zone ID is carried verbatim

import { assertEquals } from "@std/assert";
import { parseAddressv4 } from "@hertzg/ip/addressv4";

assertEquals(parseAddressv4("192.168.1.1%ether1"), {
  address: 3232235777,
  zoneId: "ether1",
});
assertEquals(parseAddressv4("192.168.1.1%25"), {
  address: 3232235777,
  zoneId: "25",
});

Error handling

import { assertThrows } from "@std/assert";
import { parseAddressv4 } from "@hertzg/ip/addressv4";

assertThrows(() => parseAddressv4("192.168.1"), TypeError);
assertThrows(() => parseAddressv4("192.168.1.256"), RangeError);
assertThrows(() => parseAddressv4("192.168.01.1"), TypeError);
assertThrows(() => parseAddressv4(" 192.168.1.1"), TypeError);
assertThrows(() => parseAddressv4("192.168.1.1abc"), TypeError);
assertThrows(() => parseAddressv4("192.168.1.1/24"), TypeError);
assertThrows(() => parseAddressv4("192.168.1.1%"), TypeError);
assertThrows(() => parseAddressv4("192.168.1.1% eth0"), TypeError);
assertThrows(() => parseAddressv4("::ffff:192.168.1.1"), TypeError);

Parameters

address: string

The address string, dotted decimal with an optional %zoneId

Return Type

The address as a 32-bit unsigned integer, and the zone ID if there was one

Throws

TypeError

If the format is invalid -- wrong number of octets, a non-decimal octet, leading zeros, a sign, whitespace, trailing text, a prefix, an empty or malformed zone ID

RangeError

If an octet is a well-formed number greater than 255

Usage

import { parseAddressv4 } from "addressv4.ts";