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 {
|
public addParagraph(paragraph: Paragraph): void {
|
||||||
this.body.push(paragraph);
|
this.body.push(paragraph);
|
||||||
}
|
}
|
||||||
|
|
||||||
public clearVariables(): void {
|
|
||||||
this.body.clearVariables();
|
|
||||||
delete this.body;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -5,9 +5,5 @@ export abstract class BaseXmlComponent {
|
|||||||
this.rootKey = rootKey;
|
this.rootKey = rootKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract replaceKey(): void;
|
public abstract toXml(): object;
|
||||||
|
|
||||||
public clearVariables(): void {
|
|
||||||
// Do Nothing
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -16,15 +16,18 @@ export abstract class XmlAttributeComponent extends BaseXmlComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public replaceKey(): void {
|
public toXml(): object {
|
||||||
if (this.root !== undefined) {
|
const attrs = {};
|
||||||
|
if (this.root != undefined) {
|
||||||
_.forOwn(this.root, (value, key) => {
|
_.forOwn(this.root, (value, key) => {
|
||||||
const newKey = this.xmlKeys[key];
|
if (value != undefined) {
|
||||||
this.root[newKey] = value;
|
const newKey = this.xmlKeys[key];
|
||||||
delete this.root[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 * as _ from "lodash";
|
||||||
import { BaseXmlComponent } from "./base";
|
import { BaseXmlComponent } from "./base";
|
||||||
|
export { BaseXmlComponent };
|
||||||
|
|
||||||
export abstract class XmlComponent extends BaseXmlComponent {
|
export abstract class XmlComponent extends BaseXmlComponent {
|
||||||
protected root: Array<BaseXmlComponent | string>;
|
protected root: Array<BaseXmlComponent | string>;
|
||||||
@ -9,17 +10,15 @@ export abstract class XmlComponent extends BaseXmlComponent {
|
|||||||
this.root = new Array<BaseXmlComponent>();
|
this.root = new Array<BaseXmlComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public replaceKey(): void {
|
public toXml(): object {
|
||||||
// console.log(this.rootKey);
|
const ret = this.root.map((comp) => {
|
||||||
// console.log(this.root);
|
if (comp instanceof BaseXmlComponent) {
|
||||||
if (this.root !== undefined) {
|
return comp.toXml();
|
||||||
this.root.forEach((root) => {
|
}
|
||||||
if (root && root instanceof BaseXmlComponent) {
|
return comp
|
||||||
root.replaceKey();
|
}).filter((comp) => comp); // Exclude null, undefined, and empty strings
|
||||||
}
|
return {
|
||||||
});
|
[this.rootKey]: ret,
|
||||||
this[this.rootKey] = this.root;
|
|
||||||
delete this.root;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,52 +1,7 @@
|
|||||||
import * as _ from "lodash";
|
import { BaseXmlComponent } from "../docx/xml-components";
|
||||||
import {XmlComponent} from "../docx/xml-components";
|
|
||||||
|
|
||||||
export class Formatter {
|
export class Formatter {
|
||||||
|
public format(input: BaseXmlComponent): Object {
|
||||||
public format(input: any): Object {
|
return input.toXml();
|
||||||
input.clearVariables();
|
|
||||||
this.replaceKeys(input);
|
|
||||||
const newJson = this.clense(input);
|
|
||||||
// console.log(JSON.stringify(newJson, null, " "));
|
|
||||||
return newJson;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
this.addLevel(level);
|
||||||
return 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);
|
this.root.push(num);
|
||||||
return 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);
|
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 {
|
public addParagraphProperty(property: XmlComponent): Level {
|
||||||
this.paragraphProperties.push(property);
|
this.paragraphProperties.push(property);
|
||||||
return this;
|
return this;
|
||||||
|
@ -34,9 +34,4 @@ export class Num extends XmlComponent {
|
|||||||
this.root.push(new AbstractNumId(abstractNumId));
|
this.root.push(new AbstractNumId(abstractNumId));
|
||||||
this.id = numId;
|
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.runPropertiesDefaults);
|
||||||
this.root.push(this.paragraphPropertiesDefaults);
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public clearVariables(): void {
|
|
||||||
this.root.forEach((element) => {
|
|
||||||
if (element instanceof XmlComponent) {
|
|
||||||
element.clearVariables();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public createParagraphStyle(styleId: string, name?: string): ParagraphStyle {
|
public createParagraphStyle(styleId: string, name?: string): ParagraphStyle {
|
||||||
const para = new ParagraphStyle(styleId, name);
|
const para = new ParagraphStyle(styleId, name);
|
||||||
this.push(para);
|
this.push(para);
|
||||||
|
@ -55,13 +55,6 @@ export class ParagraphStyle extends Style {
|
|||||||
this.root.push(this.runProperties);
|
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 {
|
public addParagraphProperty(property: XmlComponent): void {
|
||||||
this.paragraphProperties.push(property);
|
this.paragraphProperties.push(property);
|
||||||
}
|
}
|
||||||
|
@ -24,13 +24,4 @@ describe("XmlComponent", () => {
|
|||||||
assert.equal(newJson.rootKey, "w:test");
|
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