All files / core/domain VocabularyItem.ts

100% Statements 13/13
100% Branches 15/15
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                  6x         6x 2x   4x 1x   3x 1x     2x                   22x 22x 4x   18x 18x            
/**
 * VocabularyItem Entity
 * Represents a single vocabulary word with its translation and usage example.
 * This is a core domain entity with no external dependencies.
 */
export interface VocabularyItem {
  /** The English word */
  word: string
  /** Portuguese description/translation */
  description: string
  /** Usage example in Portuguese */
  useCase: string
}
 
/**
 * Factory function to create a VocabularyItem with validation
 */
export function createVocabularyItem(
  word: string,
  description: string,
  useCase: string
): VocabularyItem {
  if (!word || word.trim().length === 0) {
    throw new Error('Word cannot be empty')
  }
  if (!description || description.trim().length === 0) {
    throw new Error('Description cannot be empty')
  }
  if (!useCase || useCase.trim().length === 0) {
    throw new Error('Use case cannot be empty')
  }
 
  return {
    word: word.trim(),
    description: description.trim(),
    useCase: useCase.trim(),
  }
}
 
/**
 * Type guard to check if an object is a valid VocabularyItem
 */
export function isVocabularyItem(obj: unknown): obj is VocabularyItem {
  if (typeof obj !== 'object' || obj === null) {
    return false
  }
  const item = obj as Record<string, unknown>
  return (
    typeof item.word === 'string' &&
    typeof item.description === 'string' &&
    typeof item.useCase === 'string'
  )
}