Files
docx-js/src/file/xml-components/default-attributes.ts

29 lines
958 B
TypeScript
Raw Normal View History

2021-03-11 01:06:55 +00:00
import { BaseXmlComponent, IContext } from "./base";
import { IXmlableObject } from "./xmlable-object";
export type AttributeMap<T> = { readonly [P in keyof T]: string };
export abstract class XmlAttributeComponent<T extends object> extends BaseXmlComponent {
// tslint:disable-next-line:readonly-keyword
protected readonly root: T;
protected readonly xmlKeys?: AttributeMap<T>;
2022-08-31 07:52:27 +01:00
public constructor(properties: T) {
super("_attr");
2016-05-26 15:08:34 +01:00
this.root = properties;
}
2021-03-11 01:06:55 +00:00
public prepForXml(_: IContext): IXmlableObject {
const attrs = {};
Object.keys(this.root).forEach((key) => {
const value = this.root[key];
if (value !== undefined) {
const newKey = (this.xmlKeys && this.xmlKeys[key]) || key;
// eslint-disable-next-line functional/immutable-data
attrs[newKey] = value;
}
});
2018-01-23 01:33:12 +00:00
return { _attr: attrs };
}
2017-03-08 21:49:41 +00:00
}