IPv4 address parsing and stringifying utilities.
This module provides functions to convert between IPv4 dotted decimal notation and number representation, enabling arithmetic operations on IP addresses.
Basic IPv4 operations
Basic IPv4 operations
import { assertEquals } from "@std/assert"; import { parseAddressv4, stringifyAddressv4 } from "@hertzg/ip/addressv4"; const { address } = parseAddressv4("192.168.1.1"); assertEquals(address, 3232235777); const next = address + 1; assertEquals(stringifyAddressv4(next), "192.168.1.2");
Zone IDs are carried, not applied
Zone IDs are carried, not applied
import { assertEquals } from "@std/assert"; import { parseAddressv4, stringifyAddressv4 } from "@hertzg/ip/addressv4"; const gateway = parseAddressv4("10.155.101.1%ether1"); assertEquals(gateway, { address: 177956097, zoneId: "ether1" }); assertEquals(stringifyAddressv4(gateway), "10.155.101.1%ether1"); assertEquals(stringifyAddressv4(gateway.address), "10.155.101.1");
Bitwise operations on IPv4 addresses
Bitwise operations on IPv4 addresses
Since IPv4 addresses are plain numbers, you can use standard JavaScript
bitwise operators directly instead of library functions. Use >>> 0 to
keep results as unsigned 32-bit integers.
import { assertEquals } from "@std/assert"; import { parseAddressv4, stringifyAddressv4 } from "@hertzg/ip/addressv4"; import { cidrv4Mask } from "@hertzg/ip/cidrv4"; const ip = parseAddressv4("192.168.1.100").address; const mask = cidrv4Mask(24); // Bitwise NOT (invert all bits) const inverted = (~ip >>> 0); assertEquals(stringifyAddressv4(inverted), "63.87.254.155"); // Bitwise AND (apply network mask to get network address) const network = ((ip & mask) >>> 0); assertEquals(stringifyAddressv4(network), "192.168.1.0"); // Bitwise OR (combine network with host bits for broadcast) const broadcast = ((network | (~mask >>> 0)) >>> 0); assertEquals(stringifyAddressv4(broadcast), "192.168.1.255"); // Direct comparison (no isEqual() needed) assertEquals(parseAddressv4("10.0.0.1").address === parseAddressv4("10.0.0.1").address, true); assertEquals(parseAddressv4("10.0.0.1").address === parseAddressv4("10.0.0.2").address, false);
Compares two IPv4 addresses for sorting, numerically ascending.
Parses an IPv4 address in dotted decimal notation, with an optional zone ID, to its numeric value.
Stringifies an IPv4 address to dotted decimal notation.
An IPv4 address as a 32-bit unsigned integer, 0 to 4294967295. The
primitive type is what carries the version: a number is IPv4, a
bigint is IPv6 (ADR 0001).
What parseAddressv4 returns and what stringifyAddressv4
accepts: the address, plus the zone ID if the notation had one. Read
.address for the bare Addressv4; the zone never touches the
value, and no operation in this package reads it.
-
address: Addressv4
The address as a 32-bit unsigned integer
-
zoneId: ZoneId
The zone ID after
%, verbatim, when the notation had one
The zone ID of an address, the interface tail after % in
fe80::1%eth0 (RFC 4007 section 11). Carried verbatim by the Parsed*
types; never percent-decoded, so %25eth0 is the zone 25eth0.
Usage
import * as mod from "addressv4.ts";