Skip to content

Commit

Permalink
Replace static output file mask with a custom naming function
Browse files Browse the repository at this point in the history
  • Loading branch information
dichovsky committed Oct 17, 2024
1 parent 17e31d0 commit 9d3278b
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 187 deletions.
25 changes: 25 additions & 0 deletions __tests__/output.file.mask.buffer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { expect, test } from 'vitest';
import { PngPageOutput, pdfToPng } from '../src';

const pdfFilePath: string = resolve('./test-data/large_pdf.pdf');
const pdfBuffer: Buffer = readFileSync(pdfFilePath);

test(`should apply file mask is defined for pdf buffer`, async () => {
const pngPages: PngPageOutput[] = await pdfToPng(pdfBuffer, {
outputFileMaskFunc: (pageNumber: number) => `custom_buffer_${pageNumber}.png`,
});

pngPages.forEach((pngPage: PngPageOutput, index: number) => {
expect(pngPage.name).to.equal(`custom_buffer_${index + 1}.png`);
});
});

test(`should apply default buffer name if outputFileMaskFunc is not defined for pdf buffer`, async () => {
const pngPages: PngPageOutput[] = await pdfToPng(pdfBuffer);

pngPages.forEach((pngPage: PngPageOutput, index: number) => {
expect(pngPage.name).to.equal(`buffer_page_${index + 1}.png`);
});
});
23 changes: 23 additions & 0 deletions __tests__/output.file.mask.file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { parse, resolve } from 'node:path';
import { expect, test } from 'vitest';
import { PngPageOutput, pdfToPng } from '../src';

const pdfFilePath: string = resolve('./test-data/large_pdf.pdf');

test(`should apply file mask if defined for pdf file path`, async () => {
const pngPages: PngPageOutput[] = await pdfToPng(pdfFilePath, {
outputFileMaskFunc: (pageNumber: number) => `pdf_file_${pageNumber}.png`,
});

pngPages.forEach((pngPage: PngPageOutput, index: number) => {
expect(pngPage.name).to.equal(`pdf_file_${index + 1}.png`);
});
});

test(`should apply default buffer name if outputFileMaskFunc is not defined for pdf buffer`, async () => {
const pngPages: PngPageOutput[] = await pdfToPng(pdfFilePath);

pngPages.forEach((pngPage: PngPageOutput, index: number) => {
expect(pngPage.name).to.equal(`${parse(pdfFilePath).name}_page_${index + 1}.png`);
});
});
27 changes: 0 additions & 27 deletions __tests__/pdf.to.buffer.file-mask.test.ts

This file was deleted.

31 changes: 0 additions & 31 deletions __tests__/pdf.to.buffer.no-file-mask.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion __tests__/props.to.pdf.doc.init.params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
enableXfa: true,
pdfFilePassword: 'pdfFilePassword',
outputFolder: 'outputFolder',
outputFileMask: 'outputFileMask',
pagesToProcess: [1, 2, 3],
strictPagesToProcess: true,
verbosityLevel: 2,
Expand Down
Loading

0 comments on commit 9d3278b

Please sign in to comment.