From a0e584eeb228df3ed4e7dd3ad4c9f54ed37b3c75 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 11 Mar 2021 16:20:53 +0100 Subject: [PATCH 1/2] Replace the `objectFromEntries` helper function with an `objectFromMap` one instead Given that it's only used with `Map`s, and that it's currently implemented in such a way that we (indirectly) must iterate through the data *twice*, some simplification cannot hurt here. Note that the only reason that we're not using `Object.fromEntries(...)` directly, at each call-site, is that that one won't guarantee that a `null` prototype is being used. --- src/display/annotation_storage.js | 4 ++-- src/display/metadata.js | 4 ++-- src/display/optional_content_config.js | 7 ++----- src/shared/util.js | 13 +++++++++---- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/display/annotation_storage.js b/src/display/annotation_storage.js index 5af4cf59a..145b3faa9 100644 --- a/src/display/annotation_storage.js +++ b/src/display/annotation_storage.js @@ -14,7 +14,7 @@ */ import { deprecated } from "./display_utils.js"; -import { objectFromEntries } from "../shared/util.js"; +import { objectFromMap } from "../shared/util.js"; /** * Key/value storage for annotation data in forms. @@ -91,7 +91,7 @@ class AnnotationStorage { } getAll() { - return this._storage.size > 0 ? objectFromEntries(this._storage) : null; + return this._storage.size > 0 ? objectFromMap(this._storage) : null; } get size() { diff --git a/src/display/metadata.js b/src/display/metadata.js index 3c7da06f7..48b1fdc7a 100644 --- a/src/display/metadata.js +++ b/src/display/metadata.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import { objectFromEntries } from "../shared/util.js"; +import { objectFromMap } from "../shared/util.js"; class Metadata { constructor({ parsedData, rawData }) { @@ -30,7 +30,7 @@ class Metadata { } getAll() { - return objectFromEntries(this._metadataMap); + return objectFromMap(this._metadataMap); } has(name) { diff --git a/src/display/optional_content_config.js b/src/display/optional_content_config.js index 7e664a3e5..7d1f3178e 100644 --- a/src/display/optional_content_config.js +++ b/src/display/optional_content_config.js @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { objectFromEntries, warn } from "../shared/util.js"; +import { objectFromMap, warn } from "../shared/util.js"; class OptionalContentGroup { constructor(name, intent) { @@ -142,10 +142,7 @@ class OptionalContentConfig { } getGroups() { - if (!this._groups.size) { - return null; - } - return objectFromEntries(this._groups); + return this._groups.size > 0 ? objectFromMap(this._groups) : null; } getGroup(id) { diff --git a/src/shared/util.js b/src/shared/util.js index c175f0a2b..cfa37d033 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -597,9 +597,14 @@ function objectSize(obj) { return Object.keys(obj).length; } -// Ensures that the returned Object has a `null` prototype. -function objectFromEntries(iterable) { - return Object.assign(Object.create(null), Object.fromEntries(iterable)); +// Ensure that the returned Object has a `null` prototype; hence why +// `Object.fromEntries(...)` is not used. +function objectFromMap(map) { + const obj = Object.create(null); + for (const [key, value] of map) { + obj[key] = value; + } + return obj; } // Checks the endianness of the platform. @@ -1006,7 +1011,7 @@ export { isSameOrigin, isString, MissingPDFException, - objectFromEntries, + objectFromMap, objectSize, OPS, PageActionEventType, From b3264328959740503350d2b5b64c255aa3265182 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 11 Mar 2021 16:25:53 +0100 Subject: [PATCH 2/2] Simplify the data lookup in the `AnnotationStorage.getValue` method Rather than first checking if data exists before fetching it from storage, we can simply do the lookup directly and then check its value. Note that this follows the same pattern as utilized in the `AnnotationStorage.setValue` method. --- src/display/annotation_storage.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/display/annotation_storage.js b/src/display/annotation_storage.js index 145b3faa9..c8d51d1e5 100644 --- a/src/display/annotation_storage.js +++ b/src/display/annotation_storage.js @@ -33,8 +33,7 @@ class AnnotationStorage { } /** - * Get the value for a given key if it exists - * or return the default value + * Get the value for a given key if it exists, or return the default value. * * @public * @memberof AnnotationStorage @@ -43,11 +42,8 @@ class AnnotationStorage { * @returns {Object} */ getValue(key, defaultValue) { - if (this._storage.has(key)) { - return this._storage.get(key); - } - - return defaultValue; + const obj = this._storage.get(key); + return obj !== undefined ? obj : defaultValue; } /**