From e1ee3835cdd9e12d664c8d25db0a5dca845374b6 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald <jonas.jenwald@gmail.com> Date: Thu, 22 Jul 2021 13:20:28 +0200 Subject: [PATCH] Remove some duplication in the `Dict.merge` method Currently the `!mergeSubDicts` code-path is essentially just duplicated code, which we can easily avoid by simply moving that check. (This may lead to ever so slightly more parsing for this case, but the difference ought to be negligible in practice.) --- src/core/primitives.js | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/core/primitives.js b/src/core/primitives.js index c35dedef5..fcbfb1420 100644 --- a/src/core/primitives.js +++ b/src/core/primitives.js @@ -185,22 +185,8 @@ class Dict { } static merge({ xref, dictArray, mergeSubDicts = false }) { - const mergedDict = new Dict(xref); - - if (!mergeSubDicts) { - for (const dict of dictArray) { - if (!(dict instanceof Dict)) { - continue; - } - for (const [key, value] of Object.entries(dict._map)) { - if (mergedDict._map[key] === undefined) { - mergedDict._map[key] = value; - } - } - } - return mergedDict.size > 0 ? mergedDict : Dict.empty; - } - const properties = new Map(); + const mergedDict = new Dict(xref), + properties = new Map(); for (const dict of dictArray) { if (!(dict instanceof Dict)) { @@ -211,6 +197,8 @@ class Dict { if (property === undefined) { property = []; properties.set(key, property); + } else if (!mergeSubDicts) { + continue; // Ignore additional entries for a "shallow" merge. } property.push(value); }