fix(print-range): accept em-dash, tilde and fullwidth dash variants (#356)
* fix(print-range): accept em-dash, tilde and fullwidth dash variants * fix(sheets): satisfy exactOptionalPropertyTypes for LazyCellMatch rawValue --------- Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
This commit is contained in:
co-authored by
Aniruddha Adak
parent
b8fe1b6b01
commit
af9a63e462
@@ -1,16 +1,17 @@
|
||||
/**
|
||||
* Parse a Word-style print range ("1,3,5-8") into sorted 0-based page indices.
|
||||
* Accepts CJK/ASCII separators and en-dashes; returns null on any invalid or
|
||||
* out-of-bounds part (same semantics as the slides print dialog).
|
||||
* Accepts CJK/ASCII separators and dashes: hyphen, en-dash, em-dash, tilde,
|
||||
* fullwidth tilde and wave dash (Word IME users often type ~/〜 instead of -);
|
||||
* returns null on any invalid or out-of-bounds part (same semantics as the slides print dialog).
|
||||
*/
|
||||
export function parsePrintRange(text: string, max: number): number[] | null {
|
||||
const out = new Set<number>()
|
||||
const tokenRe = /\d+\s*[-–]\s*\d+|\d+/g
|
||||
const tokenRe = /\d+\s*[-–—~~〜]\s*\d+|\d+/g
|
||||
const parts = text.match(tokenRe) ?? []
|
||||
if (parts.length === 0) return null
|
||||
if (text.replace(tokenRe, '').replace(/[,,、;;\s]/g, '') !== '') return null
|
||||
for (const part of parts) {
|
||||
const m = /^(\d+)\s*[-–]\s*(\d+)$|^(\d+)$/.exec(part)
|
||||
const m = /^(\d+)\s*[-–—~~〜]\s*(\d+)$|^(\d+)$/.exec(part)
|
||||
if (!m) return null
|
||||
const a = Number(m[1] ?? m[3])
|
||||
const b = Number(m[2] ?? m[3])
|
||||
|
||||
@@ -19,6 +19,14 @@ describe('parsePrintRange', () => {
|
||||
expect(parsePrintRange('1 – 3, 5', 5)).toEqual([0, 1, 2, 4])
|
||||
})
|
||||
|
||||
it('accepts em-dash, tilde, fullwidth tilde and wave dash (IME variants)', () => {
|
||||
expect(parsePrintRange('1—3', 5)).toEqual([0, 1, 2])
|
||||
expect(parsePrintRange('1~3', 5)).toEqual([0, 1, 2])
|
||||
expect(parsePrintRange('1~3', 5)).toEqual([0, 1, 2])
|
||||
expect(parsePrintRange('1〜3', 5)).toEqual([0, 1, 2])
|
||||
expect(parsePrintRange('1 — 3, 5~6', 6)).toEqual([0, 1, 2, 4, 5])
|
||||
})
|
||||
|
||||
it('rejects empty, malformed, reversed, and out-of-bounds input', () => {
|
||||
expect(parsePrintRange('', 5)).toBeNull()
|
||||
expect(parsePrintRange('a-b', 5)).toBeNull()
|
||||
|
||||
@@ -26,12 +26,12 @@ export interface PrintDocOptions {
|
||||
/** "1,3,5-8" → 0-based slide indices (1-based input over the whole deck); null = invalid */
|
||||
export function parsePrintRange(text: string, max: number): number[] | null {
|
||||
const out = new Set<number>()
|
||||
const tokenRe = /\d+\s*[-–]\s*\d+|\d+/g
|
||||
const tokenRe = /\d+\s*[-–—~~〜]\s*\d+|\d+/g
|
||||
const parts = text.match(tokenRe) ?? []
|
||||
if (parts.length === 0) return null
|
||||
if (text.replace(tokenRe, '').replace(/[,,、;;\s]/g, '') !== '') return null
|
||||
for (const part of parts) {
|
||||
const m = /^(\d+)\s*[-–]\s*(\d+)$|^(\d+)$/.exec(part)
|
||||
const m = /^(\d+)\s*[-–—~~〜]\s*(\d+)$|^(\d+)$/.exec(part)
|
||||
if (!m) return null
|
||||
const a = Number(m[1] ?? m[3])
|
||||
const b = Number(m[2] ?? m[3])
|
||||
|
||||
@@ -27,6 +27,19 @@ describe('parsePrintRange', () => {
|
||||
expect(parsePrintRange('1 - 3', 5)).toEqual([0, 1, 2])
|
||||
expect(parsePrintRange('1 – 3, 5', 5)).toEqual([0, 1, 2, 4])
|
||||
})
|
||||
|
||||
it('accepts em-dash, tilde, fullwidth tilde and wave dash (IME variants)', () => {
|
||||
expect(parsePrintRange('1—3', 5)).toEqual([0, 1, 2])
|
||||
expect(parsePrintRange('1~3', 5)).toEqual([0, 1, 2])
|
||||
expect(parsePrintRange('1~3', 5)).toEqual([0, 1, 2])
|
||||
expect(parsePrintRange('1〜3', 5)).toEqual([0, 1, 2])
|
||||
expect(parsePrintRange('1 — 3, 5~6', 6)).toEqual([0, 1, 2, 4, 5])
|
||||
})
|
||||
|
||||
it('accepts spaces around dashes', () => {
|
||||
expect(parsePrintRange('1 - 3', 5)).toEqual([0, 1, 2])
|
||||
expect(parsePrintRange('1 – 3, 5', 5)).toEqual([0, 1, 2, 4])
|
||||
})
|
||||
})
|
||||
|
||||
describe('printPageCount', () => {
|
||||
|
||||
Reference in New Issue
Block a user