tidal-hifi/src/scripts/mediaInfo.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-05-07 15:45:45 +02:00
import { MediaInfo } from "../models/mediaInfo";
import { MediaStatus } from "../models/mediaStatus";
2023-05-07 15:45:45 +02:00
export const mediaInfo = {
title: "",
artists: "",
album: "",
icon: "",
status: MediaStatus.paused as string,
2023-05-07 15:45:45 +02:00
url: "",
current: "",
duration: "",
image: "tidal-hifi-icon",
favorite: false,
2023-05-07 15:45:45 +02:00
};
export const updateMediaInfo = (arg: MediaInfo) => {
mediaInfo.title = propOrDefault(arg.title);
mediaInfo.artists = propOrDefault(arg.artists);
mediaInfo.album = propOrDefault(arg.album);
mediaInfo.icon = propOrDefault(arg.icon);
mediaInfo.url = toUniversalUrl(propOrDefault(arg.url));
2023-05-07 15:45:45 +02:00
mediaInfo.status = propOrDefault(arg.status);
mediaInfo.current = propOrDefault(arg.current);
mediaInfo.duration = propOrDefault(arg.duration);
mediaInfo.image = propOrDefault(arg.image);
mediaInfo.favorite = arg.favorite;
2023-05-07 15:45:45 +02:00
};
/**
* Return the property or a default value
* @param {*} prop property to check
* @param {*} defaultValue defaults to ""
*/
function propOrDefault(prop: string, defaultValue = "") {
return prop || defaultValue;
}
/**
* Append the universal link syntax (?u) to any url
* @param url url to append the universal link syntax to
* @returns url with `?u` appended, or the original value of url if falsy
*/
function toUniversalUrl(url: string) {
if (url) {
const queryParamsSet = url.indexOf("?");
return queryParamsSet > -1 ? `${url}&u` : `${url}?u`;
}
return url;
2023-05-07 15:45:45 +02:00
}