rewrite convertIpAddressToInt
This commit is contained in:
@@ -3,11 +3,5 @@ export function convertIntToIpAddress(int: number): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function convertIpAddressToInt(ip: string): number | null {
|
export function convertIpAddressToInt(ip: string): number | null {
|
||||||
const parts = ip.split(".").map(Number).reverse(); // little-endian byte order
|
return ip.split('.').reverse().reduce((ipnum, octet) => { return (ipnum<<8) + parseInt(octet)}, 0) >>> 0;
|
||||||
|
|
||||||
if (parts.some(Number.isNaN)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return parts.reduce((total, part) => (total << 8) | part, 0);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user