drift-meter/util.mjs

84 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2021-10-21 18:40:49 +02:00
export function indexOf(array, predicate) {
for (let i in array) {
if (predicate(array[i])) {
return i;
}
}
return -1;
}
export async function renderProperty(
name,
fn,
metadata,
project_settings,
prefix = ""
) {
let value;
if (typeof fn === "string") {
value = fn;
} else {
value = await fn(metadata, project_settings);
}
if (value === null) {
return "";
} else if (value && value.$replace) {
return value.$replace;
} else {
return `<property name="${prefix}${name}">${value}</property>`;
}
}
export async function renderAllProps(
props,
metadata,
project_settings,
prefix = ""
) {
return (
await Promise.all(
Object.entries(props).map(([name, fn]) =>
renderProperty(name, fn, metadata, project_settings, prefix)
)
)
).join("\n");
}
export function getStream(tracks, type) {
return tracks.find((track) => track["@type"] == type);
}
const HOUR = 60 * 60;
const MINUTE = 60;
export function formatDuration(float_s) {
const hours = Math.floor(float_s / HOUR);
float_s = float_s - hours * HOUR;
const minutes = Math.floor(float_s / MINUTE);
const seconds = float_s - minutes * MINUTE;
return `${twoDigits(hours)}:${twoDigits(minutes)}:${twoDigits(seconds, 5)}`;
}
export function twoDigits(number, decimal = 0) {
let [int, dec] = number.toFixed(decimal).split(".");
dec = dec || "";
if (dec == "") {
return int.padStart(2, "0");
} else {
return int.padStart(2, "0") + "." + dec;
}
}
export function getStreamIndex(trackarray, type) {
const array_index = indexOf(trackarray, (e) => e["@type"] == type);
return array_index == -1 ? -1 : trackarray[array_index].StreamOrder || 0;
}
export function* makeIDGen(first = 1) {
let i = first;
while (true) {
yield i;
i++;
}
}