src/read-order.ts (671 bytes)
1 // The order in which the reader reads the pages of a book. Pure. 2 3 /** 4 * The next page to read: the current page, then the pages after it up to 5 * the end, then the pages before it, nearest first. The narration goes on 6 * from the current page, so the pages after it are necessary first. 7 * `pending` holds the pages that are not read yet. -1 when it is empty. 8 */ 9 export function nextPage(pending: ReadonlySet<number>, current: number): number { 10 let after = Infinity; 11 let before = -1; 12 for (const page of pending) { 13 if (page >= current) after = Math.min(after, page); 14 else before = Math.max(before, page); 15 } 16 return after < Infinity ? after : before; 17 }