made formatter replace tags
This commit is contained in:
@ -2,24 +2,44 @@ import * as _ from "lodash";
|
|||||||
|
|
||||||
export class Formatter {
|
export class Formatter {
|
||||||
|
|
||||||
|
private xmlKeyDictionary = {
|
||||||
|
p: 'w:p'
|
||||||
|
};
|
||||||
|
|
||||||
format(input: any) {
|
format(input: any) {
|
||||||
var stringified = JSON.stringify(input);
|
var stringified = JSON.stringify(input);
|
||||||
var newJson = JSON.parse(stringified);
|
var newJson = JSON.parse(stringified);
|
||||||
this.deepTraverseJson(newJson, (parent, value, key) => {
|
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;
|
return newJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
private deepTraverseJson(json, lambda: (json: any, value: any, key: string) => void) {
|
private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void) {
|
||||||
_.forOwn(json, function(value, key) {
|
_.forOwn(json, (value, key) => {
|
||||||
if (_.isObject(value)) {
|
if (_.isObject(value)) {
|
||||||
this.deepTraverseJson(value, lambda);
|
this.deepTraverseJson(value, lambda);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
lambda(json, value, key);
|
lambda(json, value, key);
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
|
private getReplacementKey(key: string): string {
|
||||||
|
var newKey = this.xmlKeyDictionary[key];
|
||||||
|
|
||||||
|
if (newKey !== undefined) {
|
||||||
|
return newKey;
|
||||||
|
} else {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -13,7 +13,7 @@ describe('Document', () => {
|
|||||||
describe('#constructor()', () => {
|
describe('#constructor()', () => {
|
||||||
|
|
||||||
it("should create valid JSON", () => {
|
it("should create valid JSON", () => {
|
||||||
console.log(JSON.stringify(document, null, " "));
|
//console.log(JSON.stringify(document, null, " "));
|
||||||
var stringifiedJson = JSON.stringify(document);
|
var stringifiedJson = JSON.stringify(document);
|
||||||
var newJson;
|
var newJson;
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
import {Formatter} from "../export/Formatter";
|
import {Formatter} from "../export/Formatter";
|
||||||
|
|
||||||
describe('Formatter', () => {
|
describe.only('Formatter', () => {
|
||||||
var formatter: Formatter;
|
var formatter: Formatter;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@ -12,8 +12,11 @@ describe('Formatter', () => {
|
|||||||
|
|
||||||
describe('#format()', () => {
|
describe('#format()', () => {
|
||||||
it("should work", () => {
|
it("should work", () => {
|
||||||
var newJson = formatter.format('{"p":["stuff"]}');
|
var newJson = formatter.format({ "p": [{ "t": "test" }] });
|
||||||
console.log(newJson);
|
});
|
||||||
|
|
||||||
|
it("should should change 'p' tag into 'w:p' tag", () => {
|
||||||
|
var newJson = formatter.format({ "p": "test" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
@ -18,8 +18,6 @@ describe('Paragraph', () => {
|
|||||||
describe('#constructor()', () => {
|
describe('#constructor()', () => {
|
||||||
|
|
||||||
it("should create valid JSON", () => {
|
it("should create valid JSON", () => {
|
||||||
console.log(JSON.stringify(paragraph, null, " "));
|
|
||||||
|
|
||||||
var stringifiedJson = JSON.stringify(paragraph);
|
var stringifiedJson = JSON.stringify(paragraph);
|
||||||
var newJson;
|
var newJson;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user