Skip to content

Commit

Permalink
Stopped unnecessary date processing
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwroc committed Dec 21, 2023
1 parent 62ce68a commit 863eb71
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/custom-elements/battery-state-entity.views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { HomeAssistant } from "custom-card-helpers";
import { TemplateResult, html } from "lit";
import { BatteryStateEntity } from "./battery-state-entity";

const relativeTimeTag = new RegExp("<rt>([0-9]+)</rt>", "g");
const relativeTimeTag = new RegExp("<rt>([^<]+)</rt>", "g");

/**
* Replaces temporary RT tages with proper HA "relative-time" ones
Expand All @@ -24,7 +24,7 @@ const replaceTags = (text: string, hass?: HomeAssistant): TemplateResult[] => {
result.push(html`${text.substring(currentPos, matchPos)}`);
}

result.push(html`<ha-relative-time .hass="${hass}" .datetime="${new Date(Number(matches[1]))}"></ha-relative-time>`);
result.push(html`<ha-relative-time .hass="${hass}" .datetime="${new Date(matches[1])}"></ha-relative-time>`);

currentPos += matchPos + matches[0].length;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rich-string-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ const availableProcessors: IMap<IProcessorCtor> = {
}

// The RT tags will be converted to proper HA tags at the views layer
return `<rt>${unixTime}</rt>`
return `<rt>${val}</rt>`
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/other/rich-string-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ describe("RichStringProcessor", () => {
});

test.each([
["{state|reltime()}", new Date(2021, 7, 25).toUTCString(), "<rt>1629846000000</rt>"],
["Rel time: {state|reltime()}", new Date(2021, 7, 25).toUTCString(), "Rel time: <rt>1629846000000</rt>"],
["{state|reltime()}", "2021-08-25T00:00:00.000Z", "<rt>2021-08-25T00:00:00.000Z</rt>"],
["Rel time: {state|reltime()}", "2021-08-25T00:00:00.000Z", "Rel time: <rt>2021-08-25T00:00:00.000Z</rt>"],
["Not date: {state|reltime()}", "this is not date", "Not date: this is not date"],
])("reltime function", (text: string, state:string, expectedResult: string) => {
const hassMock = new HomeAssistantMock<BatteryStateEntity>(true);
Expand Down

0 comments on commit 863eb71

Please sign in to comment.