feat: add converter registry

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-30 09:06:12 +02:00
co-authored by Claude Sonnet 5
parent 4b7ad9ef4e
commit aeebc47379
2 changed files with 56 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
let entries = new Map();
function key(sourceFormat, targetFormat) {
return `${sourceFormat}->${targetFormat}`;
}
export function register({ family, sourceFormat, targetFormat, convert }) {
entries.set(key(sourceFormat, targetFormat), { family, convert });
}
export function resolve(sourceFormat, targetFormat) {
return entries.get(key(sourceFormat, targetFormat)) ?? null;
}
export function listTargetFormats(sourceFormat) {
const prefix = `${sourceFormat}->`;
return [...entries.keys()]
.filter((k) => k.startsWith(prefix))
.map((k) => k.slice(prefix.length));
}
export function _resetForTests() {
entries = new Map();
}
+32
View File
@@ -0,0 +1,32 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { register, resolve, listTargetFormats, _resetForTests } from '../../src/converters/registry.js';
beforeEach(() => {
_resetForTests();
});
describe('converter registry', () => {
it('resolves a registered source/target pair', () => {
const convert = async () => {};
register({ family: 'image', sourceFormat: 'png', targetFormat: 'webp', convert });
const entry = resolve('png', 'webp');
expect(entry.family).toBe('image');
expect(entry.convert).toBe(convert);
});
it('returns null for an unregistered pair', () => {
expect(resolve('png', 'made-up')).toBeNull();
});
it('lists all target formats registered for a source format', () => {
register({ family: 'image', sourceFormat: 'png', targetFormat: 'webp', convert: async () => {} });
register({ family: 'image', sourceFormat: 'png', targetFormat: 'jpg', convert: async () => {} });
register({ family: 'document', sourceFormat: 'docx', targetFormat: 'pdf', convert: async () => {} });
expect(listTargetFormats('png').sort()).toEqual(['jpg', 'webp']);
expect(listTargetFormats('docx')).toEqual(['pdf']);
expect(listTargetFormats('unknown-format')).toEqual([]);
});
});