All files / shared ranking.ts

100% Statements 17/17
100% Branches 4/4
100% Functions 9/9
100% Lines 12/12

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                          8x   8x 17x   8x 45x 12x 16x 16x   12x 45x     8x 55x  
export type Arm = 'fixed' | 'adaptive' | 'holdout'
 
export interface RankedItem {
  item: string
  share: number
}
 
export interface Ranking {
  mode: Arm
  reason: string
  items: readonly RankedItem[]
}
 
export const fixedRanking = (reason: string): Ranking => ({ mode: 'fixed', reason, items: [] })
 
export const servedOrder = (ranking: Ranking): string[] =>
  ranking.mode === 'fixed' ? [] : ranking.items.map((ranked) => ranked.item)
 
export const mergeOrder = <T>(fixed: readonly T[], served: readonly string[], key: (item: T) => string): T[] => {
  const known = new Map(fixed.map((item) => [key(item), item]))
  const first = [...new Set(served)].flatMap((name) => {
    const item = known.get(name)
    return item === undefined ? [] : [item]
  })
  const placed = new Set(first.map(key))
  return [...first, ...fixed.filter((item) => !placed.has(key(item)))]
}
 
export const rankedOrder = (ranking: Ranking, fixed: readonly string[]) =>
  mergeOrder(fixed, servedOrder(ranking), (item) => item)