added replacements and properties removal in formatter

This commit is contained in:
Dolan Miu
2016-03-31 05:28:19 +01:00
parent 167de85cee
commit 30ed93b2e9
2 changed files with 55 additions and 8 deletions

View File

@ -3,27 +3,65 @@ import * as _ from "lodash";
export class Formatter { export class Formatter {
private xmlKeyDictionary = { private xmlKeyDictionary = {
p: 'w:p' p: 'w:p',
t: 'w:t',
color: 'w:color',
space: 'w:space',
sz: 'w:sz',
val: 'w:val',
type: 'w:type',
ilvl: 'w:ilvl',
numId: 'w:numId',
pBdr: 'w:pBdr',
jc: 'w:jc',
r: 'w:r',
pPr: 'w:pPr',
pStyle: 'w:pStyle',
numPr: 'w:numPr',
b: 'w:b',
i: 'w:i',
u: 'w:u',
rPr: 'w:rPr'
}; };
format(input: any) { format(input: any): Object {
var stringified = JSON.stringify(input); var newJson = this.jsonify(input);
var newJson = JSON.parse(stringified);
this.deepTraverseJson(newJson, (parent, value, key) => { this.deepTraverseJson(newJson, (parent, value, key) => {
if (isNaN(key)) { if (isNaN(key)) {
var newKey = this.getReplacementKey(key); var newKey = this.getReplacementKey(key);
parent[newKey] = parent[key]; parent[newKey] = parent[key];
if (newKey !== key) { if (newKey !== key) {
delete parent[key]; delete parent[key];
} else {
console.error("Key is not in dictionary:" + key);
} }
} }
}); });
console.log(newJson);
newJson = this.clenseProperties(newJson);
return newJson; return newJson;
} }
private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void) { private clenseProperties(input: any): Object {
var newJson = this.jsonify(input);
this.deepTraverseJson(newJson, (parent, value, key) => {
if (key === "properties") {
delete parent[key];
}
});
return newJson
}
private jsonify(obj: Object): Object {
var 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) => { _.forOwn(json, (value, key) => {
if (_.isObject(value)) { if (_.isObject(value)) {
this.deepTraverseJson(value, lambda); this.deepTraverseJson(value, lambda);

View File

@ -2,6 +2,12 @@
/// <reference path="../typings/lodash/lodash.d.ts" /> /// <reference path="../typings/lodash/lodash.d.ts" />
import {Formatter} from "../export/Formatter"; import {Formatter} from "../export/Formatter";
import * as docx from "../docx";
function jsonify(obj: Object) {
var stringifiedJson = JSON.stringify(obj);
return JSON.parse(stringifiedJson);
}
describe.only('Formatter', () => { describe.only('Formatter', () => {
var formatter: Formatter; var formatter: Formatter;
@ -11,8 +17,11 @@ describe.only('Formatter', () => {
}); });
describe('#format()', () => { describe('#format()', () => {
it("should work", () => { it("should format simple paragraph", () => {
var newJson = formatter.format({ "p": [{ "t": "test" }] }); var paragraph = new docx.Paragraph();
var newJson = formatter.format(paragraph);
newJson = jsonify(newJson);
console.log(newJson);
}); });
it("should should change 'p' tag into 'w:p' tag", () => { it("should should change 'p' tag into 'w:p' tag", () => {