| 3 | import {MetadataEntry, Metadata} from './Metadata.js'; |
| 4 | |
| 5 | export class MetadataProcessor { |
| 6 | /** |
| 7 | * Converts a raw metadata string to a structured entry |
| 8 | */ |
| 9 | static parseMetadataEntry(entry: string): MetadataEntry { |
| 10 | const colonIndex = entry.indexOf(':'); |
| 11 | if (colonIndex === -1) { |
| 12 | throw new Error(`Invalid metadata format: ${entry}`); |
| 13 | } |
| 14 | |
| 15 | return { |
| 16 | key: entry.substring(0, colonIndex).trim(), |
| 17 | value: entry.substring(colonIndex + 1).trim() |
| 18 | }; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Formats a metadata entry into a string |
| 23 | */ |
| 24 | static formatMetadataEntry(key: string, value: string | string[] | unknown): string { |
| 25 | if (Array.isArray(value)) { |
| 26 | return `${key}: ${value.join(', ')}`; |
| 27 | } |
| 28 | return `${key}: ${String(value)}`; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Processes and validates metadata entries |
| 33 | */ |
| 34 | static validateMetadata(metadata: Metadata): boolean { |
| 35 | try { |
| 36 | metadata.forEach(entry => this.parseMetadataEntry(entry)); |
| 37 | return true; |
| 38 | } catch (error) { |
| 39 | return false; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Merges multiple metadata arrays, removing duplicates |
| 45 | */ |
| 46 | static mergeMetadata(...metadataArrays: Metadata[]): Metadata { |
| 47 | const uniqueEntries = new Set<string>(); |
| 48 | |
| 49 | metadataArrays.forEach(metadata => { |
| 50 | metadata.forEach(entry => uniqueEntries.add(entry)); |
| 51 | }); |
| 52 | |
| 53 | return Array.from(uniqueEntries); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Filters metadata entries by key |
| 58 | */ |
| 59 | static filterByKey(metadata: Metadata, key: string): Metadata { |
| 60 | return metadata.filter(entry => { |
| 61 | const parsed = this.parseMetadataEntry(entry); |
| 62 | return parsed.key === key; |
nothing calls this directly
no outgoing calls
no test coverage detected