feat: Add folder title matching and parsing (#2067)

This is a breaking change for users of the [experimental 'folder'
functionality](https://card.camera/#/configuration/folders).

Related: #1748
This commit is contained in:
Dermot Duffy
2025-05-27 07:50:51 -07:00
committed by GitHub
parent c4400194f5
commit c077f8bf70
35 changed files with 1038 additions and 378 deletions
@@ -0,0 +1,38 @@
import { Matcher } from '../../../config/schema/folders';
import { BrowseMedia } from '../../../ha/browse-media/types';
import { regexpExtract } from '../../../utils/regexp-extract';
import { REGEXP_GROUP_VALUE_KEY } from './types';
export class MediaMatcher {
public match(media: BrowseMedia, matchers?: Matcher[], foldersOnly = false): boolean {
if (foldersOnly && !media.can_expand) {
return false;
}
for (const matcher of matchers ?? []) {
if (matcher.type === 'title') {
if (!this._matchTitle(matcher, media.title)) {
return false;
}
}
}
return true;
}
private _matchTitle(matcher: Matcher, src: string): boolean {
const valueToMatch = matcher.regexp
? regexpExtract(matcher.regexp, src, { groupName: REGEXP_GROUP_VALUE_KEY })
: src;
if (!valueToMatch) {
return false;
}
if (matcher.title) {
return valueToMatch === matcher.title;
}
return true;
}
}