Files
rf4-spotter/apps/web/src/lib/media.ts
T
ik b47a6cc366
CI / backend-and-migrations (push) Canceled after 0s
CI / astro-build (push) Canceled after 0s
CI / dependency-audit (push) Canceled after 0s
CI / compose-e2e (push) Canceled after 0s
feat: publish approved RF4 media catalog
2026-09-14 22:20:46 +07:00

24 lines
860 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { MediaAsset } from "./api";
const tokens = (value: string) => value
.toLocaleLowerCase("ru")
.replaceAll("ё", "е")
.replace(/^(оз\.|р\.)\s*/, "")
.replace(/[^a-zа-я0-9]+/giu, " ")
.trim()
.split(/\s+/)
.filter(Boolean);
export const findMediaByLabel = (assets: MediaAsset[], label: string): MediaAsset | undefined => {
const wanted = tokens(label);
const exact = assets.filter((asset) => tokens(asset.label ?? "").join(" ") === wanted.join(" "));
if (exact.length === 1) return exact[0];
const wantedSet = new Set(wanted);
const candidates = assets.filter((asset) => {
const available = new Set(tokens(asset.label ?? ""));
return wanted.every((token) => available.has(token)) || [...available].every((token) => wantedSet.has(token));
});
return candidates.length === 1 ? candidates[0] : undefined;
};