From 1648e70b418d2bbe313e2889fdd7f9ed672f51da Mon Sep 17 00:00:00 2001 From: websmithcode Date: Thu, 28 Dec 2023 09:29:59 +0300 Subject: [PATCH] Add `getPagesNumbers` method for parse `pages` arg --- CHANGELOG.md | 8 ++- README.md | 20 ++++++++ package.json | 4 +- .../get-pages-numbers.spec.ts | 49 +++++++++++++++++++ src/get-pages-numbers/get-pages-numbers.ts | 30 ++++++++++++ src/index.ts | 1 + 6 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 src/get-pages-numbers/get-pages-numbers.spec.ts create mode 100644 src/get-pages-numbers/get-pages-numbers.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 99b56ff..f07149a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ # Changelog -## [5.7.0](https://www.github.com/websmithcode/pdf-to-printer-modern/compare/v5.6.0...v5.7.0) (2023-12-28) +## [5.8.0](https://www.github.com/websmithcode/pdf-to-printer-modern/compare/v5.7.0...v5.8.0) (2023-12-28) + +### Features + +- Add `getPagesNumbers` method for parse `pages` arg + +## [5.8.0](https://www.github.com/websmithcode/pdf-to-printer-modern/compare/v5.7.0...v5.8.0) (2023-12-28) ### Bug Fixes diff --git a/README.md b/README.md index 64e2689..1f1c322 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ If you rely on this package, please consider supporting it. Maintaining an open - [`.print(pdf[, options]) => Promise`](#printpdf-options--promisevoid) - [`.getPrinters() => Promise`](#getprinters--promiseprinter) - [`.getDefaultPrinter() => Promise`](#getdefaultprinter--promiseprinter--null) + - [`.getPagesNumbers() => numbers[]`](#getpagesnumbers--numbers) - [License](#license) @@ -148,6 +149,25 @@ import { getDefaultPrinter } from "pdf-to-printer"; getDefaultPrinter().then(console.log); ``` +### `.getPagesNumbers() => numbers[]` + +A function to get the array that contains numbers of pages. + +**Arguments** + +1. `pages` (`string`, required): The argument represents pages, that should be print. + +**Returns** + +`number[]`: a array that contains the numbers of pages. + +**Examples** + +```javascript +import { getPagesNumbers } from "pdf-to-printer"; + +console.log(getPagesNumbers('1-2,4,8-12')); +``` ## License diff --git a/package.json b/package.json index 2b1c724..ee99d0e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pdf-to-printer-moderm", - "version": "5.7.0", + "version": "5.8.0", "description": "A utility for printing PDFs and images from Node.js and Electron.", "license": "MIT", "repository": { @@ -59,4 +59,4 @@ "electron", "windows" ] -} \ No newline at end of file +} diff --git a/src/get-pages-numbers/get-pages-numbers.spec.ts b/src/get-pages-numbers/get-pages-numbers.spec.ts new file mode 100644 index 0000000..5bdc44a --- /dev/null +++ b/src/get-pages-numbers/get-pages-numbers.spec.ts @@ -0,0 +1,49 @@ +import getPagesNumbers from "./get-pages-numbers"; + +type mockedPagesArgsType = { + args: Parameters; + expected: ReturnType | 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); + } + }); + }); +}); diff --git a/src/get-pages-numbers/get-pages-numbers.ts b/src/get-pages-numbers/get-pages-numbers.ts new file mode 100644 index 0000000..dfaf237 --- /dev/null +++ b/src/get-pages-numbers/get-pages-numbers.ts @@ -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()); +} diff --git a/src/index.ts b/src/index.ts index ef8fbd0..0f58cd2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ export { default as print } from "./print/print"; export { default as getPrinters } from "./get-printers/get-printers"; export { default as getDefaultPrinter } from "./get-default-printer/get-default-printer"; +export { default as getPagesNumbers } from "./get-pages-numbers/get-pages-numbers"; export { PrintOptions } from "./print/print"; export type Printer = {