Files
docx-js/ts/export/formatter.ts
2016-05-26 15:08:34 +01:00

52 lines
1.4 KiB
TypeScript

import * as _ from "lodash";
import {XmlComponent} from "../docx/xml-components";
export class Formatter {
format(input: any): Object {
input.clearVariables();
this.replaceKeys(input);
let newJson = this.clense(input);
// console.log(JSON.stringify(newJson, null, " "));
return newJson;
}
private replaceKeys(input: XmlComponent): Object {
input.replaceKey();
return input;
}
private clense(input: any): Object {
let newJson = this.jsonify(input);
this.deepTraverseJson(newJson, (parent, value, key) => {
if (key === "properties") {
delete parent[key];
}
if (key === "xmlKeys") {
delete parent[key];
}
if (key === "rootKey") {
delete parent[key];
}
});
return newJson;
}
private jsonify(obj: Object): Object {
let 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) && key !== "xmlKeys" && key !== "rootKey") {
this.deepTraverseJson(value, lambda);
}
lambda(json, value, key);
});
}
}