|
| 1 | +/** |
| 2 | + * Semantic key generation utility |
| 3 | + * Generates kebab-case keys with hash suffixes to ensure uniqueness |
| 4 | + */ |
| 5 | + |
| 6 | +import { createHash } from 'crypto'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Generate a semantic key from title with hash suffix for uniqueness |
| 10 | + * |
| 11 | + * @param title - The title to generate a key from |
| 12 | + * @param hashInput - Additional input for hash generation (e.g., description, timestamp) |
| 13 | + * @returns A semantic key in kebab-case format with hash suffix |
| 14 | + * |
| 15 | + * @example |
| 16 | + * generateSemanticKey("Fix Authentication Bug") |
| 17 | + * // Returns: "fix-authentication-bug-a1b2c3d4" |
| 18 | + * |
| 19 | + * generateSemanticKey("Test Long Title", "some additional context") |
| 20 | + * // Returns: "test-long-title-e5f6g7h8" |
| 21 | + */ |
| 22 | +export function generateSemanticKey(title: string, hashInput?: string): string { |
| 23 | + // 1. Create semantic base from title |
| 24 | + const semanticBase = title |
| 25 | + .toLowerCase() |
| 26 | + .replace(/[^a-z0-9\s]/g, '') // Remove special characters |
| 27 | + .replace(/\s+/g, '-') // Replace spaces with hyphens |
| 28 | + .replace(/^-+|-+$/g, '') // Remove leading/trailing hyphens |
| 29 | + .substring(0, 40); // Limit base to reasonable length |
| 30 | + |
| 31 | + // 2. Generate hash suffix for uniqueness |
| 32 | + const hashSource = hashInput ? `${title}-${hashInput}-${Date.now()}` : `${title}-${Date.now()}`; |
| 33 | + |
| 34 | + const hash = createHash('sha256').update(hashSource).digest('hex').substring(0, 8); // Use first 8 characters |
| 35 | + |
| 36 | + // 3. Combine semantic base with hash suffix |
| 37 | + return `${semanticBase}-${hash}`; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Generate a key from a title with additional context for uniqueness |
| 42 | + * |
| 43 | + * @param title - The main title |
| 44 | + * @param type - The entry type (feature, bugfix, etc.) |
| 45 | + * @param description - Optional description for additional context |
| 46 | + * @returns A semantic key with hash suffix |
| 47 | + */ |
| 48 | +export function generateDevlogKey(title: string, type: string, description?: string): string { |
| 49 | + const contextInput = description ? `${type}-${description}` : type; |
| 50 | + |
| 51 | + return generateSemanticKey(title, contextInput); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Validate that a key meets the required format |
| 56 | + * |
| 57 | + * @param key - The key to validate |
| 58 | + * @returns True if key is valid |
| 59 | + */ |
| 60 | +export function isValidKey(key: string): boolean { |
| 61 | + // Key should be kebab-case with optional hash suffix |
| 62 | + const keyPattern = /^[a-z0-9]+(-[a-z0-9]+)*$/; |
| 63 | + return keyPattern.test(key) && key.length <= 255; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Extract the semantic part (without hash) from a generated key |
| 68 | + * |
| 69 | + * @param key - The generated key |
| 70 | + * @returns The semantic part of the key |
| 71 | + */ |
| 72 | +export function extractSemanticPart(key: string): string { |
| 73 | + // Remove the last segment if it looks like a hash (8 hex chars) |
| 74 | + const parts = key.split('-'); |
| 75 | + const lastPart = parts[parts.length - 1]; |
| 76 | + |
| 77 | + if (lastPart.length === 8 && /^[a-f0-9]+$/.test(lastPart)) { |
| 78 | + return parts.slice(0, -1).join('-'); |
| 79 | + } |
| 80 | + |
| 81 | + return key; |
| 82 | +} |
0 commit comments