Skip to content

Commit

Permalink
web: fix IP Sorting (#1445)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu authored Jan 17, 2025
1 parent 9c07a2e commit 8948d1b
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,20 @@ export function first<T>(...args: Array<T | undefined | null>): T {
throw new Error(`No compatible arg given: ${args}`);
}

export function ip(raw: string): AbstractIPNum {
let ip: AbstractIPNum;
export function ip(raw: string): AbstractIPNum & { getValue(): bigint } {
try {
ip = IPv6.fromString(raw);
return IPv6.fromString(raw);
} catch {
ip = IPv4.fromString(raw);
return IPv4.fromString(raw);
}
return ip;
}

export function sortByIP<T>(getter: (item: T) => string): (a: T, b: T) => number {
return (a: T, b: T) => {
const aIP = ip(getter(a));
const bIP = ip(getter(b));
if (aIP > bIP) return 1;
if (aIP < bIP) return -1;
if (aIP.getValue() > bIP.getValue()) return 1;
if (aIP.getValue() < bIP.getValue()) return -1;
return 0;
};
}

0 comments on commit 8948d1b

Please sign in to comment.