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