Turn back on no-null-keyword. Use empty object instead of null to signal to the xml library that an empty element should be produced. Update the related tests.

Related to #306
This commit is contained in:
Bruce Duncan
2019-04-10 01:28:37 -04:00
parent 816cb54b14
commit bb1604cd8f
17 changed files with 72 additions and 71 deletions

View File

@ -73,7 +73,7 @@ describe("ImportedXmlComponent", () => {
},
},
{
"w:child": null,
"w:child": {},
},
],
});

View File

@ -1,4 +1,4 @@
import { assert } from "chai";
import { expect } from "chai";
import { Utility } from "tests/utility";
import { XmlComponent } from "./";
@ -15,7 +15,7 @@ describe("XmlComponent", () => {
describe("#constructor()", () => {
it("should create an Xml Component which has the correct rootKey", () => {
const newJson = Utility.jsonify(xmlComponent);
assert.equal(newJson.rootKey, "w:test");
expect(newJson.rootKey).to.equal("w:test");
});
});
@ -31,7 +31,7 @@ describe("XmlComponent", () => {
return;
}
assert.isNull(xml["w:test"]);
expect(xml["w:test"]).to.deep.equal({});
});
});
});

View File

@ -30,11 +30,11 @@ export abstract class XmlComponent extends BaseXmlComponent {
// represents our attributes, use the object itself as our children to
// avoid an unneeded XML close element. (Note: We have to use this
// function to get typescript to allow our check.)
// Additionally, if the array is empty, use null as our children to
// get an empty XML element generated.
// Additionally, if the array is empty, use an empty object as our
// children in order to get an empty XML element generated.
const onlyAttrs = (c) => typeof c === "object" && c._attr;
return {
[this.rootKey]: children.length ? (children.length === 1 && onlyAttrs(children[0]) ? children[0] : children) : null,
[this.rootKey]: children.length ? (children.length === 1 && onlyAttrs(children[0]) ? children[0] : children) : {},
};
}
@ -52,7 +52,9 @@ export abstract class XmlComponent extends BaseXmlComponent {
export abstract class IgnoreIfEmptyXmlComponent extends XmlComponent {
public prepForXml(): IXmlableObject | undefined {
const result = super.prepForXml();
if (result && result[this.rootKey]) {
// Ignore the object if its falsey or is an empty object (would produce
// an empty XML element if allowed to be included in the output).
if (result && (typeof result[this.rootKey] !== "object" || Object.keys(result[this.rootKey]).length)) {
return result;
}
}