CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ip-address

A library for parsing IPv4 and IPv6 IP addresses in node and the browser.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

ipv4.mddocs/

IPv4 Address Operations

Complete IPv4 address parsing, validation, and manipulation functionality including subnet operations, format conversions, and range calculations.

Capabilities

Address4 Class

The main class for handling IPv4 addresses with comprehensive parsing and validation.

/**
 * Represents an IPv4 address with optional subnet mask
 * @param address - IPv4 address string (e.g., "192.168.1.1" or "192.168.1.1/24")
 */
class Address4 {
  constructor(address: string);
  
  // Properties
  address: string;              // Original address string
  addressMinusSuffix?: string;  // Address without subnet
  groups: number;               // Always 4 for IPv4
  parsedAddress: string[];      // Array of address octets
  parsedSubnet: string;         // Subnet portion as string
  subnet: string;               // Subnet string (default "/32")
  subnetMask: number;           // Subnet mask length (0-32)
  v4: boolean;                  // Always true for IPv4
}

Static Creation Methods

Create Address4 instances from various formats and representations.

/**
 * Validate an IPv4 address string without creating an instance
 * @param address - IPv4 address string to validate
 * @returns true if valid, false otherwise
 */
static isValid(address: string): boolean;

/**
 * Create Address4 from hexadecimal string
 * @param hex - Hex string (e.g., "c0a80101" or "c0:a8:01:01")
 * @returns Address4 instance
 */
static fromHex(hex: string): Address4;

/**
 * Create Address4 from integer representation
 * @param integer - 32-bit integer representation
 * @returns Address4 instance
 */
static fromInteger(integer: number): Address4;

/**
 * Create Address4 from reverse DNS (in-addr.arpa) format
 * @param arpaFormAddress - Reverse DNS string (e.g., "1.1.168.192.in-addr.arpa.")
 * @returns Address4 instance
 */
static fromArpa(arpaFormAddress: string): Address4;

/**
 * Create Address4 from BigInt representation
 * @param bigInt - BigInt representation of the address
 * @returns Address4 instance
 */
static fromBigInt(bigInt: bigint): Address4;

Usage Examples:

import { Address4 } from "ip-address";

// From string
const addr1 = new Address4("192.168.1.1/24");

// From hex
const addr2 = Address4.fromHex("c0a80101");
console.log(addr2.correctForm()); // "192.168.1.1"

// From integer
const addr3 = Address4.fromInteger(3232235777);
console.log(addr3.correctForm()); // "192.168.1.1"

// From reverse DNS
const addr4 = Address4.fromArpa("1.1.168.192.in-addr.arpa.");
console.log(addr4.correctForm()); // "192.168.1.1"

// Validation
console.log(Address4.isValid("192.168.1.1")); // true
console.log(Address4.isValid("256.1.1.1")); // false

Form and Validation Methods

Methods for address validation and canonical representation.

/**
 * Parse an IPv4 address string into components
 * @param address - IPv4 address string
 * @returns Array of address octets as strings
 */
parse(address: string): string[];

/**
 * Return the canonical form of the address
 * @returns Canonical IPv4 address string
 */
correctForm(): string;

/**
 * Check if the address is in correct/canonical form
 * @returns true if address is in correct form
 */
isCorrect(): boolean;

/**
 * Return reverse DNS (in-addr.arpa) form
 * @param options - Options for reverse form generation
 * @returns Reverse DNS string
 */
reverseForm(options?: ReverseFormOptions): string;

Usage Examples:

const addr = new Address4("192.168.001.001/24");

console.log(addr.correctForm()); // "192.168.1.1"
console.log(addr.isCorrect()); // false (has leading zeros)

const correctAddr = new Address4("192.168.1.1");
console.log(correctAddr.isCorrect()); // true

// Reverse DNS
console.log(addr.reverseForm()); // "1.1.168.192.in-addr.arpa."
console.log(addr.reverseForm({ omitSuffix: true })); // "1.1.168.192"

Conversion Methods

Convert IPv4 addresses to different representations and formats.

/**
 * Convert address to hexadecimal string
 * @returns Colon-separated hex string (e.g., "c0:a8:01:01")
 */
toHex(): string;

/**
 * Convert address to byte array
 * @returns Array of 4 integers (0-255)
 */
toArray(): number[];

/**
 * Convert address to IPv6 group format for embedding
 * @returns IPv6-compatible group string
 */
toGroup6(): string;

/**
 * Convert address to BigInt representation
 * @returns BigInt value of the address
 */
bigInt(): bigint;

/**
 * Convert address to zero-padded binary string
 * @returns 32-character binary string
 */
binaryZeroPad(): string;

Usage Examples:

const addr = new Address4("192.168.1.1");

console.log(addr.toHex()); // "c0:a8:01:01"
console.log(addr.toArray()); // [192, 168, 1, 1]
console.log(addr.toGroup6()); // "c0a8:0101"
console.log(addr.bigInt()); // 3232235777n
console.log(addr.binaryZeroPad()); // "11000000101010000000000100000001"

Subnet and Range Methods

Advanced subnet operations for network analysis and range calculations.

/**
 * Get first n bits of address as binary string (defaults to subnet mask)
 * @param mask - Number of bits to mask (optional)
 * @returns Binary string of masked bits
 */
mask(mask?: number): string;

/**
 * Get bits in specified range as binary string
 * @param start - Start bit position
 * @param end - End bit position
 * @returns Binary string of specified bits
 */
getBitsBase2(start: number, end: number): string;

/**
 * Get first address in subnet (Network Address)
 * @returns Address4 instance of network address
 */
startAddress(): Address4;

/**
 * Get first host address in subnet (after Network Address)
 * @returns Address4 instance of first host
 */
startAddressExclusive(): Address4;

/**
 * Get last address in subnet (Broadcast Address)
 * @returns Address4 instance of broadcast address
 */
endAddress(): Address4;

/**
 * Get last host address in subnet (before Broadcast Address)
 * @returns Address4 instance of last host
 */
endAddressExclusive(): Address4;

/**
 * Check if another address is within this address's subnet
 * @param address - Address4 instance to test
 * @returns true if address is in subnet
 */
isInSubnet(address: Address4): boolean;

Usage Examples:

const network = new Address4("192.168.1.0/24");

console.log(network.startAddress().correctForm()); // "192.168.1.0"
console.log(network.startAddressExclusive().correctForm()); // "192.168.1.1"
console.log(network.endAddress().correctForm()); // "192.168.1.255"
console.log(network.endAddressExclusive().correctForm()); // "192.168.1.254"

const host = new Address4("192.168.1.100");
console.log(network.isInSubnet(host)); // true

console.log(network.mask()); // First 24 bits as binary
console.log(network.getBitsBase2(0, 8)); // First octet as binary

Address Type Detection

Identify special IPv4 address types and properties.

/**
 * Check if address is a multicast address (224.0.0.0/4)
 * @returns true if multicast address
 */
isMulticast(): boolean;

Usage Examples:

const multicast = new Address4("224.1.1.1");
console.log(multicast.isMulticast()); // true

const unicast = new Address4("192.168.1.1");
console.log(unicast.isMulticast()); // false

HTML Display Methods

Methods for generating HTML representations with styling classes.

/**
 * Generate HTML representation with styling classes for IPv6 display
 * @returns HTML string with span elements and CSS classes
 */
groupForV6(): string;

Usage Examples:

const addr = new Address4("192.168.1.1");
console.log(addr.groupForV6());
// '<span class="hover-group group-v4 group-6">192.168</span>.<span class="hover-group group-v4 group-7">1.1</span>'

docs

address-types.md

format-conversion.md

index.md

ipv4.md

ipv6.md

special-analysis.md

subnet-operations.md

tile.json