Merge pull request #13081 from Snuffleupagus/objectFromMap

Replace the `objectFromEntries` helper function with an `objectFromMap` one instead, and simplify the data lookup in the AnnotationStorage.getValue method
This commit is contained in:
Tim van der Meij 2021-03-12 21:12:03 +01:00 committed by GitHub
commit be4a41960a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 20 deletions

View File

@ -14,7 +14,7 @@
*/ */
import { deprecated } from "./display_utils.js"; 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. * Key/value storage for annotation data in forms.
@ -33,8 +33,7 @@ class AnnotationStorage {
} }
/** /**
* Get the value for a given key if it exists * Get the value for a given key if it exists, or return the default value.
* or return the default value
* *
* @public * @public
* @memberof AnnotationStorage * @memberof AnnotationStorage
@ -43,11 +42,8 @@ class AnnotationStorage {
* @returns {Object} * @returns {Object}
*/ */
getValue(key, defaultValue) { getValue(key, defaultValue) {
if (this._storage.has(key)) { const obj = this._storage.get(key);
return this._storage.get(key); return obj !== undefined ? obj : defaultValue;
}
return defaultValue;
} }
/** /**
@ -91,7 +87,7 @@ class AnnotationStorage {
} }
getAll() { getAll() {
return this._storage.size > 0 ? objectFromEntries(this._storage) : null; return this._storage.size > 0 ? objectFromMap(this._storage) : null;
} }
get size() { get size() {

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { objectFromEntries } from "../shared/util.js"; import { objectFromMap } from "../shared/util.js";
class Metadata { class Metadata {
constructor({ parsedData, rawData }) { constructor({ parsedData, rawData }) {
@ -30,7 +30,7 @@ class Metadata {
} }
getAll() { getAll() {
return objectFromEntries(this._metadataMap); return objectFromMap(this._metadataMap);
} }
has(name) { has(name) {

View File

@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { objectFromEntries, warn } from "../shared/util.js"; import { objectFromMap, warn } from "../shared/util.js";
class OptionalContentGroup { class OptionalContentGroup {
constructor(name, intent) { constructor(name, intent) {
@ -142,10 +142,7 @@ class OptionalContentConfig {
} }
getGroups() { getGroups() {
if (!this._groups.size) { return this._groups.size > 0 ? objectFromMap(this._groups) : null;
return null;
}
return objectFromEntries(this._groups);
} }
getGroup(id) { getGroup(id) {

View File

@ -597,9 +597,14 @@ function objectSize(obj) {
return Object.keys(obj).length; return Object.keys(obj).length;
} }
// Ensures that the returned Object has a `null` prototype. // Ensure that the returned Object has a `null` prototype; hence why
function objectFromEntries(iterable) { // `Object.fromEntries(...)` is not used.
return Object.assign(Object.create(null), Object.fromEntries(iterable)); 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. // Checks the endianness of the platform.
@ -1006,7 +1011,7 @@ export {
isSameOrigin, isSameOrigin,
isString, isString,
MissingPDFException, MissingPDFException,
objectFromEntries, objectFromMap,
objectSize, objectSize,
OPS, OPS,
PageActionEventType, PageActionEventType,