Page MenuHomeSealhub

structured.ts
No OneTemporary

structured.ts

import { JDDContext } from "..";
import {
ComponentArgument,
ExtractComponentArgumentValue,
} from "./component-argument";
export class Structured<
T extends Record<string, ComponentArgument<unknown>>
> extends ComponentArgument<{
[property in keyof T]: ExtractComponentArgumentValue<T[property]>;
}> {
constructor(public structure: T) {
super();
Object.values(structure).forEach((arg) => (arg.parent_argument = this));
}
getTypeName() {
return "structured";
}
countWords(value: {
[property in keyof T]: ExtractComponentArgumentValue<T[property]>;
}): number {
return Object.entries(value).reduce((acc, [key, val]) => {
if (!this.structure[key]) {
console.warn(`Key ${key} doesn't exist in structured argument`);
return acc + 0;
}
return acc + this.structure[key].countWords(val);
}, 0);
}
getEmptyValue() {
return Object.fromEntries(
Object.entries(this.structure).map(([name, arg]) => [
name,
arg.getEmptyValue(),
])
) as {
[property in keyof T]: ExtractComponentArgumentValue<T[property]>;
};
}
async getExampleValue(context: JDDContext) {
return Object.fromEntries(
await Promise.all(
Object.entries(this.structure).map(async ([name, arg]) => [
name,
await arg.getExampleValue(context),
])
)
) as {
[property in keyof T]: ExtractComponentArgumentValue<T[property]>;
};
}
}

File Metadata

Mime Type
text/x-java
Expires
Tue, Jul 8, 07:50 (17 h, 48 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
810422
Default Alt Text
structured.ts (1 KB)

Event Timeline