From 9f4e4c85da06730feb5ef1a525fee89f90d30954 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Fri, 13 Feb 2026 17:14:59 +0100 Subject: [PATCH] feat: favourite transformations --- src/common/playlist.js | 32 +++++++++++++++++++ .../orchestrator/favourites/common.js | 20 +----------- .../orchestrator/favourites/element.js | 5 +-- .../orchestrator/favourites/worker.js | 20 +++++++++--- src/definitions/output/playlist.json | 2 +- 5 files changed, 53 insertions(+), 26 deletions(-) create mode 100644 src/common/playlist.js diff --git a/src/common/playlist.js b/src/common/playlist.js new file mode 100644 index 00000000..74cb5a88 --- /dev/null +++ b/src/common/playlist.js @@ -0,0 +1,32 @@ +/** + * @import {Playlist, PlaylistItem, Track} from "@definitions/types.d.ts" + */ + +/** + * Check if a track matches the criteria of a playlist item. + * + * @param {Track} track + * @param {PlaylistItem} item + */ +export function match(track, item) { + return item.criteria.every((c) => { + /** @type {any} */ + let value = track; + let critValue = c.value + + c.field.split(".").forEach((f) => { + if (value) value = value[f]; + }); + + if (value && c.transformations) { + c.transformations.forEach((t) => { + try { + value = value[t]() + critValue = value[t]() + } catch (err) {} + }); + } + + return critValue === value; + }); +} diff --git a/src/components/orchestrator/favourites/common.js b/src/components/orchestrator/favourites/common.js index 447819e3..f9d6fc64 100644 --- a/src/components/orchestrator/favourites/common.js +++ b/src/components/orchestrator/favourites/common.js @@ -1,5 +1,5 @@ /** - * @import {Playlist, PlaylistItem, Track} from "@definitions/types.d.ts" + * @import {Playlist} from "@definitions/types.d.ts" */ /** @@ -20,21 +20,3 @@ export function createEmptyFavouritesPlaylist() { updatedAt: now, }); } - -/** - * Check if a track is a favourite based on the `PlaylistItem`. - * - * @param {Track} track - * @param {PlaylistItem} item - */ -export function matchItemWithTrack(track, item) { - const artist = item.criteria.find((c) => c.field === "artist")?.value - .toString() - .toLowerCase(); - const title = item.criteria.find((c) => c.field === "title")?.value - .toString() - .toLowerCase(); - - return track.tags?.artist?.toLowerCase() === artist && - track.tags?.title?.toLowerCase() === title; -} diff --git a/src/components/orchestrator/favourites/element.js b/src/components/orchestrator/favourites/element.js index fb973320..9e602a40 100644 --- a/src/components/orchestrator/favourites/element.js +++ b/src/components/orchestrator/favourites/element.js @@ -1,6 +1,7 @@ import { BroadcastableDiffuseElement, query } from "@common/element.js"; +import { match as matchPlaylistItem } from "@common/playlist.js"; import { computed, signal } from "@common/signal.js"; -import { createEmptyFavouritesPlaylist, matchItemWithTrack } from "./common.js"; +import { createEmptyFavouritesPlaylist } from "./common.js"; /** * @import {Playlist, Track} from "@definitions/types.d.ts" @@ -163,7 +164,7 @@ class FavouritesOrchestrator extends BroadcastableDiffuseElement { */ isFavourite(track) { return this.playlist().items.some((item) => { - return matchItemWithTrack(track, item); + return matchPlaylistItem(track, item); }); } } diff --git a/src/components/orchestrator/favourites/worker.js b/src/components/orchestrator/favourites/worker.js index 66f6f47b..7eb23777 100644 --- a/src/components/orchestrator/favourites/worker.js +++ b/src/components/orchestrator/favourites/worker.js @@ -25,8 +25,10 @@ function matchKey(track) { * @returns {string} */ function itemMatchKey(item) { - const artist = item.criteria.find((c) => c.field === "tags.artist")?.value ?? ""; - const title = item.criteria.find((c) => c.field === "tags.title")?.value ?? ""; + const artist = item.criteria.find((c) => c.field === "tags.artist")?.value ?? + ""; + const title = item.criteria.find((c) => c.field === "tags.title")?.value ?? + ""; return `${artist}.${title}`; } @@ -35,9 +37,19 @@ function itemMatchKey(item) { * @param {Track} track */ function trackCriteria(track) { + const transformations = ["toLowerCase"]; + return [ - { field: "tags.artist", value: /** @type {unknown} */ (track.tags?.artist) }, - { field: "tags.title", value: /** @type {unknown} */ (track.tags?.title) }, + { + field: "tags.artist", + value: /** @type {unknown} */ (track.tags?.artist), + transformations, + }, + { + field: "tags.title", + value: /** @type {unknown} */ (track.tags?.title), + transformations, + }, ]; } diff --git a/src/definitions/output/playlist.json b/src/definitions/output/playlist.json index 2286c394..1572a3ae 100644 --- a/src/definitions/output/playlist.json +++ b/src/definitions/output/playlist.json @@ -72,7 +72,7 @@ }, "transformation": { "type": "string", - "description": "Transformation to apply to the field value before comparison" + "description": "Transformation to apply to the field value before comparison. This transformation is method to call on the value. For example, `toString`" } } } -- knotmirror 0.0.1