made formatter replace tags

This commit is contained in:
Dolan Miu
2016-03-31 04:33:06 +01:00
parent 4f4653200e
commit 167de85cee
4 changed files with 33 additions and 12 deletions

View File

@ -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;
}
}
}

View File

@ -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;

View File

@ -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" });
});
});
});

View File

@ -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;