@prexjs/ipaddress
v1.0.0
Published
Comprehensive IPv4 and IPv6 address parsing, validation, subnet matching, CIDR manipulation, and formatting utility.
Readme
@prexjs/ipaddress
Comprehensive IPv4 and IPv6 address parsing, validation, subnet matching, CIDR manipulation, and formatting utility.
@prexjs/ipaddress is a full-featured library for working with IPv4 and IPv6 addresses in JavaScript and TypeScript. It supports CIDR parsing, RFC 5952 canonical formatting, subnet range matching, IPv4-mapped IPv6 conversion, and zone index support.
Features
- Validate and parse IPv4 and IPv6 addresses and CIDR subnets.
- RFC 5952 compliant IPv6 formatting (
toRFC5952String). - IPv4-mapped IPv6 address detection and conversion.
- Test whether IP addresses belong to special ranges (private, loopback, linkLocal, carrierGradeNat, etc.).
- Calculate network addresses and broadcast addresses from CIDR ranges.
- Dual ES Module (
.mjs) and CommonJS (.cjs) support with TypeScript definitions. - Zero external runtime dependencies.
Installation
Install using your preferred package manager:
# npm
npm install @prexjs/ipaddress
# pnpm
pnpm add @prexjs/ipaddress
# Yarn
yarn add @prexjs/ipaddress
# Bun
bun add @prexjs/ipaddressQuick start
import ipaddress, { IPv4, IPv6, parse, isValid } from '@prexjs/ipaddress';
// Validation
console.log(isValid('192.168.1.1')); // true
console.log(isValid('2001:db8::1')); // true
// Parsing and inspecting
const ip = parse('192.168.1.1');
console.log(ip.kind()); // 'ipv4'
console.log(ip.range()); // 'private'
const [subnet, mask] = IPv4.parseCIDR('192.168.1.50/24');
console.log(ip.match(subnet, mask)); // trueBoth default and named exports are supported:
import ipaddress, {
IPv4,
IPv6,
Address4,
Address6,
isValid,
parse,
parseCIDR,
process,
subnetMatch,
type Subnet,
type RangeList,
} from '@prexjs/ipaddress';Examples
Checking special ranges
import { parse } from '@prexjs/ipaddress';
parse('127.0.0.1').range(); // 'loopback'
parse('10.0.0.1').range(); // 'private'
parse('169.254.0.1').range(); // 'linkLocal'
parse('::1').range(); // 'loopback'
parse('fe80::1').range(); // 'linkLocal'IPv4-mapped IPv6 addresses
import { IPv6 } from '@prexjs/ipaddress';
const ipv6 = IPv6.parse('::ffff:192.168.1.1');
console.log(ipv6.isIPv4MappedAddress()); // true
const ipv4 = ipv6.toIPv4Address();
console.log(ipv4.toString()); // '192.168.1.1'Compatibility
@prexjs/ipaddress is designed for JavaScript and TypeScript projects running in Node.js, Deno, Bun, or browsers without external dependencies.
License
This project is licensed under the MIT License. See LICENSE for the full license text.
Related projects
- prex.js — prex.js monorepo and ecosystem.
