mirror of
https://github.com/home-assistant/frontend.git
synced 2025-11-09 02:49:51 +00:00
Fix/dhcp config network sort (#25799)
* Add ip sort method to compare helper * Add ip sort functionality to dhcp config panel datatable * Add type ip to DataTableColumnData * Change ip sorting to padStart method for better readablity * Rename ip compare method to clarify ipv4 * Enhance IP compare method to include ipv6 * Add compare IP test
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import memoizeOne from "memoize-one";
|
||||
import { isIPAddress } from "./is_ip_address";
|
||||
|
||||
const collator = memoizeOne(
|
||||
(language: string | undefined) => new Intl.Collator(language)
|
||||
@@ -33,6 +34,19 @@ export const stringCompare = (
|
||||
return fallbackStringCompare(a, b);
|
||||
};
|
||||
|
||||
export const ipCompare = (a: string, b: string) => {
|
||||
const aIsIpV4 = isIPAddress(a);
|
||||
const bIsIpV4 = isIPAddress(b);
|
||||
|
||||
if (aIsIpV4 && bIsIpV4) {
|
||||
return ipv4Compare(a, b);
|
||||
}
|
||||
if (!aIsIpV4 && !bIsIpV4) {
|
||||
return ipV6Compare(a, b);
|
||||
}
|
||||
return aIsIpV4 ? -1 : 1;
|
||||
};
|
||||
|
||||
export const caseInsensitiveStringCompare = (
|
||||
a: string,
|
||||
b: string,
|
||||
@@ -64,3 +78,42 @@ export const orderCompare = (order: string[]) => (a: string, b: string) => {
|
||||
|
||||
return idxA - idxB;
|
||||
};
|
||||
|
||||
function ipv4Compare(a: string, b: string) {
|
||||
const num1 = Number(
|
||||
a
|
||||
.split(".")
|
||||
.map((num) => num.padStart(3, "0"))
|
||||
.join("")
|
||||
);
|
||||
const num2 = Number(
|
||||
b
|
||||
.split(".")
|
||||
.map((num) => num.padStart(3, "0"))
|
||||
.join("")
|
||||
);
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
function ipV6Compare(a: string, b: string) {
|
||||
const ipv6a = normalizeIPv6(a)
|
||||
.split(":")
|
||||
.map((part) => part.padStart(4, "0"))
|
||||
.join("");
|
||||
const ipv6b = normalizeIPv6(b)
|
||||
.split(":")
|
||||
.map((part) => part.padStart(4, "0"))
|
||||
.join("");
|
||||
|
||||
return ipv6a.localeCompare(ipv6b);
|
||||
}
|
||||
|
||||
function normalizeIPv6(ip) {
|
||||
const parts = ip.split("::");
|
||||
const head = parts[0].split(":");
|
||||
const tail = parts[1] ? parts[1].split(":") : [];
|
||||
const totalParts = 8;
|
||||
const missing = totalParts - (head.length + tail.length);
|
||||
const zeros = new Array(missing).fill("0");
|
||||
return [...head, ...zeros, ...tail].join(":");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user