fixing tests and formatter

This commit is contained in:
Dolan Miu
2016-04-09 21:12:55 +01:00
parent f68a2aff56
commit 50e2344d2c
4 changed files with 10 additions and 16 deletions

View File

@ -22,7 +22,6 @@ export class Paragraph extends XmlComponent {
constructor(text?: string) {
super("w:p");
this.root.push(new Attributes());
this.properties = new ParagraphProperties();
this.root.push(this.properties);
this.root.push(new TextRun(text));

View File

@ -4,8 +4,9 @@ export class Formatter {
format(input: any): Object {
this.deepTraverseJson(input, (parent, value, key) => {
if (isNaN(key)) {
var newKey = this.getReplacementKey(parent, key);
if (isNaN(key) && key !== "rootKey") {
var newKey = parent.rootKey;
console.log(key);
if (newKey !== key) {
parent[newKey] = parent[key];
delete parent[key];
@ -30,6 +31,9 @@ export class Formatter {
if (key === "xmlKeys") {
delete parent[key];
}
if (key === "rootKey") {
delete parent[key];
}
});
return newJson
@ -42,21 +46,11 @@ export class Formatter {
private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void {
_.forOwn(json, (value, key) => {
if (_.isObject(value) && key !== "xmlKeys") {
if (_.isObject(value) && key !== "xmlKeys" && key != "rootKey") {
this.deepTraverseJson(value, lambda);
}
lambda(json, value, key);
});
}
private getReplacementKey(input: any, key: string): string {
var newKey = input.xmlKeys[key];
if (newKey !== undefined) {
return newKey;
} else {
return key;
}
}
}

View File

@ -36,11 +36,12 @@ describe("Formatter", () => {
assert(stringifiedJson.indexOf("xmlKeys") < 0);
});
it("should format simple paragraph with bold text", () => {
it.only("should format simple paragraph with bold text", () => {
var paragraph = new docx.Paragraph();
paragraph.addText(new docx.TextRun("test").bold());
var newJson = formatter.format(paragraph);
newJson = jsonify(newJson);
console.log(JSON.stringify(newJson, null, " "));
assert.isDefined(newJson["w:p"][3]["w:r"][0]["w:rPr"][0]["w:b"][0]["_attr"]["w:val"]);
});

View File

@ -35,7 +35,7 @@ describe("Paragraph", () => {
paragraph.heading1();
var newJson = jsonify(paragraph);
assert(newJson.p[1].pPr[1].pStyle[0]._attr.val === "Heading1");
assert(newJson.root[1].root[1].root[0]._attr.val === "Heading1");
});
});