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-12-18 00:52:29 +00:00
|
|
|
export type AttributeData = { readonly [key: string]: boolean | number | string };
|
|
|
|
export type AttributePayload<T extends AttributeData> = Record<
|
|
|
|
keyof T,
|
|
|
|
{
|
|
|
|
readonly key: string;
|
|
|
|
readonly value?: T[keyof T];
|
|
|
|
}
|
|
|
|
>;
|
|
|
|
|
2022-09-15 20:00:50 +01:00
|
|
|
export abstract class XmlAttributeComponent<T extends object> extends BaseXmlComponent {
|
2019-04-09 05:27:18 -04:00
|
|
|
protected readonly xmlKeys?: AttributeMap<T>;
|
2016-05-20 00:40:31 +01:00
|
|
|
|
2022-12-18 00:52:29 +00:00
|
|
|
public constructor(private readonly root: T) {
|
2017-03-10 10:42:24 +01:00
|
|
|
super("_attr");
|
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
|
|
|
}
|
2022-12-18 00:52:29 +00:00
|
|
|
|
|
|
|
export class NextAttributeComponent<T extends AttributeData> extends BaseXmlComponent {
|
|
|
|
public constructor(private readonly root: AttributePayload<T>) {
|
|
|
|
super("_attr");
|
|
|
|
}
|
|
|
|
|
|
|
|
public prepForXml(_: IContext): IXmlableObject {
|
|
|
|
const attrs = {};
|
|
|
|
Object.values(this.root).forEach(({ key, value }) => {
|
|
|
|
if (value !== undefined) {
|
|
|
|
// eslint-disable-next-line functional/immutable-data
|
|
|
|
attrs[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return { _attr: attrs };
|
|
|
|
}
|
|
|
|
}
|