forked from artiebits/pdf-to-printer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
getPagesNumbers
method for parse pages
arg
- Loading branch information
1 parent
62ddbd9
commit 1648e70
Showing
6 changed files
with
109 additions
and
3 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
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,49 @@ | ||
import getPagesNumbers from "./get-pages-numbers"; | ||
|
||
type mockedPagesArgsType = { | ||
args: Parameters<typeof getPagesNumbers>; | ||
expected: ReturnType<typeof getPagesNumbers> | typeof Error; | ||
}[]; | ||
|
||
const mockedPagesArgs = [ | ||
{ | ||
args: ["1-3,5,7-9"], | ||
expected: [1, 2, 3, 5, 7, 8, 9], | ||
}, | ||
{ | ||
args: ["1,2,3"], | ||
expected: [1, 2, 3], | ||
}, | ||
{ | ||
args: ["1-3"], | ||
expected: [1, 2, 3], | ||
}, | ||
{ | ||
args: ["1-3,5"], | ||
expected: [1, 2, 3, 5], | ||
}, | ||
{ | ||
args: ["5,1-3"], | ||
expected: [5, 1, 2, 3], | ||
}, | ||
{ | ||
args: ["qwe"], | ||
expected: Error, | ||
}, | ||
{ | ||
args: ["1-3,5,7-9,1e+3"], | ||
expected: Error, | ||
}, | ||
] as mockedPagesArgsType; | ||
|
||
describe("getPagesNumbers", () => { | ||
mockedPagesArgs.forEach(({ args, expected }) => { | ||
it(`should return ${expected} for ${JSON.stringify(args)}`, () => { | ||
if (expected === Error) { | ||
expect(() => getPagesNumbers(...args)).toThrow(); | ||
} else { | ||
expect(getPagesNumbers(...args)).toEqual(expected); | ||
} | ||
}); | ||
}); | ||
}); |
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,30 @@ | ||
export default function getPagesNumbers(pagesString: string) { | ||
// parse pages string to array of numbers | ||
const pagesArray = pagesString.split(","); | ||
const pagesNumbers: number[] = []; | ||
pagesArray.forEach((page) => { | ||
if (page.includes("-")) { | ||
const [start, end] = page.split("-"); | ||
if (!isNumber(start) || !isNumber(end)) { | ||
throw new Error(`Invalid page numbers: ${page}`); | ||
} | ||
|
||
const [parsedStart, parsedEnd] = [parseInt(start), parseInt(end)]; | ||
for (let i = parsedStart; i <= parsedEnd; i++) { | ||
pagesNumbers.push(i); | ||
} | ||
} else { | ||
const trimmedPage = page.trim(); | ||
if (!isNumber(trimmedPage)) { | ||
throw new Error(`Invalid page number: ${page}`); | ||
} | ||
pagesNumbers.push(parseInt(trimmedPage)); | ||
} | ||
}); | ||
|
||
return pagesNumbers; | ||
} | ||
|
||
function isNumber(value: string | number): boolean { | ||
return value != null && /^\d+$/.test(value.toString()); | ||
} |
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