• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adnsistemas / pdf-lib / #18

24 Mar 2026 08:15PM UTC coverage: 74.286% (+0.3%) from 74.001%
#18

push

David N. Abdala
Documentation change

2569 of 3981 branches covered (64.53%)

Branch coverage included in aggregate %.

7372 of 9401 relevant lines covered (78.42%)

297170.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.63
/src/api/objects.ts
1
import { PDFName, PDFNumber } from '../core';
54✔
2

3
export const asPDFName = (name: string | PDFName) =>
54✔
4
  typeof name !== 'string' ? name : PDFName.of(name);
181✔
5

6
export const asPDFNumber = (num: number | PDFNumber) =>
54✔
7
  typeof num !== 'number' ? num : PDFNumber.of(num);
9,408!
8

9
export const asNumber = (num: number | PDFNumber) =>
54✔
10
  typeof num !== 'number' ? num.asNumber() : num;
2,093!
11

12
export enum PDFClasses {
54✔
13
  PDFDict = 'PDFDict',
54✔
14
  PDFObject = 'PDFObject',
54✔
15
  PDFNumber = 'PDFNumber',
54✔
16
  PDFName = 'PDFName',
54✔
17
  PDFStream = 'PDFStream',
54✔
18
  PDFWriter = 'PDFWriter',
54✔
19
  PDFStreamWriter = 'PDFStreamWriter',
54✔
20
  PDFString = 'PDFString',
54✔
21
  PDFHexString = 'PDFHexString',
54✔
22
  PDFDocument = 'PDFDocument',
54✔
23
  PDFPageLeaf = 'PDFPageLeaf',
54✔
24
  StandardFontEmbedder = 'StandardFontEmbedder',
54✔
25
  PDFArray = 'PDFArray',
54✔
26
  PDFButton = 'PDFButton',
54✔
27
  PDFCheckBox = 'PDFCheckBox',
54✔
28
  PDFDropdown = 'PDFDropdown',
54✔
29
  PDFOptionList = 'PDFOptionList',
54✔
30
  PDFRadioGroup = 'PDFRadioGroup',
54✔
31
  PDFSignature = 'PDFSignature',
54✔
32
  PDFTextField = 'PDFTextField',
54✔
33
  PDFField = 'PDFField',
54✔
34
  PDFRef = 'PDFRef',
54✔
35
  PDFAcroForm = 'PDFAcroForm',
54✔
36
  PDFAcroField = 'PDFAcroField',
54✔
37
  PDFAcroNonTerminal = 'PDFAcroNonTerminal',
54✔
38
  PDFAcroPushButton = 'PDFAcroPushButton',
54✔
39
  PDFAcroCheckBox = 'PDFAcroCheckBox',
54✔
40
  PDFAcroComboBox = 'PDFAcroComboBox',
54✔
41
  PDFAcroListBox = 'PDFAcroListBox',
54✔
42
  PDFAcroText = 'PDFAcroText',
54✔
43
  PDFAcroRadioButton = 'PDFAcroRadioButton',
54✔
44
  PDFAcroSignature = 'PDFAcroSignature',
54✔
45
  AESBaseCipher = 'AESBaseCipher',
54✔
46
  PDFBool = 'PDFBool',
54✔
47
  PDFRawStream = 'PDFRawStream',
54✔
48
  PDFContentStream = 'PDFContentStream',
54✔
49
  Segment = 'Segment',
54✔
50
  Rectangle = 'Rectangle',
54✔
51
  Arc = 'Arc',
54✔
52
  Circle = 'Circle',
54✔
53
  Ellipse = 'Ellipse',
54✔
54
  Line = 'Line',
54✔
55
  Plot = 'Plot',
54✔
56
  Point = 'Point',
54✔
57
  PDFPageTree = 'PDFPageTree',
54✔
58
  PDFInvalidObject = 'PDFInvalidObject',
54✔
59
  PDFCatalog = 'PDFCatalog',
54✔
60
  DefaultDocumentSnapshot = 'DefaultDocumentSnapshot',
54✔
61
  PDFObjectStream = 'PDFObjectStream',
54✔
62
  IncrementalDocumentSnapshot = 'IncrementalDocumentSnapshot',
54✔
63
  PDFNull = 'PDFNull',
54✔
64
}
65

66
/**
67
 * Replaces instanceof operator by a "secure function" that uses clases internal identification.
68
 * This allows complex environments, where objects comes from different library instances, to work as expected.
69
 * @param {any} object Object to which compare the classname.
70
 * @param {PDFClasses | typeof PDFClass} className Class name object is expected to be an instanca (or subclass instance)
71
 * @returns {boolean}
72
 */
73
export const isPDFInstance = (object: any, className: PDFClasses): boolean => {
54✔
74
  if (!object) return false;
85,353,861✔
75
  if (!object.myClass) return false; // it's not an object from this library
85,319,639✔
76
  if (typeof className === 'function') {
85,281,604✔
77
    // is a constructor (PDFnNumber, PDFDict, etc)
78
    try {
24,617✔
79
      return isPDFInstance(object, (className as any).className());
24,617✔
80
    } catch (error) {
81
      return false;
1✔
82
    }
83
  }
84
  if (object.myClass() === className) return true;
85,256,987✔
85
  // not a perfect match, check superclasses
86
  let proto = Object.getPrototypeOf(object);
84,016,868✔
87
  while (proto && proto.constructor.className) {
84,016,868✔
88
    try {
168,300,323✔
89
      if (proto.constructor.className() === className) return true;
168,300,323✔
90
      proto = Object.getPrototypeOf(proto);
168,155,140✔
91
    } catch (error) {
92
      return false;
×
93
    }
94
  }
95
  // not the class, nor a subclass
96
  return false;
83,871,685✔
97
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc