| 1 | import { isNumber, range, uniq } from '@antfu/utils' |
| 2 | |
| 3 | export * from './timesplit' |
| 4 | |
| 5 | const RE_ASPECT_RATIO_SEPARATOR = /[:/x|]/ |
| 6 | |
| 7 | /** |
| 8 | * 1,3-5,8 => [1, 3, 4, 5, 8] |
| 9 | */ |
| 10 | export function parseRangeString(total: number, rangeStr?: string) { |
| 11 | if (!rangeStr || rangeStr === 'all' || rangeStr === '*') |
| 12 | return range(1, total + 1) |
| 13 | |
| 14 | if (rangeStr === 'none') |
| 15 | return [] |
| 16 | |
| 17 | const indexes: number[] = [] |
| 18 | for (const part of rangeStr.split(/[,;]/g)) { |
| 19 | if (!part.includes('-')) { |
| 20 | indexes.push(+part) |
| 21 | } |
| 22 | else { |
| 23 | const [start, end] = part.split('-', 2) |
| 24 | indexes.push( |
| 25 | ...range(+start, !end ? (total + 1) : (+end + 1)), |
| 26 | ) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | return uniq(indexes).filter(i => i <= total).sort((a, b) => a - b) |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Accepts `16/9` `1:1` `3x4` |
| 35 | */ |
| 36 | export function parseAspectRatio(str: string | number) { |
| 37 | if (isNumber(str)) |
| 38 | return str |
| 39 | if (!Number.isNaN(+str)) |
| 40 | return +str |
| 41 | const [wStr = '', hStr = ''] = str.split(RE_ASPECT_RATIO_SEPARATOR) |
| 42 | const w = Number.parseFloat(wStr.trim()) |
| 43 | const h = Number.parseFloat(hStr.trim()) |
| 44 | |
| 45 | if (Number.isNaN(w) || Number.isNaN(h) || h === 0) |
| 46 | throw new Error(`Invalid aspect ratio "${str}"`) |
| 47 | |
| 48 | return w / h |
| 49 | } |
| 50 |