All files index.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 2/2
100% Lines 5/5

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            10x     1x 10x 10x   2x    
export interface CaseBlock<A, B> extends Readonly<{
  case: A
  do: () => B
}> {}
 
function matchesScrutinee<A, B> (scrutinee: A, caseBlock: CaseBlock<A, B>): boolean {
  return JSON.stringify(caseBlock.case) === JSON.stringify(scrutinee)
}
 
export function patternMatch<A, B> (scrutinee: A, cases: ReadonlyArray<CaseBlock<A, B>>, defaultBlock: () => B): B {
  for (const currentCase of cases) {
    if (matchesScrutinee(scrutinee, currentCase)) return currentCase.do()
  }
  return defaultBlock()
}