taglib-wasm
Version:
TagLib-Wasm is the universal tagging library for TypeScript/JavaScript platforms: Browsers, Node.js, Deno, Bun, Cloudflare Workers, and Electron apps
74 lines (73 loc) • 1.39 kB
JavaScript
function normalized(value) {
return value;
}
function popm(value) {
return value;
}
function toNormalized(value) {
return value / 255;
}
function fromNormalized(value) {
return Math.round(value * 255);
}
function toStars(value, maxStars = 5) {
return Math.round(value * maxStars);
}
function fromStars(stars, maxStars = 5) {
return stars / maxStars;
}
const POPM_STAR_VALUES = [0, 1, 64, 128, 196, 255];
function toPopm(value) {
const stars = Math.round(value * 5);
return POPM_STAR_VALUES[stars] ?? 0;
}
function fromPopm(value) {
if (value === 0) return 0;
if (value <= 1) return 0.2;
if (value <= 64) return 0.4;
if (value <= 128) return 0.6;
if (value <= 196) return 0.8;
return 1;
}
function toPercent(value) {
return value * 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" && !Number.isNaN(rating) && rating >= 0 && rating <= 1;
}
const RatingUtils = {
normalized,
popm,
toNormalized,
fromNormalized,
toStars,
fromStars,
toPopm,
fromPopm,
toPercent,
fromPercent,
clamp,
isValid,
POPM_STAR_VALUES
};
export {
RatingUtils,
clamp,
fromNormalized,
fromPercent,
fromPopm,
fromStars,
isValid,
normalized,
popm,
toNormalized,
toPercent,
toPopm,
toStars
};