Skip to content

Commit 1079f3c

Browse files
committed
simplify max IP check
1 parent d99adfc commit 1079f3c

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

src/IPv4.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export class IPv4 extends IPAddress {
1717

1818
public constructor(value: number | bigint) {
1919
const int = BigInt(value);
20-
if (int < 0n || int > 0xFFFFFFFFn) throw new TypeError("Expected 32-bit unsigned integer, got " + int.constructor.name + " " + int.toString(10));
20+
if (int < 0n || int > (1n << BigInt(IPv4.bitLength)) - 1n)
21+
throw new TypeError("Expected 32-bit unsigned integer, got " + int.constructor.name + " " + int.toString(10));
2122
super(int);
2223
}
2324

src/IPv6.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class IPv6 extends IPAddress {
1010
* Create new IPv6 address instance
1111
*/
1212
public constructor(value: bigint) {
13-
if (value < 0n || value > 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFn)
13+
if (value < 0n || value > ((1n << BigInt(IPv6.bitLength)) - 1n))
1414
throw new TypeError("Expected 128-bit unsigned integer, got " + value.constructor.name + " 0x" + value.toString(16));
1515
super(value);
1616
}

0 commit comments

Comments
 (0)