Skip to content

Commit 4ac9117

Browse files
authored
Improved the wording of the documentation (#55)
2 parents 97a45ce + 50c4cb8 commit 4ac9117

File tree

5 files changed

+107
-66
lines changed

5 files changed

+107
-66
lines changed

src/IPAddress.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,27 @@
1717
import {IPv4, IPv6} from "./index.js";
1818

1919
/**
20-
* An IP address
20+
* An IP address.
2121
*/
2222
export abstract class IPAddress {
2323
/**
24-
* The integer representation of the IP address
24+
* The integer representation of the IP address.
2525
*/
2626
public readonly value: bigint;
2727

2828
/**
29-
* Create new IP address instance
29+
* Creates a new IP address instance.
30+
*
31+
* @param value The integer representation of the IP address.
32+
* @deprecated This class will be sealed in v2.0.0 and should not be extended by public API users.
3033
*/
3134
protected constructor(value: bigint) {
3235
this.value = value;
3336
}
3437

3538
/**
36-
* Create IP address from string
39+
* Creates an IP address from a string.
40+
*
3741
* @throws {@link !RangeError} If provided string is not a valid IPv4 or IPv6 address
3842
*/
3943
public static fromString(str: string): IPv4 | IPv6 {
@@ -42,19 +46,19 @@ export abstract class IPAddress {
4246
}
4347

4448
/**
45-
* Get IP address binary representation
49+
* Gets the IP address binary representation.
4650
*/
4751
public abstract binary(): ArrayBufferView;
4852

4953
/**
50-
* Check if the given addresses are equal
54+
* Checks if the given addresses are equal.
5155
*/
5256
public equals(other: IPAddress): boolean {
5357
return other instanceof this.constructor && other.value === this.value;
5458
}
5559

5660
/**
57-
* Format IP address as string
61+
* Formats the IP address as string.
5862
*/
5963
public abstract toString(): string;
6064
}

src/IPv4.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2024 Cloudnode OÜ
2+
* Copyright © 2024–2025 Cloudnode OÜ
33
*
44
* This file is part of @cldn/ip.
55
*
@@ -17,18 +17,33 @@
1717
import {IPAddress} from "./index.js";
1818

1919
/**
20-
* An IPv4 address
20+
* An IPv4 address.
2121
*/
2222
export class IPv4 extends IPAddress {
23+
/**
24+
* Regular expression for testing IPv4 addresses in string form.
25+
*/
2326
public static regex = /^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$/;
27+
28+
/**
29+
* Bit length of IPv4 addresses.
30+
*/
2431
public static bitLength = 32;
2532

2633
/**
27-
* Create new IPv4 address instance
28-
* @throws {@link !TypeError} If
34+
* Creates a new IPv4 address instance.
35+
*
36+
* @param value A 32-bit unsigned big integer.
37+
* @throws {@link !TypeError} If provided value is not a 32-bit unsigned integer.
2938
*/
3039
public constructor(value: bigint);
3140

41+
/**
42+
* Creates a new IPv4 address instance.
43+
*
44+
* @param value A 32-bit unsigned number.
45+
* @throws {@link !TypeError} If provided value is not a 32-bit unsigned integer.
46+
*/
3247
public constructor(value: number);
3348

3449
public constructor(value: number | bigint) {
@@ -39,8 +54,10 @@ export class IPv4 extends IPAddress {
3954
}
4055

4156
/**
42-
* Create an IPv4 address instance from octets
43-
* @throws {@link !RangeError} If provided octets are not 4
57+
* Creates an IPv4 address instance from octets.
58+
*
59+
* @param octets A typed array of 4 octets.
60+
* @throws {@link !RangeError} If provided octets are not 4.
4461
*/
4562
public static fromBinary(octets: Uint8Array): IPv4 {
4663
if (octets.length !== 4) throw new RangeError("Expected 4 octets, got " + octets.length);
@@ -56,8 +73,10 @@ export class IPv4 extends IPAddress {
5673
}
5774

5875
/**
59-
* Create an IPv4 address instance from string
60-
* @throws {@link !RangeError} If provided string is not a valid IPv4 address
76+
* Creates an IPv4 address instance from a string.
77+
*
78+
* @param str A string representation of an IPv4 address.
79+
* @throws {@link !RangeError} If provided string is not a valid IPv4 address.
6180
*/
6281
public static override fromString(str: string): IPv4 {
6382
const octets = str.split(".", 4).map(octet => Number.parseInt(octet, 10));
@@ -68,7 +87,7 @@ export class IPv4 extends IPAddress {
6887
}
6988

7089
/**
71-
* Get the 4 octets of the IPv4 address
90+
* Gets the 4 octets of the IPv4 address.
7291
*/
7392
public override binary(): Uint8Array {
7493
return new Uint8Array([

src/IPv6.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2024 Cloudnode OÜ
2+
* Copyright © 2024–2025 Cloudnode OÜ
33
*
44
* This file is part of @cldn/ip.
55
*
@@ -17,13 +17,16 @@
1717
import {IPAddress, IPv4, Subnet} from "./index.js";
1818

1919
/**
20-
* An IPv6 address
20+
* An IPv6 address.
2121
*/
2222
export class IPv6 extends IPAddress {
2323
public static bitLength = 128;
2424

2525
/**
26-
* Create new IPv6 address instance
26+
* Creates a new IPv6 address instance.
27+
*
28+
* @param value A 128-bit unsigned big integer.
29+
* @throws {@link !TypeError} If provided value is not a 128-bit unsigned integer.
2730
*/
2831
public constructor(value: bigint) {
2932
if (value < 0n || value > ((1n << BigInt(IPv6.bitLength)) - 1n))
@@ -32,8 +35,10 @@ export class IPv6 extends IPAddress {
3235
}
3336

3437
/**
35-
* Create an IPv6 address instance from hextets
36-
* @throws {@link !RangeError} If provided hextets are not 8
38+
* Creates an IPv6 address instance from hextets.
39+
*
40+
* @param hextets A typed array of 8 hextets.
41+
* @throws {@link !RangeError} If provided hextets are not 8.
3742
*/
3843
public static fromBinary(hextets: Uint16Array): IPv6 {
3944
if (hextets.length !== 8) throw new RangeError("Expected 8 hextets, got " + hextets.length);
@@ -51,8 +56,10 @@ export class IPv6 extends IPAddress {
5156
}
5257

5358
/**
54-
* Create an IPv6 address instance from string
55-
* @throws {@link !RangeError} If provided string is not a valid IPv6 address
59+
* Creates an IPv6 address instance from a string.
60+
*
61+
* @param str A string representation of an IPv6 address.
62+
* @throws {@link !RangeError} If provided string is not a valid IPv6 address.
5663
*/
5764
public static override fromString(str: string): IPv6 {
5865
const parts = str.split("::", 2);
@@ -77,7 +84,7 @@ export class IPv6 extends IPAddress {
7784
}
7885

7986
/**
80-
* Parse string hextet into unsigned 16-bit integer
87+
* Parses a string hextet into unsigned 16-bit integer.
8188
* @internal
8289
*/
8390
private static parseHextet(hextet: string): number | number[] {
@@ -92,7 +99,7 @@ export class IPv6 extends IPAddress {
9299
}
93100

94101
/**
95-
* Get the 8 hextets of the IPv6 address
102+
* Gets the 8 hextets of the IPv6 address
96103
*/
97104
public binary(): Uint16Array {
98105
return new Uint16Array([
@@ -108,16 +115,16 @@ export class IPv6 extends IPAddress {
108115
}
109116

110117
/**
111-
* Check whether this is an IPv4-mapped IPv6 address.
112-
* Only works for `::ffff:0:0/96`
118+
* Checks whether this is an IPv4-mapped IPv6 address. Only works for `::ffff:0:0/96`.
113119
*/
114120
public hasMappedIPv4(): boolean {
115121
return Subnet.IPV4_MAPPED_IPV6.has(this);
116122
}
117123

118124
/**
119-
* Get the IPv4-mapped IPv6 address.
120-
* Returns the last 32 bits as an IPv4 address.
125+
* Gets the IPv4-mapped IPv6 address.
126+
*
127+
* @returns An IPv4 address from the least significant 32 bits of this IPv6 address.
121128
* @see {@link IPv6#hasMappedIPv4}
122129
*/
123130
public getMappedIPv4(): IPv4 {

src/Network.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2024 Cloudnode OÜ
2+
* Copyright © 2024–2025 Cloudnode OÜ
33
*
44
* This file is part of @cldn/ip.
55
*
@@ -17,11 +17,11 @@
1717
import {IPv4, IPv6, Subnet} from "./index.js"
1818

1919
/**
20-
* A network that can contain multiple subnets
20+
* A network that can contain multiple subnets.
2121
*/
2222
export class Network {
2323
/**
24-
* Reserved subnets
24+
* A network of reserved subnets. This network does not contain publicly routable IP addresses.
2525
*/
2626
public static readonly BOGON = new Network([
2727
// IPv4
@@ -58,53 +58,58 @@ export class Network {
5858
readonly #subnets: Map<string, Subnet<IPv4 | IPv6>> = new Map();
5959

6060
/**
61-
* Create new network
62-
* @param [subnets] Initial subnets to add to this network
61+
* Creates a new network.
62+
*
63+
* @param [subnets] Initial subnets to add to this network.
6364
*/
6465
public constructor(subnets?: Iterable<Subnet<IPv4 | IPv6>>) {
6566
if (subnets) for (const subnet of subnets) this.add(subnet);
6667
}
6768

6869
/**
69-
* Add a subnet to this network
70+
* Adds a subnet to this network.
7071
*/
7172
public add(subnet: Subnet<IPv4 | IPv6>): void {
7273
this.#subnets.set(subnet.toString(), subnet);
7374
}
7475

7576
/**
76-
* Remove a subnet from this network
77-
* @param cidr CIDR notation of the subnet to remove
77+
* Removes a subnet from this network.
78+
*
79+
* @param cidr CIDR notation of the subnet to remove.
7880
*/
7981
public remove(cidr: string): void {
8082
this.#subnets.delete(Subnet.fromCIDR(cidr).toString());
8183
}
8284

8385
/**
84-
* Check if subnet is in this network
85-
* @param cidr CIDR notation of the subnet to check
86+
* Checks if a subnet is in this network.
87+
*
88+
* @param cidr CIDR notation of the subnet to check.
8689
*/
8790
public hasSubnet(cidr: string): boolean {
8891
return this.#subnets.has(Subnet.fromCIDR(cidr).toString());
8992
}
9093

9194
/**
92-
* Get all subnets in this network mapped to their CIDR notation
95+
* Gets all subnets in this network mapped to their CIDR notation.
9396
*/
9497
public subnets(): ReadonlyMap<string, Subnet<IPv4 | IPv6>> {
9598
return this.#subnets;
9699
}
97100

98101
/**
99-
* Check if an IP address is in this network
102+
* Checks if an IP address is in this network.
103+
*
104+
* @param address IP address to check.
100105
*/
101106
public has(address: IPv4 | IPv6): boolean {
102107
for (const subnet of this.#subnets.values()) if (subnet.has(address)) return true;
103108
return false;
104109
}
105110

106111
/**
107-
* Get the number of addresses in this network
112+
* Gets the number of addresses in this network.
108113
*/
109114
public size(): bigint {
110115
let size = 0n;

0 commit comments

Comments
 (0)