All files / molecules/Accordion/mobile/Components/Utils Utils.ts

44.44% Statements 8/18
33.33% Branches 5/15
50% Functions 3/6
44.44% Lines 8/18

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68                  20x 20x                                           2x 20x                         20x   20x 20x                               20x    
import type { HorizontalDirection, VerticalDirection } from '../../../Accordion.types';
import type { AccordionHeaderPosition } from '../../../Accordion.types';
 
/**
 * Get the index from an id.
 * @param id — the id to get the index from
 * @returns the index
 */
export function getIndexFromId(id: string) {
  const index = parseInt(id.split('-').pop() || '0');
  return index;
}
 
/**
 * Extract text classes from a string of CSS classes.
 * @param cnStr — the string of CSS classes to extract text classes from
 * @returns the text classes as a string
 */
export function extractTextClasses(cnStr?: string) {
  return cnStr
    ? cnStr
        .split(' ')
        .filter((cls) => cls.startsWith('text-') || cls.startsWith('font-'))
        .join(' ')
    : '';
}
 
/**
 * Determine layout orientation from header position.
 * - 'horizontal' if header is on left/right.
 * - 'vertical' if header is on top/bottom.
 */
export const getOrientation = (pos: AccordionHeaderPosition): 'horizontal' | 'vertical' =>
  pos === 'left' || pos === 'right' ? 'horizontal' : 'vertical';
 
/**
 * Map a header position to its own direction type.
 * @param pos — one of 'top'|'bottom'|'left'|'right'
 * @returns the same literal, but typed as HorizontalDirection or VerticalDirection
 */
export function getDirection(
  pos: AccordionHeaderPosition
): HorizontalDirection | VerticalDirection {
  // Prepare for exhaustive checking if no case matches
  let returnValue: HorizontalDirection | VerticalDirection | never;
 
  switch (pos) {
    case 'top':
      returnValue = 'top';
      break;
    case 'bottom':
      returnValue = 'bottom';
      break;
    case 'left':
      returnValue = 'left';
      break;
    case 'right':
      returnValue = 'right';
      break;
    default:
      returnValue = ((): never => {
        throw new Error(`Unhandled position: ${pos}`);
      })();
  }
 
  return returnValue;
}