mirror of
https://github.com/home-assistant/frontend.git
synced 2025-05-01 08:47:21 +00:00
Improve stripPrefixFromEntityName
to handle colon and space separator (#11691)
This commit is contained in:
parent
9c8d683a19
commit
d049990f04
@ -1,24 +1,32 @@
|
|||||||
|
const SUFFIXES = [" ", ": "];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strips a device name from an entity name.
|
* Strips a device name from an entity name.
|
||||||
* @param entityName the entity name
|
* @param entityName the entity name
|
||||||
* @param lowerCasedPrefixWithSpaceSuffix the prefix to strip, lower cased with a space suffix
|
* @param lowerCasedPrefix the prefix to strip, lower cased
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const stripPrefixFromEntityName = (
|
export const stripPrefixFromEntityName = (
|
||||||
entityName: string,
|
entityName: string,
|
||||||
lowerCasedPrefixWithSpaceSuffix: string
|
lowerCasedPrefix: string
|
||||||
) => {
|
) => {
|
||||||
if (!entityName.toLowerCase().startsWith(lowerCasedPrefixWithSpaceSuffix)) {
|
const lowerCasedEntityName = entityName.toLowerCase();
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const newName = entityName.substring(lowerCasedPrefixWithSpaceSuffix.length);
|
for (const suffix of SUFFIXES) {
|
||||||
|
const lowerCasedPrefixWithSuffix = `${lowerCasedPrefix}${suffix}`;
|
||||||
|
|
||||||
|
if (lowerCasedEntityName.startsWith(lowerCasedPrefixWithSuffix)) {
|
||||||
|
const newName = entityName.substring(lowerCasedPrefixWithSuffix.length);
|
||||||
|
|
||||||
// If first word already has an upper case letter (e.g. from brand name)
|
// If first word already has an upper case letter (e.g. from brand name)
|
||||||
// leave as-is, otherwise capitalize the first word.
|
// leave as-is, otherwise capitalize the first word.
|
||||||
return hasUpperCase(newName.substr(0, newName.indexOf(" ")))
|
return hasUpperCase(newName.substr(0, newName.indexOf(" ")))
|
||||||
? newName
|
? newName
|
||||||
: newName[0].toUpperCase() + newName.slice(1);
|
: newName[0].toUpperCase() + newName.slice(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
const hasUpperCase = (str: string): boolean => str.toLowerCase() !== str;
|
const hasUpperCase = (str: string): boolean => str.toLowerCase() !== str;
|
||||||
|
@ -165,7 +165,7 @@ export class HaDeviceEntitiesCard extends LitElement {
|
|||||||
const stateObj = this.hass.states[entry.entity_id];
|
const stateObj = this.hass.states[entry.entity_id];
|
||||||
const name = stripPrefixFromEntityName(
|
const name = stripPrefixFromEntityName(
|
||||||
computeStateName(stateObj),
|
computeStateName(stateObj),
|
||||||
`${this.deviceName} `.toLowerCase()
|
this.deviceName.toLowerCase()
|
||||||
);
|
);
|
||||||
if (name) {
|
if (name) {
|
||||||
config.name = name;
|
config.name = name;
|
||||||
@ -198,7 +198,7 @@ export class HaDeviceEntitiesCard extends LitElement {
|
|||||||
${name
|
${name
|
||||||
? stripPrefixFromEntityName(
|
? stripPrefixFromEntityName(
|
||||||
name,
|
name,
|
||||||
`${this.deviceName} `.toLowerCase()
|
this.deviceName.toLowerCase()
|
||||||
) || name
|
) || name
|
||||||
: entry.entity_id}
|
: entry.entity_id}
|
||||||
</div>
|
</div>
|
||||||
|
@ -97,7 +97,7 @@ export const computeCards = (
|
|||||||
const entities: Array<string | LovelaceRowConfig> = [];
|
const entities: Array<string | LovelaceRowConfig> = [];
|
||||||
|
|
||||||
const titlePrefix = entityCardOptions.title
|
const titlePrefix = entityCardOptions.title
|
||||||
? `${entityCardOptions.title} `.toLowerCase()
|
? entityCardOptions.title.toLowerCase()
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
const footerEntities: ButtonsHeaderFooterConfig["entities"] = [];
|
const footerEntities: ButtonsHeaderFooterConfig["entities"] = [];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user