video_press/static/js/modules/utils.js

45 lines
972 B
JavaScript

/**
* utils.js
* --------
* Pure utility functions with no DOM or state dependencies.
* Safe to import anywhere without side-effects.
*/
/**
* Escape a string for safe insertion into HTML.
* @param {*} str
* @returns {string}
*/
export function esc(str) {
if (!str) return '';
return String(str)
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
}
/**
* Format a duration in seconds as M:SS or H:MM:SS.
* @param {number} seconds
* @returns {string}
*/
export function fmtTime(seconds) {
const s = Math.floor(seconds);
const h = Math.floor(s / 3600);
const m = Math.floor((s % 3600) / 60);
const sec = s % 60;
return h > 0
? `${h}:${pad(m)}:${pad(sec)}`
: `${m}:${pad(sec)}`;
}
/**
* Zero-pad a number to at least 2 digits.
* @param {number} n
* @returns {string}
*/
export function pad(n) {
return String(n).padStart(2, '0');
}