( pages: string, )
| 14 | * Pages are 1-indexed. |
| 15 | */ |
| 16 | export function parsePDFPageRange( |
| 17 | pages: string, |
| 18 | ): { firstPage: number; lastPage: number } | null { |
| 19 | const trimmed = pages.trim() |
| 20 | if (!trimmed) { |
| 21 | return null |
| 22 | } |
| 23 | |
| 24 | // "N-" open-ended range |
| 25 | if (trimmed.endsWith('-')) { |
| 26 | const first = parseInt(trimmed.slice(0, -1), 10) |
| 27 | if (isNaN(first) || first < 1) { |
| 28 | return null |
| 29 | } |
| 30 | return { firstPage: first, lastPage: Infinity } |
| 31 | } |
| 32 | |
| 33 | const dashIndex = trimmed.indexOf('-') |
| 34 | if (dashIndex === -1) { |
| 35 | // Single page: "5" |
| 36 | const page = parseInt(trimmed, 10) |
| 37 | if (isNaN(page) || page < 1) { |
| 38 | return null |
| 39 | } |
| 40 | return { firstPage: page, lastPage: page } |
| 41 | } |
| 42 | |
| 43 | // Range: "1-10" |
| 44 | const first = parseInt(trimmed.slice(0, dashIndex), 10) |
| 45 | const last = parseInt(trimmed.slice(dashIndex + 1), 10) |
| 46 | if (isNaN(first) || isNaN(last) || first < 1 || last < 1 || last < first) { |
| 47 | return null |
| 48 | } |
| 49 | return { firstPage: first, lastPage: last } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Check if PDF reading is supported with the current model. |
no outgoing calls
no test coverage detected