Files
2026-05-26 22:46:00 +02:00

44 lines
1.4 KiB
TypeScript

import { extractCode } from "./codeParser";
export interface ParsedDropFilename {
/** Original filename with extension. */
original: string;
/** Canonical "PREFIX-NUMBER" code, or null. */
code: string | null;
/** Actress display names parsed from filename (after the first " - "). */
actresses: string[];
/** Suggested filename to save as (CODE.ext) when code is known; otherwise the original. */
targetFilename: string;
}
/**
* Parse a dropped image filename of the form:
* "DDK-134.jpg"
* "DDK-134 - Shuri Atomi.jpg"
* "DDK-134 - Shuri Atomi, Reika Aiba.jpg"
*
* The first " - " (space-hyphen-space) after the code separates the code/title from
* the actress list. Multiple actresses are comma-separated.
*/
export function parseDroppedFilename(name: string): ParsedDropFilename {
const extMatch = name.match(/\.([^.]+)$/);
const ext = extMatch ? extMatch[1] : "";
const stem = ext ? name.slice(0, -(ext.length + 1)) : name;
const code = extractCode(name);
let actresses: string[] = [];
const sepIdx = stem.indexOf(" - ");
if (sepIdx !== -1) {
const tail = stem.slice(sepIdx + 3).trim();
if (tail) {
actresses = tail
.split(/\s*,\s*/)
.map((s) => s.trim())
.filter(Boolean);
}
}
const targetFilename = code ? (ext ? `${code}.${ext}` : code) : name;
return { original: name, code, actresses, targetFilename };
}