Skip to content

Replace the objectFromEntries helper function with an objectFromMap one instead, and simplify the data lookup in the AnnotationStorage.getValue method #13081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/display/annotation_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -91,7 +87,7 @@ class AnnotationStorage {
}

getAll() {
return this._storage.size > 0 ? objectFromEntries(this._storage) : null;
return this._storage.size > 0 ? objectFromMap(this._storage) : null;
}

get size() {
Expand Down
4 changes: 2 additions & 2 deletions src/display/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand All @@ -30,7 +30,7 @@ class Metadata {
}

getAll() {
return objectFromEntries(this._metadataMap);
return objectFromMap(this._metadataMap);
}

has(name) {
Expand Down
7 changes: 2 additions & 5 deletions src/display/optional_content_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 9 additions & 4 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1006,7 +1011,7 @@ export {
isSameOrigin,
isString,
MissingPDFException,
objectFromEntries,
objectFromMap,
objectSize,
OPS,
PageActionEventType,
Expand Down