@@ -49,6 +49,12 @@ export class IPv4 {
4949 *
5050 * @param string The input to parse as an IPv4 address.
5151 * @returns An IPv4 instance if parsing succeeds, otherwise null.
52+ * @example
53+ * const ip = IPv4.parse("192.168.1.1");
54+ * // ip is an IPv4 instance
55+ * @example
56+ * const invalid = IPv4.parse("not-an-ip");
57+ * // invalid is null
5258 */
5359 static parse ( string : string ) : IPv4 | null {
5460 const int = parseIPv4 ( string , 0 ) ;
@@ -61,6 +67,12 @@ export class IPv4 {
6167 * @param a The first IPv4 address.
6268 * @param b The second IPv4 address.
6369 * @returns 0, -1, or 1 if `a` is equal to, less than or greater than `b`, respectively.
70+ * @example
71+ * const a = IPv4.parse("192.168.1.1");
72+ * const b = IPv4.parse("192.168.1.2");
73+ * IPv4.cmp(a, a); // 0
74+ * IPv4.cmp(a, b); // -1
75+ * IPv4.cmp(b, a); // 1
6476 */
6577 static cmp ( a : IPv4 , b : IPv4 ) : number {
6678 return Math . sign ( a . _u32 - b . _u32 ) ;
@@ -92,6 +104,9 @@ export class IPv4 {
92104 * Convert the IPv4 address to its string representation.
93105 *
94106 * @returns The IPv4 address as a string.
107+ * @example
108+ * const ip = IPv4.parse("192.168.1.1");
109+ * ip.toString(); // "192.168.1.1"
95110 */
96111 toString ( ) : string {
97112 const b = this . _u32 ;
@@ -103,6 +118,10 @@ export class IPv4 {
103118 *
104119 * @param bits The number of bits in the mask.
105120 * @returns An IPRange representing the CIDR block.
121+ * @example
122+ * const ip = IPv4.parse("192.168.1.100");
123+ * const range = ip.cidr(24);
124+ * // range covers 192.168.1.0 to 192.168.1.255
106125 */
107126 cidr ( bits : number ) : IPRange {
108127 if ( bits === 32 ) {
@@ -251,6 +270,12 @@ export class IPv6 {
251270 *
252271 * @param string The input to parse as an IPv6 address.
253272 * @returns An IPv6 instance if parsing succeeds, otherwise null.
273+ * @example
274+ * const ip = IPv6.parse("2001:db8::1");
275+ * // ip is an IPv6 instance
276+ * @example
277+ * const embedded = IPv6.parse("::ffff:192.0.2.1");
278+ * // embedded IPv4 address in IPv6
254279 */
255280 static parse ( string : string ) : IPv6 | null {
256281 const index = string . lastIndexOf ( ":" ) ;
@@ -282,6 +307,12 @@ export class IPv6 {
282307 * @param a The first IPv6 address.
283308 * @param b The second IPv6 address.
284309 * @returns 0, -1, or 1 if `a` is equal to, less than or greater than `b`, respectively.
310+ * @example
311+ * const a = IPv6.parse("2001:db8::1");
312+ * const b = IPv6.parse("2001:db8::2");
313+ * IPv6.cmp(a, a); // 0
314+ * IPv6.cmp(a, b); // -1
315+ * IPv6.cmp(b, a); // 1
285316 */
286317 static cmp ( a : IPv6 , b : IPv6 ) : number {
287318 const aw = a . _words ;
@@ -331,6 +362,9 @@ export class IPv6 {
331362 * Convert the IPv6 address to its string representation.
332363 *
333364 * @returns The IPv6 address as a string.
365+ * @example
366+ * const ip = IPv6.parse("2001:0db8:0000:0000:0000:0000:0000:0001");
367+ * ip.toString(); // "2001:db8::1"
334368 */
335369 toString ( ) : string {
336370 this . _string ??= formatIPv6 ( this . _words ) . toLowerCase ( ) ;
@@ -342,6 +376,10 @@ export class IPv6 {
342376 *
343377 * @param bits The number of bits in the mask.
344378 * @returns An IPRange representing the CIDR block.
379+ * @example
380+ * const ip = IPv6.parse("2001:db8::1");
381+ * const range = ip.cidr(64);
382+ * // range covers 2001:db8:: to 2001:db8::ffff:ffff:ffff:ffff
345383 */
346384 cidr ( bits : number ) : IPRange {
347385 const first = new IPv6 ( mask ( this . _words , bits , 16 , 0 ) ) ;
@@ -386,6 +424,10 @@ export const IP: {
386424 *
387425 * @param string The input to parse.
388426 * @returns Return an IP instance if parsing succeeds, otherwise null.
427+ * @example
428+ * const ipv4 = IP.parse("192.168.1.1");
429+ * const ipv6 = IP.parse("2001:db8::1");
430+ * const invalid = IP.parse("not-an-ip"); // null
389431 */
390432 parse ( string : string ) : IP | null ;
391433 /**
@@ -395,6 +437,11 @@ export const IP: {
395437 * @param a The first IP address.
396438 * @param b The second IP address.
397439 * @returns -1, 0, or 1 depending on the comparison result.
440+ * @example
441+ * const ipv4 = IP.parse("192.168.1.1");
442+ * const ipv6 = IP.parse("2001:db8::1");
443+ * IP.cmp(ipv4, ipv6); // -1 (IPv4 < IPv6)
444+ * IP.cmp(ipv4, ipv4); // 0 (equal)
398445 */
399446 cmp ( a : IP , b : IP ) : number ;
400447} = {
@@ -494,6 +541,10 @@ export class IPRange {
494541 *
495542 * @param string The input to parse.
496543 * @returns An IPRange instance if parsing succeeds, otherwise null.
544+ * @example
545+ * const cidr = IPRange.parse("192.168.1.0/24");
546+ * const range = IPRange.parse("192.168.1.1-192.168.1.10");
547+ * const single = IPRange.parse("192.168.1.1");
497548 */
498549 static parse ( string : string ) : IPRange | null {
499550 if ( string . includes ( "/" ) ) {
@@ -575,6 +626,12 @@ export class IPRange {
575626 * Yield all IP addresses in the range.
576627 *
577628 * @returns An iterable of IP addresses from first to last.
629+ * @example
630+ * const range = IPRange.parse("192.168.1.1-192.168.1.3");
631+ * for (const ip of range.ips()) {
632+ * console.log(ip.toString());
633+ * }
634+ * // Outputs: "192.168.1.1", "192.168.1.2", "192.168.1.3"
578635 */
579636 * ips ( ) : Iterable < IP > {
580637 let ip : IP | null = this . first ;
@@ -588,6 +645,12 @@ export class IPRange {
588645 * Convert the range to its string representation.
589646 *
590647 * @returns Return a minimal string representation of the range.
648+ * @example
649+ * const cidr = IPRange.parse("192.168.1.0/24");
650+ * cidr.toString(); // "192.168.1.0/24"
651+ * @example
652+ * const range = IPRange.parse("192.168.1.1-192.168.1.10");
653+ * range.toString(); // "192.168.1.1-192.168.1.10"
591654 */
592655 toString ( ) : string {
593656 if ( this . first === this . last ) {
0 commit comments