diff --git a/ts/export/formatter.ts b/ts/export/formatter.ts index a1eceb33b9..105ab7f4ca 100644 --- a/ts/export/formatter.ts +++ b/ts/export/formatter.ts @@ -2,24 +2,44 @@ import * as _ from "lodash"; export class Formatter { + private xmlKeyDictionary = { + p: 'w:p' + }; + format(input: any) { var stringified = JSON.stringify(input); var newJson = JSON.parse(stringified); this.deepTraverseJson(newJson, (parent, value, key) => { - //parent.blah = parent[key]; - + if (isNaN(key)) { + var newKey = this.getReplacementKey(key); + parent[newKey] = parent[key]; + if (newKey !== key) { + delete parent[key]; + } + } }); + console.log(newJson); return newJson; } - private deepTraverseJson(json, lambda: (json: any, value: any, key: string) => void) { - _.forOwn(json, function(value, key) { + private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void) { + _.forOwn(json, (value, key) => { if (_.isObject(value)) { this.deepTraverseJson(value, lambda); - return; } lambda(json, value, key); }); - }; + } + + private getReplacementKey(key: string): string { + var newKey = this.xmlKeyDictionary[key]; + + if (newKey !== undefined) { + return newKey; + } else { + return key; + } + } + } \ No newline at end of file diff --git a/ts/tests/documentTest.ts b/ts/tests/documentTest.ts index 9b8dc70e16..0203424191 100644 --- a/ts/tests/documentTest.ts +++ b/ts/tests/documentTest.ts @@ -13,7 +13,7 @@ describe('Document', () => { describe('#constructor()', () => { it("should create valid JSON", () => { - console.log(JSON.stringify(document, null, " ")); + //console.log(JSON.stringify(document, null, " ")); var stringifiedJson = JSON.stringify(document); var newJson; diff --git a/ts/tests/formatterTest.ts b/ts/tests/formatterTest.ts index 8e56e13996..cd0ac2c670 100644 --- a/ts/tests/formatterTest.ts +++ b/ts/tests/formatterTest.ts @@ -3,7 +3,7 @@ import {Formatter} from "../export/Formatter"; -describe('Formatter', () => { +describe.only('Formatter', () => { var formatter: Formatter; beforeEach(() => { @@ -12,8 +12,11 @@ describe('Formatter', () => { describe('#format()', () => { it("should work", () => { - var newJson = formatter.format('{"p":["stuff"]}'); - console.log(newJson); + var newJson = formatter.format({ "p": [{ "t": "test" }] }); + }); + + it("should should change 'p' tag into 'w:p' tag", () => { + var newJson = formatter.format({ "p": "test" }); }); }); }); \ No newline at end of file diff --git a/ts/tests/paragraphTest.ts b/ts/tests/paragraphTest.ts index 701b260012..3e3c54a088 100644 --- a/ts/tests/paragraphTest.ts +++ b/ts/tests/paragraphTest.ts @@ -18,8 +18,6 @@ describe('Paragraph', () => { describe('#constructor()', () => { it("should create valid JSON", () => { - console.log(JSON.stringify(paragraph, null, " ")); - var stringifiedJson = JSON.stringify(paragraph); var newJson;