All files / src/domain sort-by-id.ts

100% Statements 21/21
86.66% Branches 13/15
84.61% Functions 11/13
100% Lines 21/21

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 2214x 1x 1x 1x 14x 14x 1x 1x 11x 11x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 14x  
export type IdentifiedItem = { id: string }
 
/** Primary: id (v0, v4-S2, EPIC-05, US-0094). Numeric segments sort naturally. */
export function compareByIdAsc(a: IdentifiedItem, b: IdentifiedItem): number {
  return a.id.localeCompare(b.id, undefined, { numeric: true })
}
 
export function sortByIdAsc<T extends IdentifiedItem>(items: readonly T[]): T[] {
  return [...items].sort(compareByIdAsc)
}
 
/** Kanban: newest US id first (US-0159 before US-0158). */
export function compareByIdDesc(a: IdentifiedItem, b: IdentifiedItem): number {
  return compareByIdAsc(b, a)
}
 
export function sortStoriesById<
  T extends IdentifiedItem & { id: string },
>(stories: T[]): T[] {
  return [...stories].sort(compareByIdDesc)
}