Files
docx-js/ts/export/formatter.ts

45 lines
1.1 KiB
TypeScript
Raw Normal View History

2016-03-28 00:53:24 +01:00
import * as _ from "lodash";
export class Formatter {
2016-03-31 04:33:06 +01:00
private xmlKeyDictionary = {
p: 'w:p'
};
2016-03-28 00:53:24 +01:00
format(input: any) {
var stringified = JSON.stringify(input);
var newJson = JSON.parse(stringified);
this.deepTraverseJson(newJson, (parent, value, key) => {
2016-03-31 04:33:06 +01:00
if (isNaN(key)) {
var newKey = this.getReplacementKey(key);
parent[newKey] = parent[key];
if (newKey !== key) {
delete parent[key];
}
}
2016-03-28 00:53:24 +01:00
});
2016-03-31 04:33:06 +01:00
console.log(newJson);
2016-03-28 00:53:24 +01:00
return newJson;
}
2016-03-31 04:33:06 +01:00
private deepTraverseJson(json: Object, lambda: (json: any, value: any, key: any) => void) {
_.forOwn(json, (value, key) => {
2016-03-28 00:53:24 +01:00
if (_.isObject(value)) {
this.deepTraverseJson(value, lambda);
}
lambda(json, value, key);
});
2016-03-31 04:33:06 +01:00
}
private getReplacementKey(key: string): string {
var newKey = this.xmlKeyDictionary[key];
if (newKey !== undefined) {
return newKey;
} else {
return key;
}
}
2016-03-28 00:53:24 +01:00
}