Skip to content

Commit 2c0cc48

Browse files
committed
Replace the forEach method in Dict with "proper" iteration support
1 parent 691be77 commit 2c0cc48

File tree

6 files changed

+19
-30
lines changed

6 files changed

+19
-30
lines changed

src/core/catalog.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -720,12 +720,12 @@ class Catalog {
720720
}
721721
}
722722
} else if (obj instanceof Dict) {
723-
obj.forEach(function (key, value) {
723+
for (const [key, value] of obj) {
724724
const dest = fetchDest(value);
725725
if (dest) {
726726
dests[key] = dest;
727727
}
728-
});
728+
}
729729
}
730730
return shadow(this, "destinations", dests);
731731
}

src/core/document.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -1282,13 +1282,8 @@ class PDFDocument {
12821282
},
12831283
};
12841284

1285-
const fonts = new Map();
1286-
fontRes.forEach((fontName, font) => {
1287-
fonts.set(fontName, font);
1288-
});
12891285
const promises = [];
1290-
1291-
for (const [fontName, font] of fonts) {
1286+
for (const [fontName, font] of fontRes) {
12921287
const descriptor = font.get("FontDescriptor");
12931288
if (!(descriptor instanceof Dict)) {
12941289
continue;

src/core/primitives.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,14 @@ class Dict {
203203
return this._map.has(key);
204204
}
205205

206-
forEach(callback) {
206+
*[Symbol.iterator]() {
207207
for (const [key, value] of this._map) {
208-
callback(
208+
yield [
209209
key,
210210
value instanceof Ref && this.xref
211211
? this.xref.fetch(value, this.suppressEncryption)
212-
: value
213-
);
212+
: value,
213+
];
214214
}
215215
}
216216

src/core/struct_tree.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,11 @@ class StructTreeRoot {
6262
if (!(roleMapDict instanceof Dict)) {
6363
return;
6464
}
65-
roleMapDict.forEach((key, value) => {
66-
if (!(value instanceof Name)) {
67-
return;
65+
for (const [key, value] of roleMapDict) {
66+
if (value instanceof Name) {
67+
this.roleMap.set(key, value.name);
6868
}
69-
this.roleMap.set(key, value.name);
70-
});
69+
}
7170
}
7271

7372
static async canCreateStructureTree({

src/core/worker.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,11 @@ class WorkerMessageHandler {
688688
const infoObj = Object.create(null);
689689
const xrefInfo = xref.trailer.get("Info") || null;
690690
if (xrefInfo instanceof Dict) {
691-
xrefInfo.forEach((key, value) => {
691+
for (const [key, value] of xrefInfo) {
692692
if (typeof value === "string") {
693693
infoObj[key] = stringToPDFString(value);
694694
}
695-
});
695+
}
696696
}
697697

698698
newXrefInfo = {

test/unit/primitives_spec.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -221,17 +221,12 @@ describe("primitives", function () {
221221
expect(values[2]).toEqual(testFontFile);
222222
});
223223

224-
it("should callback for each stored key", function () {
225-
const callbackSpy = jasmine.createSpy("spy on callback in dictionary");
226-
227-
dictWithManyKeys.forEach(callbackSpy);
228-
229-
expect(callbackSpy).toHaveBeenCalled();
230-
const callbackSpyCalls = callbackSpy.calls;
231-
expect(callbackSpyCalls.argsFor(0)).toEqual(["FontFile", testFontFile]);
232-
expect(callbackSpyCalls.argsFor(1)).toEqual(["FontFile2", testFontFile2]);
233-
expect(callbackSpyCalls.argsFor(2)).toEqual(["FontFile3", testFontFile3]);
234-
expect(callbackSpyCalls.count()).toEqual(3);
224+
it("should iterate through each stored key", function () {
225+
expect([...dictWithManyKeys]).toEqual([
226+
["FontFile", testFontFile],
227+
["FontFile2", testFontFile2],
228+
["FontFile3", testFontFile3],
229+
]);
235230
});
236231

237232
it("should handle keys pointing to indirect objects, both sync and async", async function () {

0 commit comments

Comments
 (0)