Skip to content

Commit

Permalink
Add getPagesNumbers method for parse pages arg
Browse files Browse the repository at this point in the history
  • Loading branch information
websmithcode committed Dec 28, 2023
1 parent 62ddbd9 commit 1648e70
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ If you rely on this package, please consider supporting it. Maintaining an open
- [`.print(pdf[, options]) => Promise<void>`](#printpdf-options--promisevoid)
- [`.getPrinters() => Promise<Printer[]>`](#getprinters--promiseprinter)
- [`.getDefaultPrinter() => Promise<Printer | null>`](#getdefaultprinter--promiseprinter--null)
- [`.getPagesNumbers() => numbers[]`](#getpagesnumbers--numbers)
- [License](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -59,4 +59,4 @@
"electron",
"windows"
]
}
}
49 changes: 49 additions & 0 deletions src/get-pages-numbers/get-pages-numbers.spec.ts
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);
}
});
});
});
30 changes: 30 additions & 0 deletions src/get-pages-numbers/get-pages-numbers.ts
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());
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down

0 comments on commit 1648e70

Please sign in to comment.