Skip to content

Commit

Permalink
Merge pull request #628 from maxwroc/FixDecimalSort
Browse files Browse the repository at this point in the history
Fixed sorting of decimal numbers separated by comma
  • Loading branch information
maxwroc authored Jan 4, 2024
2 parents a52965b + 4e6dd2d commit 8df014a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "battery-state-card",
"version": "3.1.0",
"version": "3.1.1",
"description": "Battery State card for Home Assistant",
"main": "dist/battery-state-card.js",
"author": "Max Chodorowski",
Expand Down
6 changes: 4 additions & 2 deletions src/sorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import { isNumber, log, safeGetConfigArrayOfObjects } from "./utils";
valB = batteries[idB].name;
break;
case "state":
valA = batteries[idA].state;
valB = batteries[idB].state;
// not a perfect solution but we try to fix numer formatting in some countries/langs
// where decimals are separated by comma
valA = batteries[idA].state?.replace(",", ".");
valB = batteries[idB].state?.replace(",", ".");
break;
default:
if ((<string>o.by).startsWith("entity.")) {
Expand Down
18 changes: 18 additions & 0 deletions test/other/sorting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ describe("Entity sorting", () => {

expect(sortedIds).toStrictEqual(expectedOrder);
});

test.each([
["state", "38", "38,5", "38,4", ["a_sensor", "c_sensor", "b_sensor"]],
["state", "38", "99,5", "99,4", ["a_sensor", "c_sensor", "b_sensor"]],
["state", "38", "99,4", "99,44", ["a_sensor", "b_sensor", "c_sensor"]],
["state", "38", "99.4", "99.44", ["a_sensor", "b_sensor", "c_sensor"]],
])("Decimals separated by comma", (sort: string, stateA: string | undefined, stateB: string | undefined, stateC: string | undefined, expectedOrder: string[]) => {

let testBatteries = [
createBattery("a Sensor", stateA),
createBattery("b Sensor", stateB),
createBattery("c Sensor", stateC),
];

const sortedIds = getIdsOfSortedBatteries({ entities: [], sort }, convertToCollection(testBatteries));

expect(sortedIds).toStrictEqual(expectedOrder);
});
});

const createBattery = (name: string, state: string | undefined, last_changed?: string | undefined) => {
Expand Down

0 comments on commit 8df014a

Please sign in to comment.