feat(bulk-upload): add bulkTitleFromFilename utility
Converts a raw filename into a human-readable title candidate by stripping the extension and replacing underscore/hyphen runs with spaces. Reuses the existing stripExtension() helper. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parseFilename, stripExtension } from './filename';
|
||||
import { parseFilename, stripExtension, bulkTitleFromFilename } from './filename';
|
||||
|
||||
describe('parseFilename', () => {
|
||||
describe('date-first patterns', () => {
|
||||
@@ -86,6 +86,24 @@ describe('parseFilename', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('bulkTitleFromFilename', () => {
|
||||
it('replaces underscores with spaces', () => {
|
||||
expect(bulkTitleFromFilename('hello_world.pdf')).toBe('hello world');
|
||||
});
|
||||
|
||||
it('replaces hyphens with spaces', () => {
|
||||
expect(bulkTitleFromFilename('2024-01-01_Max.pdf')).toBe('2024 01 01 Max');
|
||||
});
|
||||
|
||||
it('collapses multiple separators', () => {
|
||||
expect(bulkTitleFromFilename('foo__bar--baz.pdf')).toBe('foo bar baz');
|
||||
});
|
||||
|
||||
it('strips extension', () => {
|
||||
expect(bulkTitleFromFilename('document.pdf')).toBe('document');
|
||||
});
|
||||
});
|
||||
|
||||
describe('stripExtension', () => {
|
||||
it('removes the extension', () => {
|
||||
expect(stripExtension('document.pdf')).toBe('document');
|
||||
|
||||
@@ -81,3 +81,7 @@ export function parseFilename(filename: string): FilenameParseResult {
|
||||
export function stripExtension(filename: string): string {
|
||||
return filename.replace(/\.[^/.]+$/, '');
|
||||
}
|
||||
|
||||
export function bulkTitleFromFilename(filename: string): string {
|
||||
return stripExtension(filename).replace(/[_-]+/g, ' ').trim();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user