taglib-wasm
Version:
TagLib for TypeScript platforms: Deno, Node.js, Bun, Electron, browsers, and Cloudflare Workers
64 lines (63 loc) • 1.28 kB
JavaScript
function toNormalized(popm) {
return popm / 255;
}
function fromNormalized(normalized) {
return Math.round(normalized * 255);
}
function toStars(normalized, maxStars = 5) {
return Math.round(normalized * maxStars);
}
function fromStars(stars, maxStars = 5) {
return stars / maxStars;
}
const POPM_STAR_VALUES = [0, 1, 64, 128, 196, 255];
function toPopm(normalized) {
const stars = Math.round(normalized * 5);
return POPM_STAR_VALUES[stars] ?? 0;
}
function fromPopm(popm) {
if (popm === 0) return 0;
if (popm <= 1) return 0.2;
if (popm <= 64) return 0.4;
if (popm <= 128) return 0.6;
if (popm <= 196) return 0.8;
return 1;
}
function toPercent(normalized) {
return normalized * 100;
}
function fromPercent(percent) {
return percent / 100;
}
function clamp(rating) {
return Math.max(0, Math.min(1, rating));
}
function isValid(rating) {
return typeof rating === "number" && !isNaN(rating) && rating >= 0 && rating <= 1;
}
const RatingUtils = {
toNormalized,
fromNormalized,
toStars,
fromStars,
toPopm,
fromPopm,
toPercent,
fromPercent,
clamp,
isValid,
POPM_STAR_VALUES
};
export {
RatingUtils,
clamp,
fromNormalized,
fromPercent,
fromPopm,
fromStars,
isValid,
toNormalized,
toPercent,
toPopm,
toStars
};