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