1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-20 02:38:53 +00:00

Consolidate floor sorting with floorCompare() (#27553)

* data/floor_registry: Fix `floorCompare()` argument type

* data/floor_registry: Add test suite for `floorCompare()` fn

* data/floor_registry: Add level-based sorting to `floorCompare()`

Update `floorCompare()` to include level-based sorting between custom order
and name sorting, matching the pattern used in the `getFloors()` helper.

Sort priority:
1. Custom order (if provided)
2. Floor level (lower levels first, with 0 fallback for null)
3. Floor name (alphabetical)

This makes `floorCompare()` consistent with the floor sorting logic used
throughout the codebase and prepares it for actual use.

* areas-strategy-helper: Use `floorCompare()` in `getFloors()` helper

Replace duplicated floor sorting logic in `getFloors()` with the
centralized `floorCompare()` function, matching the pattern used
for areas with `areaCompare()`.

This eliminates code duplication and ensures consistent floor sorting
across the codebase.

* data/area_floor: Use `floorCompare()` in `getAreasAndFloors()`

Replace duplicated floor sorting logic in `getAreasAndFloors()` with
`floorCompare()`, passing `haFloors` directly since it's already in
the correct format (Record<string, FloorRegistryEntry>).

This ensures consistent floor sorting across the codebase.
This commit is contained in:
Tobias Bieniek
2025-10-18 20:16:30 +02:00
committed by GitHub
parent 82ac26b326
commit 5f71938d60
4 changed files with 138 additions and 26 deletions

View File

@@ -0,0 +1,116 @@
import { describe, expect, it } from "vitest";
import { floorCompare } from "../../src/data/floor_registry";
import type { FloorRegistryEntry } from "../../src/data/floor_registry";
describe("floorCompare", () => {
describe("floorCompare()", () => {
it("sorts by floor ID alphabetically", () => {
const floors = ["basement", "attic", "ground"];
expect(floors.sort(floorCompare())).toEqual([
"attic",
"basement",
"ground",
]);
});
it("handles numeric strings in natural order", () => {
const floors = ["floor10", "floor2", "floor1"];
expect(floors.sort(floorCompare())).toEqual([
"floor1",
"floor2",
"floor10",
]);
});
});
describe("floorCompare(entries)", () => {
it("sorts by level, then by name", () => {
const entries = {
floor1: { name: "Ground Floor", level: 0 } as FloorRegistryEntry,
floor2: { name: "First Floor", level: 1 } as FloorRegistryEntry,
floor3: { name: "Basement", level: -1 } as FloorRegistryEntry,
};
const floors = ["floor1", "floor2", "floor3"];
expect(floors.sort(floorCompare(entries))).toEqual([
"floor3",
"floor1",
"floor2",
]);
});
it("treats null level as 0", () => {
const entries = {
floor1: { name: "Ground Floor", level: 0 } as FloorRegistryEntry,
floor2: { name: "First Floor", level: 1 } as FloorRegistryEntry,
floor3: { name: "Basement", level: null } as FloorRegistryEntry,
};
const floors = ["floor2", "floor3", "floor1"];
expect(floors.sort(floorCompare(entries))).toEqual([
"floor3",
"floor1",
"floor2",
]);
});
it("sorts by name when levels are equal", () => {
const entries = {
floor1: { name: "Suite B", level: 1 } as FloorRegistryEntry,
floor2: { name: "Suite A", level: 1 } as FloorRegistryEntry,
};
const floors = ["floor1", "floor2"];
expect(floors.sort(floorCompare(entries))).toEqual(["floor2", "floor1"]);
});
it("falls back to floor ID when entry not found", () => {
const entries = {
floor1: { name: "Ground Floor" } as FloorRegistryEntry,
};
const floors = ["xyz", "floor1", "abc"];
expect(floors.sort(floorCompare(entries))).toEqual([
"abc",
"floor1",
"xyz",
]);
});
});
describe("floorCompare(entries, order)", () => {
it("follows order array", () => {
const entries = {
basement: { name: "Basement" } as FloorRegistryEntry,
ground: { name: "Ground Floor" } as FloorRegistryEntry,
first: { name: "First Floor" } as FloorRegistryEntry,
};
const order = ["first", "ground", "basement"];
const floors = ["basement", "first", "ground"];
expect(floors.sort(floorCompare(entries, order))).toEqual([
"first",
"ground",
"basement",
]);
});
it("places items not in order array at the end, sorted by name", () => {
const entries = {
floor1: { name: "First Floor" } as FloorRegistryEntry,
floor2: { name: "Ground Floor" } as FloorRegistryEntry,
floor3: { name: "Basement" } as FloorRegistryEntry,
};
const order = ["floor1"];
const floors = ["floor3", "floor2", "floor1"];
expect(floors.sort(floorCompare(entries, order))).toEqual([
"floor1",
"floor3",
"floor2",
]);
});
});
});