@pdfme/pdf-lib
Version:
Create and modify PDF files with JavaScript
102 lines • 4.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("../../../src/core");
const utils_1 = require("../../../src/utils");
describe(`PDFArray`, () => {
const context = core_1.PDFContext.create();
it(`can be constructed with PDFArray.withContext(...)`, () => {
expect(core_1.PDFArray.withContext(context)).toBeInstanceOf(core_1.PDFArray);
});
const pdfArray = core_1.PDFArray.withContext(context);
const pdfBool = core_1.PDFBool.True;
const pdfHexString = core_1.PDFHexString.of('ABC123');
const pdfName = core_1.PDFName.of('Foo#Bar!');
const pdfNull = core_1.PDFNull;
const pdfNumber = core_1.PDFNumber.of(-24.179);
const pdfString = core_1.PDFString.of('foobar');
const pdfSubDict = core_1.PDFDict.withContext(context);
pdfSubDict.set(core_1.PDFName.of('Foo'), core_1.PDFName.of('Bar'));
const pdfSubArray = core_1.PDFArray.withContext(context);
pdfSubArray.push(core_1.PDFBool.True);
pdfSubArray.push(pdfSubDict);
const pdfRef = core_1.PDFRef.of(21, 92);
pdfArray.push(pdfBool);
pdfArray.push(pdfHexString);
pdfArray.push(pdfName);
pdfArray.push(pdfNull);
pdfArray.push(pdfNumber);
pdfArray.push(pdfString);
pdfArray.push(pdfSubArray);
pdfArray.push(pdfRef);
it(`retains pushed objects`, () => {
expect(pdfArray.size()).toBe(8);
expect(pdfArray.get(0)).toBe(pdfBool);
expect(pdfArray.get(1)).toBe(pdfHexString);
expect(pdfArray.get(2)).toBe(pdfName);
expect(pdfArray.get(3)).toBe(pdfNull);
expect(pdfArray.get(4)).toBe(pdfNumber);
expect(pdfArray.get(5)).toBe(pdfString);
expect(pdfArray.get(6)).toBe(pdfSubArray);
expect(pdfArray.get(7)).toBe(pdfRef);
});
it(`allows objects to be assigned to specific indices`, () => {
const array = core_1.PDFArray.withContext(core_1.PDFContext.create());
array.push(core_1.PDFName.of('a'));
array.push(core_1.PDFName.of('b'));
array.push(core_1.PDFName.of('c'));
array.set(1, core_1.PDFName.of('z'));
expect(array.get(1)).toBe(core_1.PDFName.of('z'));
expect(array.size()).toBe(3);
});
it(`allows objects to be inserted at specific indices`, () => {
const array = core_1.PDFArray.withContext(core_1.PDFContext.create());
array.push(core_1.PDFName.of('a'));
array.push(core_1.PDFName.of('b'));
array.push(core_1.PDFName.of('c'));
array.insert(1, core_1.PDFName.of('z'));
expect(array.get(0)).toBe(core_1.PDFName.of('a'));
expect(array.get(1)).toBe(core_1.PDFName.of('z'));
expect(array.get(2)).toBe(core_1.PDFName.of('b'));
expect(array.size()).toBe(4);
});
it(`allows objects to be removed from specific indices`, () => {
const array = core_1.PDFArray.withContext(core_1.PDFContext.create());
array.push(core_1.PDFName.of('a'));
array.push(core_1.PDFName.of('b'));
array.push(core_1.PDFName.of('c'));
array.remove(1);
expect(array.get(0)).toBe(core_1.PDFName.of('a'));
expect(array.get(1)).toBe(core_1.PDFName.of('c'));
expect(array.size()).toBe(2);
});
it(`can be converted to an Array`, () => {
expect(pdfArray.asArray()).toEqual([
pdfBool,
pdfHexString,
pdfName,
pdfNull,
pdfNumber,
pdfString,
pdfSubArray,
pdfRef,
]);
});
it(`can be cloned`, () => {
const original = pdfArray;
const clone = original.clone();
expect(clone).not.toBe(original);
expect(clone.toString()).toBe(original.toString());
});
it(`can be converted to a string`, () => {
expect(String(pdfArray)).toBe('[ true <ABC123> /Foo#23Bar! null -24.179 (foobar) [ true <<\n/Foo /Bar\n>> ] 21 92 R ]');
});
it(`can provide its size in bytes`, () => {
expect(pdfArray.sizeInBytes()).toBe(84);
});
it(`can be serialized`, () => {
const buffer = new Uint8Array(88).fill((0, utils_1.toCharCode)(' '));
expect(pdfArray.copyBytesInto(buffer, 3)).toBe(84);
expect(buffer).toEqual((0, utils_1.typedArrayFor)(' [ true <ABC123> /Foo#23Bar! null -24.179 (foobar) [ true <<\n/Foo /Bar\n>> ] 21 92 R ] '));
});
});
//# sourceMappingURL=PDFArray.spec.js.map