Skip to content

Commit

Permalink
Replace the forEach method in Dict with "proper" iteration support
Browse files Browse the repository at this point in the history
  • Loading branch information
Snuffleupagus committed Nov 13, 2024
1 parent 6d05158 commit bcf9c80
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/core/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,12 +720,12 @@ class Catalog {
}
}
} else if (obj instanceof Dict) {
obj.forEach(function (key, value) {
for (const [key, value] of obj) {
const dest = fetchDest(value);
if (dest) {
dests[key] = dest;
}
});
}
}
return shadow(this, "destinations", dests);
}
Expand Down
7 changes: 1 addition & 6 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1282,13 +1282,8 @@ class PDFDocument {
},
};

const fonts = new Map();
fontRes.forEach((fontName, font) => {
fonts.set(fontName, font);
});
const promises = [];

for (const [fontName, font] of fonts) {
for (const [fontName, font] of fontRes) {
const descriptor = font.get("FontDescriptor");
if (!(descriptor instanceof Dict)) {
continue;
Expand Down
14 changes: 7 additions & 7 deletions src/core/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,14 @@ class Dict {
return this._map.has(key);
}

forEach(callback) {
for (const [key, value] of this._map) {
callback(
*[Symbol.iterator]() {
for (const [key, val] of this._map) {
yield [
key,
value instanceof Ref && this.xref
? this.xref.fetch(value, this.suppressEncryption)
: value
);
val instanceof Ref && this.xref
? this.xref.fetch(val, this.suppressEncryption)
: val,
];
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/core/struct_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ class StructTreeRoot {
if (!(roleMapDict instanceof Dict)) {
return;
}
roleMapDict.forEach((key, value) => {
if (!(value instanceof Name)) {
return;
for (const [key, value] of roleMapDict) {
if (value instanceof Name) {
this.roleMap.set(key, value.name);
}
this.roleMap.set(key, value.name);
});
}
}

static async canCreateStructureTree({
Expand Down
4 changes: 2 additions & 2 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,11 +688,11 @@ class WorkerMessageHandler {
const infoObj = Object.create(null);
const xrefInfo = xref.trailer.get("Info") || null;
if (xrefInfo instanceof Dict) {
xrefInfo.forEach((key, value) => {
for (const [key, value] of xrefInfo) {
if (typeof value === "string") {
infoObj[key] = stringToPDFString(value);
}
});
}
}

newXrefInfo = {
Expand Down
17 changes: 6 additions & 11 deletions test/unit/primitives_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,12 @@ describe("primitives", function () {
expect(values[2]).toEqual(testFontFile);
});

it("should callback for each stored key", function () {
const callbackSpy = jasmine.createSpy("spy on callback in dictionary");

dictWithManyKeys.forEach(callbackSpy);

expect(callbackSpy).toHaveBeenCalled();
const callbackSpyCalls = callbackSpy.calls;
expect(callbackSpyCalls.argsFor(0)).toEqual(["FontFile", testFontFile]);
expect(callbackSpyCalls.argsFor(1)).toEqual(["FontFile2", testFontFile2]);
expect(callbackSpyCalls.argsFor(2)).toEqual(["FontFile3", testFontFile3]);
expect(callbackSpyCalls.count()).toEqual(3);
it("should iterate through each stored key", function () {
expect([...dictWithManyKeys]).toEqual([
["FontFile", testFontFile],
["FontFile2", testFontFile2],
["FontFile3", testFontFile3],
]);
});

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

0 comments on commit bcf9c80

Please sign in to comment.