Re-factor how Metadata class instances store its data internally

Please note that these changes do *not* affect the *public* interface of the `Metadata` class, but only touches internal structures.[1]

These changes were prompted by looking at the `getAll` method, which simply returns the "private" metadata object to the consumer. This seems wrong conceptually, since it allows way too easy/accidental changes to the internal parsed metadata.
As part of fixing this, the internal metadata was changed to use a `Map` rather than a plain Object.

---
[1] Basically, we shouldn't need to worry about someone depending on internal implementation details.
This commit is contained in:
Jonas Jenwald 2020-02-13 17:45:38 +01:00
parent 3f1568b51a
commit 5cdfff4a47

View File

@ -27,7 +27,7 @@ class Metadata {
const parser = new SimpleXMLParser();
const xmlDocument = parser.parseFromString(data);
this._metadata = Object.create(null);
this._metadataMap = new Map();
if (xmlDocument) {
this._parse(xmlDocument);
@ -107,23 +107,26 @@ class Metadata {
const entry = desc.childNodes[j];
const name = entry.nodeName.toLowerCase();
this._metadata[name] = entry.textContent.trim();
this._metadataMap.set(name, entry.textContent.trim());
}
}
}
}
get(name) {
const data = this._metadata[name];
return typeof data !== "undefined" ? data : null;
return this._metadataMap.has(name) ? this._metadataMap.get(name) : null;
}
getAll() {
return this._metadata;
const obj = Object.create(null);
for (const [key, value] of this._metadataMap) {
obj[key] = value;
}
return obj;
}
has(name) {
return typeof this._metadata[name] !== "undefined";
return this._metadataMap.has(name);
}
}