feat: add converter registry
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -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();
|
||||
}
|
||||
@@ -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([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user