clear out clearVariables! In comes toXml
This commit is contained in:
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -5,9 +5,5 @@ export abstract class BaseXmlComponent {
|
||||
this.rootKey = rootKey;
|
||||
}
|
||||
|
||||
public abstract replaceKey(): void;
|
||||
|
||||
public clearVariables(): void {
|
||||
// Do Nothing
|
||||
}
|
||||
public abstract toXml(): object;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import * as _ from "lodash";
|
||||
import { BaseXmlComponent } from "./base";
|
||||
export { BaseXmlComponent };
|
||||
|
||||
export abstract class XmlComponent extends BaseXmlComponent {
|
||||
protected root: Array<BaseXmlComponent | string>;
|
||||
@ -9,17 +10,15 @@ export abstract class XmlComponent extends BaseXmlComponent {
|
||||
this.root = new Array<BaseXmlComponent>();
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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"]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user