UNPKG

cidr-block

Version:

ipv4 and ipv6 address and cidr range utilities

138 lines (137 loc) 3.9 kB
import { Ipv4Literal, Ipv4Representable } from './types'; /** * Representation of an IPv4 address. Provides various utility methods like equality * checking. * * @remarks * Avoid direct instantiation; use {@link ipv4.address} instead. */ export declare class Ipv4Address { private _address; constructor(address: Ipv4Literal); /** * The address as a number */ get address(): number; /** * Returns the string representation of the address * * @example * ```typescript * import { ipv4 as ip } from 'cidr-block' * * ip.address(255) // ==> '0.0.0.255' * ip.address(0b11111111_00000000_11111111_00000000) // ==> '255.0.255.0' * ```` * * @public * @returns the IPv4 address as a string */ toString(): string; /** * Compares if two IPv4 addresses are the same. * * @example * ```typescript * import { ipv4 as ip } from 'cidr-block' * * function isLoopback(address: Ipv4Representable) { * return ip.address(address).equals('127.0.0.1') * } * ``` * * @public * @param otherIpAddress the other IPv4 address to compare * @returns if the other IP address is the same */ equals(otherIpAddress: Ipv4Representable): boolean; /** * Calculates the next logical IPv4 address. * * @example * ```typescript * import { ipv4 as ip } from 'cidr-block' * * const myIp = ip.address('52.89.32.255') * myIp.nextIp() // ==> '52.89.33.0' * ``` * * @public * @returns the next consecutive IPv4 address */ nextIp(): Ipv4Address; /** * @example * ```typescript * import { ipv4 as ip } from 'cidr-block' * * const myIp = ip.address('52.89.32.19') * myIp.previousIp() // ==> '52.89.32.18' * ``` * * @public * @returns the preceding IPv4 address */ previousIp(): Ipv4Address; } /** * Convenience function for creating an IPv4 address instance. * * @remarks * In general, you should use this function instead of instantiating an Ipv4Address * object directly. * * @example * * ```typescript * import { ipv4 as ip } from 'cidr-block' * * const localhost = ip.address('127.0.0.1') * ``` * * @see {@link Ipv4Address} * * @public * @param ip string representation of the IPv4 address * @returns an instance of Ipv4Address */ export declare function address(ip: Ipv4Literal): Ipv4Address; /** * Converts the string representation of an IPv4 address to a number. * * @example * ```typescript * import * as cidr from 'cidr-block' * * cidr.ipv4.stringToNum('255.255.255.255') === 4_294_967_295 // ==> true * cidr.ipv4.stringToNum('0.0.0.255') === 255 // ==> true * ``` * * @see This method is the inverse of {@link ipv4.numToString} * @throws {@link InvalidIpAddressError} * * @public * @param address IPv4 address represented as a string * @returns numerical number representation of the address */ export declare function stringToNum(address: string): number; /** * Converts the numerical representation of an IPv4 address to its string representation. * * @example * ```typescript * import * as cidr from 'cidr-block' * * cidr.ipv4.numToString(0) === '0.0.0.0' // ==> true * cidr.ipv4.numToString(65_280) === '0.0.255.0' // ==> true * cidr.ipv4.numToString(4_294_967_295) === '255.255.255.255' // ==> true * ``` * * @see This method is the inverse of {@link ipv4.stringToNum} * @throws {@link InvalidIpAddressError} * * @public * @param ip IPv4 address as a number * @returns string representation of the address */ export declare function numToString(ip: number): string;