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

adnsistemas / pdf-lib / #8

02 Dec 2025 12:22AM UTC coverage: 70.499% (+0.3%) from 70.248%
#8

push

David N. Abdala
Inclusion of saveAndContinue logic

2443 of 3918 branches covered (62.35%)

Branch coverage included in aggregate %.

6767 of 9146 relevant lines covered (73.99%)

145637.43 hits per line

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

85.71
/src/core/objects/PDFArray.ts
1
import PDFBool from './PDFBool';
2
import PDFDict from './PDFDict';
3
import PDFHexString from './PDFHexString';
4
import PDFName from './PDFName';
5
import PDFNull from './PDFNull';
6
import PDFNumber from './PDFNumber';
53✔
7
import PDFObject from './PDFObject';
53✔
8
import PDFRef from './PDFRef';
9
import PDFStream from './PDFStream';
10
import PDFString from './PDFString';
11
import PDFContext from '../PDFContext';
12
import CharCodes from '../syntax/CharCodes';
53✔
13
import { PDFArrayIsNotRectangleError } from '../errors';
53✔
14
import PDFRawStream from './PDFRawStream';
15

16
class PDFArray extends PDFObject {
17
  static withContext = (context: PDFContext) => new PDFArray(context);
106,642✔
18

19
  private readonly array: PDFObject[];
20
  private readonly context: PDFContext;
21

22
  private constructor(context: PDFContext) {
23
    super();
106,642✔
24
    this.array = [];
106,642✔
25
    this.context = context;
106,642✔
26
  }
27

28
  size(): number {
29
    return this.array.length;
389,967✔
30
  }
31

32
  push(object: PDFObject): void {
33
    this.registerChange();
5,589,759✔
34
    this.array.push(object);
5,589,759✔
35
  }
36

37
  insert(index: number, object: PDFObject): void {
38
    this.registerChange();
241✔
39
    this.array.splice(index, 0, object);
241✔
40
  }
41

42
  indexOf(object: PDFObject): number | undefined {
43
    const index = this.array.indexOf(object);
19✔
44
    return index === -1 ? undefined : index;
19✔
45
  }
46

47
  remove(index: number): void {
48
    this.registerChange();
33✔
49
    this.array.splice(index, 1);
33✔
50
  }
51

52
  set(idx: number, object: PDFObject): void {
53
    this.registerChange();
822✔
54
    this.array[idx] = object;
822✔
55
  }
56

57
  get(index: number): PDFObject {
58
    return this.array[index];
25,385,877✔
59
  }
60

61
  lookupMaybe(index: number, type: typeof PDFArray): PDFArray | undefined;
62
  lookupMaybe(index: number, type: typeof PDFBool): PDFBool | undefined;
63
  lookupMaybe(index: number, type: typeof PDFDict): PDFDict | undefined;
64
  lookupMaybe(
65
    index: number,
66
    type: typeof PDFHexString,
67
  ): PDFHexString | undefined;
68
  lookupMaybe(index: number, type: typeof PDFName): PDFName | undefined;
69
  lookupMaybe(index: number, type: typeof PDFNull): typeof PDFNull | undefined;
70
  lookupMaybe(index: number, type: typeof PDFNumber): PDFNumber | undefined;
71
  lookupMaybe(index: number, type: typeof PDFStream): PDFStream | undefined;
72
  lookupMaybe(
73
    index: number,
74
    type: typeof PDFRawStream,
75
  ): PDFRawStream | undefined;
76
  lookupMaybe(index: number, type: typeof PDFRef): PDFRef | undefined;
77
  lookupMaybe(index: number, type: typeof PDFString): PDFString | undefined;
78
  lookupMaybe(
79
    index: number,
80
    type1: typeof PDFString,
81
    type2: typeof PDFHexString,
82
  ): PDFString | PDFHexString | undefined;
83

84
  lookupMaybe(index: number, ...types: any[]) {
85
    return this.context.lookupMaybe(
×
86
      this.get(index),
87
      // @ts-ignore
88
      ...types,
89
    ) as any;
90
  }
91

92
  lookup(index: number): PDFObject | undefined;
93
  lookup(index: number, type: typeof PDFArray): PDFArray;
94
  lookup(index: number, type: typeof PDFBool): PDFBool;
95
  lookup(index: number, type: typeof PDFDict): PDFDict;
96
  lookup(index: number, type: typeof PDFHexString): PDFHexString;
97
  lookup(index: number, type: typeof PDFName): PDFName;
98
  lookup(index: number, type: typeof PDFNull): typeof PDFNull;
99
  lookup(index: number, type: typeof PDFNumber): PDFNumber;
100
  lookup(index: number, type: typeof PDFStream): PDFStream;
101
  lookup(index: number, type: typeof PDFRawStream): PDFRawStream;
102
  lookup(index: number, type: typeof PDFRef): PDFRef;
103
  lookup(index: number, type: typeof PDFString): PDFString;
104
  lookup(
105
    index: number,
106
    type1: typeof PDFString,
107
    type2: typeof PDFHexString,
108
  ): PDFString | PDFHexString;
109

110
  lookup(index: number, ...types: any[]) {
111
    return this.context.lookup(
12,134✔
112
      this.get(index),
113
      // @ts-ignore
114
      ...types,
115
    ) as any;
116
  }
117

118
  asRectangle(): { x: number; y: number; width: number; height: number } {
119
    if (this.size() !== 4) throw new PDFArrayIsNotRectangleError(this.size());
460!
120

121
    const x1 = this.lookup(0, PDFNumber).asNumber();
460✔
122
    const y1 = this.lookup(1, PDFNumber).asNumber();
460✔
123
    const x2 = this.lookup(2, PDFNumber).asNumber();
460✔
124
    const y2 = this.lookup(3, PDFNumber).asNumber();
460✔
125

126
    const x = Math.min(x1, x2);
460✔
127
    const y = Math.min(y1, y2);
460✔
128
    const width = Math.abs(x1 - x2);
460✔
129
    const height = Math.abs(y1 - y2);
460✔
130

131
    return { x, y, width, height };
460✔
132
  }
133

134
  asArray(): PDFObject[] {
135
    return this.array.slice();
12✔
136
  }
137

138
  clone(context?: PDFContext): PDFArray {
139
    const clone = PDFArray.withContext(context || this.context);
58✔
140
    for (let idx = 0, len = this.size(); idx < len; idx++) {
58✔
141
      clone.push(this.array[idx]);
827✔
142
    }
143
    return clone;
58✔
144
  }
145

146
  toString(): string {
147
    let arrayString = '[ ';
34✔
148
    for (let idx = 0, len = this.size(); idx < len; idx++) {
34✔
149
      arrayString += this.get(idx).toString();
97✔
150
      arrayString += ' ';
97✔
151
    }
152
    arrayString += ']';
34✔
153
    return arrayString;
34✔
154
  }
155

156
  sizeInBytes(): number {
157
    let size = 3;
3,034✔
158
    for (let idx = 0, len = this.size(); idx < len; idx++) {
3,034✔
159
      size += this.get(idx).sizeInBytes() + 1;
33,908✔
160
    }
161
    return size;
3,034✔
162
  }
163

164
  copyBytesInto(buffer: Uint8Array, offset: number): number {
165
    const initialOffset = offset;
2,980✔
166

167
    buffer[offset++] = CharCodes.LeftSquareBracket;
2,980✔
168
    buffer[offset++] = CharCodes.Space;
2,980✔
169
    for (let idx = 0, len = this.size(); idx < len; idx++) {
2,980✔
170
      offset += this.get(idx).copyBytesInto(buffer, offset);
30,207✔
171
      buffer[offset++] = CharCodes.Space;
30,207✔
172
    }
173
    buffer[offset++] = CharCodes.RightSquareBracket;
2,980✔
174

175
    return offset - initialOffset;
2,980✔
176
  }
177

178
  scalePDFNumbers(x: number, y: number): void {
179
    for (let idx = 0, len = this.size(); idx < len; idx++) {
×
180
      const el = this.lookup(idx);
×
181
      if (el instanceof PDFNumber) {
×
182
        const factor = idx % 2 === 0 ? x : y;
×
183
        this.set(idx, PDFNumber.of(el.asNumber() * factor));
×
184
      }
185
    }
186
  }
187

188
  registerChange(): void {
189
    this.context.registerObjectChange(this);
5,590,855✔
190
  }
191
}
192

193
export default PDFArray;
53✔
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