-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use helper function to parse pages string
- Loading branch information
Showing
3 changed files
with
123 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Takes a string like "1,2,3" or "1-3" and returns an Array of numbers. | ||
* | ||
* @param {string} pages | ||
* | ||
* @example ```js | ||
* parsePagesString('2') // [2] | ||
* parsePagesString('1,2,3') // [1,2,3] | ||
* parsePagesString('1-3') // [1,2,3] | ||
* parsePagesString('1to3') // [1,2,3] | ||
* parsePagesString('1 to 3') // [1,2,3] | ||
* parsePagesString('10,1-3') // [10,1,2,3] | ||
* parsePagesString('9,1-3,5-7') // [9,1,2,3,5,6,7] | ||
* ``` | ||
*/ | ||
export function parsePagesString (pages) { | ||
const throwError = () => { | ||
throw new Error([ | ||
'Invalid parameter "pages".', | ||
'Must be a string like "1,2,3" or "1-3" or "1to3"', | ||
`Was "${pages}" instead.` | ||
].join(' ')) | ||
} | ||
|
||
const isRangeString = (rangeString) => { | ||
return rangeString.includes('-') || rangeString.toLowerCase().includes('to') | ||
} | ||
|
||
const parseRange = (rangeString) => { | ||
const [start, end] = rangeString.split(/-|to/).map(s => typeof s === 'string' ? parseInt(s.trim()) : s) | ||
return Array.from({ length: end - start + 1 }, (_, i) => start + i) | ||
} | ||
|
||
if (typeof pages !== 'string') { | ||
throwError() | ||
} else if (!pages.trim().replace(/ /g, '').match(/^(\d+|\d+-\d+|\d+to\d+)(,(\d+|\d+-\d+|\d+to\d+))*$/)) { | ||
// string does not fit the expected pattern | ||
throwError() | ||
} else if (pages.trim().match(/^\d+$/)) { | ||
// string consists of a single page-number | ||
return [parseInt(pages.trim())] | ||
} else if (pages.trim().includes(',')) { | ||
// string consists od a list of page-numbers and/or ranges | ||
return pages.split(',').flatMap(s => isRangeString(s) ? parseRange(s) : parseInt(s)) | ||
} else if (isRangeString(pages)) { | ||
// string consists of a single range | ||
return parseRange(pages) | ||
} | ||
|
||
throwError() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { parsePagesString } from '../parsePagesString' | ||
|
||
describe('parsePagesString', () => { | ||
test('should parse lists', () => { | ||
expect(parsePagesString('2')).toStrictEqual([2]) | ||
expect(parsePagesString(' 2 ')).toStrictEqual([2]) | ||
expect(parsePagesString('1,2,3')).toStrictEqual([1, 2, 3]) | ||
expect(parsePagesString(' 1,2,3 ')).toStrictEqual([1, 2, 3]) | ||
expect(parsePagesString(' 1, 2, 3 ')).toStrictEqual([1, 2, 3]) | ||
expect(parsePagesString(' 1 , 2 , 3 ')).toStrictEqual([1, 2, 3]) | ||
expect(parsePagesString(' 2,4,6 ')).toStrictEqual([2, 4, 6]) | ||
}) | ||
|
||
test('should parse ranges', () => { | ||
expect(parsePagesString('1-3')).toStrictEqual([1, 2, 3]) | ||
expect(parsePagesString(' 1-3 ')).toStrictEqual([1, 2, 3]) | ||
expect(parsePagesString(' 1 - 3 ')).toStrictEqual([1, 2, 3]) | ||
expect(parsePagesString('2-6')).toStrictEqual([2, 3, 4, 5, 6]) | ||
}) | ||
|
||
test('should parse range with "to"', () => { | ||
expect(parsePagesString('1to3')).toStrictEqual([1, 2, 3]) | ||
expect(parsePagesString(' 1to3 ')).toStrictEqual([1, 2, 3]) | ||
expect(parsePagesString(' 1 to 3 ')).toStrictEqual([1, 2, 3]) | ||
expect(parsePagesString('2 to 6')).toStrictEqual([2, 3, 4, 5, 6]) | ||
}) | ||
|
||
test('should parse combined lists and ranges', () => { | ||
expect(parsePagesString('1-3,4')).toStrictEqual([1, 2, 3, 4]) | ||
expect(parsePagesString(' 1-3 , 4 , 5')).toStrictEqual([1, 2, 3, 4, 5]) | ||
expect(parsePagesString(' 1 - 3, 5-7 ')).toStrictEqual([1, 2, 3, 5, 6, 7]) | ||
expect(parsePagesString(' 9,8,1 - 3, 5-6 ,8,9')).toStrictEqual([9, 8, 1, 2, 3, 5, 6, 8, 9]) | ||
expect(parsePagesString('2-6,8')).toStrictEqual([2, 3, 4, 5, 6, 8]) | ||
expect(parsePagesString('1,3-5,7')).toStrictEqual([1, 3, 4, 5, 7]) | ||
expect(parsePagesString('11-13,5,8,16-18,14')).toStrictEqual([11, 12, 13, 5, 8, 16, 17, 18, 14]) | ||
}) | ||
|
||
test('invalid', () => { | ||
expect(() => parsePagesString()).toThrow() | ||
expect(() => parsePagesString(null)).toThrow() | ||
expect(() => parsePagesString({})).toThrow() | ||
|
||
expect(() => parsePagesString('')).toThrow() | ||
expect(() => parsePagesString('-1to-3')).toThrow() | ||
expect(() => parsePagesString('1--3')).toThrow() | ||
expect(() => parsePagesString('1 until 3')).toThrow() | ||
expect(() => parsePagesString('1-')).toThrow() | ||
expect(() => parsePagesString('10e3')).toThrow() | ||
}) | ||
}) |