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 {
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) {
var stringified = JSON.stringify(input);
var newJson = JSON.parse(stringified);
format(input: any): Object {
var newJson = this.jsonify(input);
this.deepTraverseJson(newJson, (parent, value, key) => {
if (isNaN(key)) {
var newKey = this.getReplacementKey(key);
parent[newKey] = parent[key];
if (newKey !== key) {
delete parent[key];
} else {
console.error("Key is not in dictionary:" + key);
}
}
});
console.log(newJson);
newJson = this.clenseProperties(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) => {
if (_.isObject(value)) {
this.deepTraverseJson(value, lambda);