// A CUT'S OWN NAME FOR A CHANNEL (lane channel-title): `render.channelNames`, // `{ "": "" }` -- what every clip of that channel is called on // screen in this cut, whatever the record of each recording says. One // channel's two recordings can carry two uploader names (an old one and the // current one); this says once which to show, without touching a clip. // // The resolver is umtool's report-to-video/attribution.mjs `channelName`: // a clip's own `channelTitle` > this > the record's uploader name > the // sweep's display name > the slug. This module holds the rules every writer // and reader checks (look.mjs applyLookPatch / validateLook, umtool's clip // writer for a clip's `channelTitle`), so the editor's video assistant, the // CLI, the Look panel and the build refuse the same names. // // PURE: no fs. Plain ESM with a .d.mts beside it. const isObj = (v) => !!v && typeof v === "object" && !Array.isArray(v); /** The longest name a channel is given on screen, in characters. */ export const CHANNEL_NAME_MAX = 60; /** A channel slug as a clip's `channel` names it: no space, no slash, not empty. */ export const CHANNEL_SLUG_RE = /^[^\s/]+$/; // C0 controls (newline and tab among them), DEL and the C1 controls. // eslint-disable-next-line no-control-regex const CONTROL_RE = /[\u0000-\u001f\u007f-\u009f]/; /** * Why `value` cannot be a channel's name on screen, or null when it can: words * (a string), not empty once trimmed, at most CHANNEL_NAME_MAX characters, no * control character (a newline would break the one line the header is). * @param {unknown} value * @returns {string | null} */ export function channelNameProblem(value) { if (typeof value !== "string") return `a channel's name is words, not ${JSON.stringify(value)}`; const v = value.trim(); if (!v) return "a channel's name is empty (clear it instead)"; if (CONTROL_RE.test(value)) return "a channel's name has a control character (a newline, a tab …): one line of plain words"; const n = [...v].length; if (n > CHANNEL_NAME_MAX) return `a channel's name is ${n} characters (at most ${CHANNEL_NAME_MAX})`; return null; } /** `render.channelNames` as a plain map (an empty one when absent or malformed). */ export function channelNamesOf(render) { return isObj(render?.channelNames) ? render.channelNames : {}; } /** * `render.channelNames`: absent, or an object of slug → name, each name by * channelNameProblem's rules. * @returns {string[]} */ export function validateChannelNames(manifest) { const v = manifest?.render?.channelNames; if (v === undefined || v === null) return []; if (!isObj(v)) return ['render.channelNames must be an object {"": ""}']; const errors = []; for (const [slug, name] of Object.entries(v)) { if (!CHANNEL_SLUG_RE.test(slug)) { errors.push(`render.channelNames: ${JSON.stringify(slug)} is not a channel slug (no spaces, no slash)`); continue; } const p = channelNameProblem(name); if (p) errors.push(`render.channelNames.${slug}: ${p}`); } return errors; } /** * A patch of `render.channelNames` over what it holds: each key set (a string * trimmed), a key null dropping one; `null` drops them all. Returns the new * map, or null when none is left (written as no key). A value that is not a * string is kept as given, for validateChannelNames to name. * @param {unknown} was * @param {unknown} patch * @returns {Record | null} */ export function patchChannelNames(was, patch) { if (patch === null || patch === undefined) return null; if (!isObj(patch)) return /** @type {any} */ (patch); const next = { ...(isObj(was) ? was : {}) }; for (const [k, v] of Object.entries(patch)) { if (v === null || v === undefined) delete next[k]; else next[k] = typeof v === "string" ? v.trim() : v; } return Object.keys(next).length ? next : null; } /** * The names given to a channel no clip of the cut is from -- a typo in the * slug, or a clip since removed. Warned about, never refused: a name kept for * a clip about to come back is harmless. * @returns {string[]} */ export function channelNamesWarnings(manifest) { const names = channelNamesOf(manifest?.render); const keys = Object.keys(names); if (!keys.length) return []; const own = manifest?.provenance?.channelSlug ?? null; const used = new Set( (Array.isArray(manifest?.timeline) ? manifest.timeline : []) .filter((e) => isObj(e) && e.type === "clip") .map((e) => e.channel ?? own) .filter(Boolean), ); return keys.filter((k) => !used.has(k)).map((k) => `render.channelNames.${k}: no clip of this cut is from "${k}" -- kept, but it names nothing (${[...used].join(", ") || "no clips"})`); }