Universal IP address classification.

This module provides a single classifyAddress function that accepts both IPv4 (number) and IPv6 (bigint) addresses and returns the appropriate classification with version information and parsed value.

For version-specific classifiers, see:

Examples

Classify any IP address

import { assertEquals } from "@std/assert";
import { classifyAddress } from "@hertzg/ip/classify";
import { parseAddressv4 } from "@hertzg/ip/addressv4";
import { parseAddressv6 } from "@hertzg/ip/addressv6";

// IPv4 from parsed value
const v4 = classifyAddress(parseAddressv4("192.168.1.1").address);
assertEquals(v4.kind, "ipv4");
assertEquals(v4.value, 3232235777);
assertEquals(v4.classification, "private");

// IPv6 from parsed value
const v6 = classifyAddress(parseAddressv6("::1").address);
assertEquals(v6.kind, "ipv6");
assertEquals(v6.value, 1n);
assertEquals(v6.classification, "loopback");

// From string directly
const str4 = classifyAddress("127.0.0.1");
assertEquals(str4.kind, "ipv4");
assertEquals(str4.classification, "loopback");

const str6 = classifyAddress("2001:db8::1");
assertEquals(str6.kind, "ipv6");
assertEquals(str6.classification, "documentation");

Functions

f
classifyAddress(address: Address | string): ClassifiedAddress
4 overloads

Classifies an IPv4 address into its well-known range.

Type Aliases

T
Address = Addressv4 | Addressv6

A plain IP address of either IP version.

T
Addressv4 = number

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).

T
Addressv6 = bigint

An IPv6 address as a 128-bit unsigned bigint, 0n to 2n ** 128n - 1n. The primitive type is what carries the version: a bigint is IPv6, a number is IPv4 (ADR 0001).

T
ClassifiedAddress = ClassifiedAddressv4 | ClassifiedAddressv6

Result of classifying an IP address with version information and parsed value.