this repo has no description
1import type { GeneratorConfig } from './types.ts'
2
3/**
4 * Generates tagged strings in the correct format using configured delimiters.
5 * Provides a minimal reference implementation for producing tagged strings
6 * that can be parsed by TaggedStringParser.
7 */
8export class TaggedStringGenerator {
9 private readonly openDelimiter: string
10 private readonly closeDelimiter: string
11 private readonly typeSeparator: string
12
13 /**
14 * Creates a new TaggedStringGenerator with optional configuration.
15 * Defaults match the parser defaults: [ ] :
16 *
17 * @param config - Optional configuration for delimiters and separator
18 * @throws Error if delimiters are empty or identical
19 */
20 constructor(config?: GeneratorConfig) {
21 this.openDelimiter = config?.openDelimiter ?? '['
22 this.closeDelimiter = config?.closeDelimiter ?? ']'
23 this.typeSeparator = config?.typeSeparator ?? ':'
24
25 this.validateConfig()
26 }
27
28 /**
29 * Validates the configuration.
30 * Throws an error if delimiters are invalid.
31 *
32 * @throws Error if openDelimiter or closeDelimiter is empty
33 * @throws Error if openDelimiter equals closeDelimiter
34 */
35 private validateConfig(): void {
36 if (this.openDelimiter === '') {
37 throw new Error('openDelimiter cannot be empty')
38 }
39 if (this.closeDelimiter === '') {
40 throw new Error('closeDelimiter cannot be empty')
41 }
42 if (this.openDelimiter === this.closeDelimiter) {
43 throw new Error('openDelimiter and closeDelimiter must be different')
44 }
45 }
46
47 /**
48 * Converts any value to its string representation.
49 * Handles null and undefined explicitly.
50 *
51 * @param value - The value to convert
52 * @returns String representation of the value
53 */
54 private valueToString(value: unknown): string {
55 return String(value)
56 }
57
58 /**
59 * Generates a single tagged entity from a type and value.
60 *
61 * @param type - The entity type
62 * @param value - The entity value (will be converted to string)
63 * @returns Formatted tag string
64 * @example
65 * ```ts
66 * const generator = new TaggedStringGenerator()
67 * generator.tag('operation', 'deploy') // Returns: [operation:deploy]
68 * ```
69 */
70 tag(type: string, value: unknown): string {
71 const stringValue = this.valueToString(value)
72 return `${this.openDelimiter}${type}${this.typeSeparator}${stringValue}${this.closeDelimiter}`
73 }
74
75 /**
76 * Convenience method for embedding a tag in a message.
77 * Concatenates the message with the generated tag.
78 *
79 * @param message - The message to prepend
80 * @param type - The entity type
81 * @param value - The entity value (will be converted to string)
82 * @returns Message with embedded tag
83 * @example
84 * ```ts
85 * const generator = new TaggedStringGenerator()
86 * generator.embed('Starting ', 'operation', 'deploy') // Returns: Starting [operation:deploy]
87 * ```
88 */
89 embed(message: string, type: string, value: unknown): string {
90 return message + this.tag(type, value)
91 }
92}