mirror of
https://github.com/khoaliber/dockhand.git
synced 2026-03-05 21:29:04 +00:00
Initial commit
This commit is contained in:
15
lib/utils/ip.ts
Normal file
15
lib/utils/ip.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Convert IP address (with optional CIDR) to numeric value for sorting
|
||||
* e.g., "192.168.1.0/24" -> 3232235776, "10.0.0.1" -> 167772161
|
||||
*/
|
||||
export function ipToNumber(ip: string | undefined | null): number {
|
||||
if (!ip || ip === '-') return Infinity; // Push empty IPs to the end
|
||||
// Strip CIDR notation if present
|
||||
const ipOnly = ip.split('/')[0];
|
||||
const parts = ipOnly.split('.');
|
||||
if (parts.length !== 4) return Infinity;
|
||||
return parts.reduce((acc, octet) => {
|
||||
const num = parseInt(octet, 10);
|
||||
return isNaN(num) ? Infinity : (acc << 8) + num;
|
||||
}, 0) >>> 0; // Convert to unsigned 32-bit
|
||||
}
|
||||
Reference in New Issue
Block a user