All files / slidev deck-id.ts

100% Statements 9/9
100% Branches 0/0
100% Functions 4/4
100% Lines 9/9

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                    1x 1106x 1106x   1106x   3318x 13272x                               1x 25x 25x    
/**
 * Generate a unique deck ID in the format: xxxx-xxxx-xxxx (12 characters total)
 * Uses lowercase alphanumeric characters (a-z, 0-9)
 *
 * @returns A unique deck ID string
 *
 * @example
 * const deckId = generateDeckId();
 * // Returns something like: "a3b2-c5d9-e7f1"
 */
export function generateDeckId(): string {
  const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
  const segments = [4, 4, 4];
 
  return segments
    .map(len =>
      Array.from({ length: len }, () =>
        chars[Math.floor(Math.random() * chars.length)]
      ).join('')
    )
    .join('-');
}
 
/**
 * Validate if a string matches the deck ID format
 *
 * @param deckId - The deck ID string to validate
 * @returns True if the deck ID matches the expected format
 *
 * @example
 * isValidDeckId("a3b2-c5d9-e7f1"); // true
 * isValidDeckId("invalid"); // false
 */
export function isValidDeckId(deckId: string): boolean {
  const pattern = /^[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}$/;
  return pattern.test(deckId);
}