diff --git a/ts/docx/document/index.ts b/ts/docx/document/index.ts index cffa5f17d2..de8f3f0053 100644 --- a/ts/docx/document/index.ts +++ b/ts/docx/document/index.ts @@ -33,9 +33,4 @@ export class Document extends XmlComponent { public addParagraph(paragraph: Paragraph): void { this.body.push(paragraph); } - - public clearVariables(): void { - this.body.clearVariables(); - delete this.body; - } } diff --git a/ts/docx/xml-components/base.ts b/ts/docx/xml-components/base.ts index 0fb1811bf4..cc20f27cb2 100644 --- a/ts/docx/xml-components/base.ts +++ b/ts/docx/xml-components/base.ts @@ -5,9 +5,5 @@ export abstract class BaseXmlComponent { this.rootKey = rootKey; } - public abstract replaceKey(): void; - - public clearVariables(): void { - // Do Nothing - } + public abstract toXml(): object; } diff --git a/ts/docx/xml-components/default-attributes.ts b/ts/docx/xml-components/default-attributes.ts index b95611d4c8..ec2641fb66 100644 --- a/ts/docx/xml-components/default-attributes.ts +++ b/ts/docx/xml-components/default-attributes.ts @@ -16,15 +16,18 @@ export abstract class XmlAttributeComponent extends BaseXmlComponent { } } - public replaceKey(): void { - if (this.root !== undefined) { + public toXml(): object { + const attrs = {}; + if (this.root != undefined) { _.forOwn(this.root, (value, key) => { - const newKey = this.xmlKeys[key]; - this.root[newKey] = value; - delete this.root[key]; + if (value != undefined) { + const newKey = this.xmlKeys[key]; + attrs[newKey] = value; + } }); - this[this.rootKey] = this.root; - delete this.root; } + const ret = {}; + ret[this.rootKey] = attrs; + return ret; } } diff --git a/ts/docx/xml-components/index.ts b/ts/docx/xml-components/index.ts index bcbaef4eec..7e049df6a7 100644 --- a/ts/docx/xml-components/index.ts +++ b/ts/docx/xml-components/index.ts @@ -1,5 +1,6 @@ import * as _ from "lodash"; import { BaseXmlComponent } from "./base"; +export { BaseXmlComponent }; export abstract class XmlComponent extends BaseXmlComponent { protected root: Array; @@ -9,17 +10,15 @@ export abstract class XmlComponent extends BaseXmlComponent { this.root = new Array(); } - public replaceKey(): void { - // console.log(this.rootKey); - // console.log(this.root); - if (this.root !== undefined) { - this.root.forEach((root) => { - if (root && root instanceof BaseXmlComponent) { - root.replaceKey(); - } - }); - this[this.rootKey] = this.root; - delete this.root; + public toXml(): object { + const ret = this.root.map((comp) => { + if (comp instanceof BaseXmlComponent) { + return comp.toXml(); + } + return comp + }).filter((comp) => comp); // Exclude null, undefined, and empty strings + return { + [this.rootKey]: ret, } } } diff --git a/ts/export/formatter.ts b/ts/export/formatter.ts index 163e059148..4d99848996 100644 --- a/ts/export/formatter.ts +++ b/ts/export/formatter.ts @@ -1,52 +1,7 @@ -import * as _ from "lodash"; -import {XmlComponent} from "../docx/xml-components"; +import { BaseXmlComponent } from "../docx/xml-components"; export class Formatter { - - public format(input: any): Object { - input.clearVariables(); - this.replaceKeys(input); - const newJson = this.clense(input); - // console.log(JSON.stringify(newJson, null, " ")); - return newJson; + public format(input: BaseXmlComponent): Object { + return input.toXml(); } - - private replaceKeys(input: XmlComponent): Object { - input.replaceKey(); - - return input; - } - - private clense(input: any): Object { - const newJson = this.jsonify(input); - - this.deepTraverseJson(newJson, (parent, value, key) => { - if (key === "properties") { - delete parent[key]; - } - if (key === "xmlKeys") { - delete parent[key]; - } - if (key === "rootKey") { - delete parent[key]; - } - }); - - return newJson; - } - - private jsonify(obj: Object): Object { - let stringifiedJson = JSON.stringify(obj); - return JSON.parse(stringifiedJson); - } - - private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void { - _.forOwn(json, (value, key) => { - if (_.isObject(value) && key !== "xmlKeys" && key !== "rootKey") { - this.deepTraverseJson(value, lambda); - } - lambda(json, value, key); - }); - } - -} \ No newline at end of file +} diff --git a/ts/numbering/abstract-numbering.ts b/ts/numbering/abstract-numbering.ts index d987702a8a..f8c3822658 100644 --- a/ts/numbering/abstract-numbering.ts +++ b/ts/numbering/abstract-numbering.ts @@ -40,13 +40,4 @@ export class AbstractNumbering extends XmlComponent { this.addLevel(level); return level; } - - public clearVariables(): void { - _.forEach(this.root, (element) => { - if (element instanceof XmlComponent) { - element.clearVariables(); - } - }); - delete this.id; - } } diff --git a/ts/numbering/index.ts b/ts/numbering/index.ts index fd789faf5f..06b32b1ab2 100644 --- a/ts/numbering/index.ts +++ b/ts/numbering/index.ts @@ -86,14 +86,4 @@ export class Numbering extends XmlComponent { this.root.push(num); return num; } - - public clearVariables(): void { - super.clearVariables(); - _.forEach(this.root, (element) => { - if (element instanceof XmlComponent) { - element.clearVariables(); - } - }); - delete this.nextId; - } } diff --git a/ts/numbering/level.ts b/ts/numbering/level.ts index 8540b00e8f..2882c20589 100644 --- a/ts/numbering/level.ts +++ b/ts/numbering/level.ts @@ -80,14 +80,6 @@ export class Level extends XmlComponent { this.root.push(this.runProperties); } - public clearVariables(): void { - this.paragraphProperties.clearVariables(); - this.runProperties.clearVariables(); - - delete this.paragraphProperties; - delete this.runProperties; - } - public addParagraphProperty(property: XmlComponent): Level { this.paragraphProperties.push(property); return this; diff --git a/ts/numbering/num.ts b/ts/numbering/num.ts index f127817654..1958b989ac 100644 --- a/ts/numbering/num.ts +++ b/ts/numbering/num.ts @@ -34,9 +34,4 @@ export class Num extends XmlComponent { this.root.push(new AbstractNumId(abstractNumId)); this.id = numId; } - - public clearVariables(): void { - super.clearVariables(); - delete this.id; - } } diff --git a/ts/styles/defaults/index.ts b/ts/styles/defaults/index.ts index 65737fcc52..065ed785bd 100644 --- a/ts/styles/defaults/index.ts +++ b/ts/styles/defaults/index.ts @@ -14,11 +14,4 @@ export class DocumentDefaults extends XmlComponent { this.root.push(this.runPropertiesDefaults); this.root.push(this.paragraphPropertiesDefaults); } - - public clearVariables(): void { - this.runPropertiesDefaults.clearVariables(); - this.paragraphPropertiesDefaults.clearVariables(); - delete this.runPropertiesDefaults; - delete this.paragraphPropertiesDefaults; - } } diff --git a/ts/styles/index.ts b/ts/styles/index.ts index f69ebdafd8..c0114c1c54 100644 --- a/ts/styles/index.ts +++ b/ts/styles/index.ts @@ -30,14 +30,6 @@ export class Styles extends XmlComponent { return this; } - public clearVariables(): void { - this.root.forEach((element) => { - if (element instanceof XmlComponent) { - element.clearVariables(); - } - }); - } - public createParagraphStyle(styleId: string, name?: string): ParagraphStyle { const para = new ParagraphStyle(styleId, name); this.push(para); diff --git a/ts/styles/style/index.ts b/ts/styles/style/index.ts index 840e55b317..e2fae80a75 100644 --- a/ts/styles/style/index.ts +++ b/ts/styles/style/index.ts @@ -55,13 +55,6 @@ export class ParagraphStyle extends Style { this.root.push(this.runProperties); } - public clearVariables(): void { - this.paragraphProperties.clearVariables(); - this.runProperties.clearVariables(); - delete this.paragraphProperties; - delete this.runProperties; - } - public addParagraphProperty(property: XmlComponent): void { this.paragraphProperties.push(property); } diff --git a/ts/tests/docx/xml-components/xmlComponentTests.ts b/ts/tests/docx/xml-components/xmlComponentTests.ts index 28f389e4f4..578a5dc2a4 100644 --- a/ts/tests/docx/xml-components/xmlComponentTests.ts +++ b/ts/tests/docx/xml-components/xmlComponentTests.ts @@ -24,13 +24,4 @@ describe("XmlComponent", () => { assert.equal(newJson.rootKey, "w:test"); }); }); - - describe("#replaceKey", () => { - - it("should replace the key to the specified root key", () => { - xmlComponent.replaceKey(); - let newJson = jsonify(xmlComponent); - assert.isDefined(newJson["w:test"]); - }); - }); -}); \ No newline at end of file +});