2016-03-28 00:53:24 +01:00
|
|
|
import * as _ from "lodash";
|
2016-04-10 05:09:02 +01:00
|
|
|
import {XmlComponent} from "../docx/xml-components";
|
2016-03-28 00:53:24 +01:00
|
|
|
|
|
|
|
export class Formatter {
|
|
|
|
|
2017-03-08 21:54:52 +00:00
|
|
|
public format(input: any): Object {
|
2016-05-01 22:24:20 +01:00
|
|
|
input.clearVariables();
|
2016-05-10 00:31:39 +01:00
|
|
|
this.replaceKeys(input);
|
2017-03-08 21:54:52 +00:00
|
|
|
const newJson = this.clense(input);
|
2016-05-26 15:08:34 +01:00
|
|
|
// console.log(JSON.stringify(newJson, null, " "));
|
2016-04-10 05:09:02 +01:00
|
|
|
return newJson;
|
|
|
|
}
|
|
|
|
|
|
|
|
private replaceKeys(input: XmlComponent): Object {
|
|
|
|
input.replaceKey();
|
2016-05-01 22:24:20 +01:00
|
|
|
|
2016-04-10 05:09:02 +01:00
|
|
|
return input;
|
2016-03-28 00:53:24 +01:00
|
|
|
}
|
|
|
|
|
2016-04-03 01:44:18 +01:00
|
|
|
private clense(input: any): Object {
|
2017-03-08 21:54:52 +00:00
|
|
|
const newJson = this.jsonify(input);
|
2016-03-31 05:28:19 +01:00
|
|
|
|
|
|
|
this.deepTraverseJson(newJson, (parent, value, key) => {
|
|
|
|
if (key === "properties") {
|
|
|
|
delete parent[key];
|
|
|
|
}
|
2016-04-03 01:44:18 +01:00
|
|
|
if (key === "xmlKeys") {
|
|
|
|
delete parent[key];
|
|
|
|
}
|
2016-04-09 21:12:55 +01:00
|
|
|
if (key === "rootKey") {
|
|
|
|
delete parent[key];
|
|
|
|
}
|
2016-03-31 05:28:19 +01:00
|
|
|
});
|
2016-04-03 01:44:18 +01:00
|
|
|
|
2016-05-23 22:23:05 +01:00
|
|
|
return newJson;
|
2016-03-31 05:28:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private jsonify(obj: Object): Object {
|
2016-05-26 15:08:34 +01:00
|
|
|
let stringifiedJson = JSON.stringify(obj);
|
2016-03-31 05:28:19 +01:00
|
|
|
return JSON.parse(stringifiedJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void): void {
|
2016-03-31 04:33:06 +01:00
|
|
|
_.forOwn(json, (value, key) => {
|
2016-05-26 15:08:34 +01:00
|
|
|
if (_.isObject(value) && key !== "xmlKeys" && key !== "rootKey") {
|
2016-03-28 00:53:24 +01:00
|
|
|
this.deepTraverseJson(value, lambda);
|
|
|
|
}
|
|
|
|
lambda(json, value, key);
|
|
|
|
});
|
2016-03-31 04:33:06 +01:00
|
|
|
}
|
|
|
|
|
2016-03-28 00:53:24 +01:00
|
|
|
}
|