Page MenuHomeSealhub

No OneTemporary

diff --git a/src/component.test.ts b/src/component.test.ts
index 03b3750..d4c7f38 100644
--- a/src/component.test.ts
+++ b/src/component.test.ts
@@ -1,75 +1,80 @@
import assert from "assert";
import { Table } from "./component-arguments/table.js";
import { Component } from "./component.js";
import {
List,
ShortText,
Structured,
makeSimpleEnglishJDDContext,
makeSimpleJDDContext,
} from "./index.js";
import { FileManager } from "@sealcode/file-manager";
describe("component class", () => {
describe("getSubArgument", () => {
it("traverses argument path that includes a table", () => {
const args = {
table: new Table(
new Structured({ title: new ShortText() }),
new Structured({ tags: new List(new ShortText()) })
),
};
const component = new (class extends Component<typeof args> {
getArguments() {
return args;
}
toHTML() {
return "";
}
})();
const [arg, _, values] = component.getArgumentAtPath(
"table/rows/2/cells/0/tags".split("/"),
{
table: {
rows: [
{
type: "header",
header_content: { title: "hehe" },
},
{
type: "header",
header_content: { title: "hehe2" },
},
{
type: "row",
cells: [{ tags: ["tag1", "tag2"] }],
},
],
},
}
);
assert(arg instanceof List);
assert.deepStrictEqual(values, ["tag1", "tag2"]);
});
});
it("Doesn't fail if provided with unknown arguments", async () => {
const args = {
title: new ShortText(),
};
const component = new (class extends Component<typeof args> {
getArguments() {
return args;
}
toHTML() {
return "";
}
})();
- const result = await component.convertStorageToParsed(
+ await component.convertStorageToParsed(
+ makeSimpleEnglishJDDContext(new FileManager("/tmp", "/tmp")),
+ { title2: "string" } as any
+ );
+
+ await component.convertParsedToStorage(
makeSimpleEnglishJDDContext(new FileManager("/tmp", "/tmp")),
{ title2: "string" } as any
);
});
});
diff --git a/src/component.ts b/src/component.ts
index 3e8c044..79e03f7 100644
--- a/src/component.ts
+++ b/src/component.ts
@@ -1,163 +1,163 @@
import { FlatTemplatable } from "tempstream";
import {
ComponentArgument,
ExtractStructuredComponentArgumentsParsed,
ExtractStructuredComponentArgumentsReceived,
ExtractStructuredComponentArgumentsStorage,
JDDContext,
} from "./index.js";
export interface ComponentConstructor<
A extends Record<string, ComponentArgument<unknown>> = Record<
string,
ComponentArgument<unknown>
>
> {
new (): Component<A>;
}
export type EarlyAsset = (
| { type: "script" | "style"; url: string; integrity?: string }
| { type: "script" | "style"; content: string }
) & { identity: string }; // identity key will be used for deduplication
export type ComponentToHTMLArgs<
T extends Record<string, ComponentArgument<unknown>>
> = {
args: ExtractStructuredComponentArgumentsParsed<T>;
classes: string[];
jdd_context: JDDContext;
index: number;
};
export abstract class Component<
ArgumentsT extends Record<string, ComponentArgument<unknown>> = Record<
string,
ComponentArgument<unknown>
>
> {
abstract getArguments(): ArgumentsT;
abstract toHTML(
params: ComponentToHTMLArgs<ArgumentsT>
): FlatTemplatable | Promise<FlatTemplatable>;
async getEarlyAssets(
_args: ExtractStructuredComponentArgumentsParsed<ArgumentsT>,
_context: JDDContext
): Promise<EarlyAsset[]> {
return [];
}
countWords(args: Record<string, unknown>): number {
return Object.entries(args).reduce((acc, [arg_name, value]) => {
const arg = this.getArguments()[arg_name];
if (!arg) {
console.warn(
`Arguemnt ${arg_name} was not found in the component`
);
return acc + 0;
}
return acc + arg.countWords(value);
}, 0);
}
async getExampleValues(
context: JDDContext
): Promise<ExtractStructuredComponentArgumentsParsed<ArgumentsT>> {
return Object.fromEntries(
await Promise.all(
Object.entries(this.getArguments()).map(
async ([key, value]) => [
key,
await value.getExampleValue(context),
]
)
)
) as ExtractStructuredComponentArgumentsParsed<ArgumentsT>;
}
async convertReceivedValuesToParsed(
context: JDDContext,
values: ExtractStructuredComponentArgumentsReceived<ArgumentsT>
): Promise<ExtractStructuredComponentArgumentsParsed<ArgumentsT>> {
const args = this.getArguments();
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Object.fromEntries(
await Promise.all(
Object.entries(values).map(async ([key, value]) => {
return [
key,
await args[key].receivedToParsed(context, value),
];
})
)
);
}
async convertParsedToStorage(
context: JDDContext,
values: ExtractStructuredComponentArgumentsParsed<ArgumentsT>
): Promise<ExtractStructuredComponentArgumentsStorage<ArgumentsT>> {
const args = this.getArguments();
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Object.fromEntries(
await Promise.all(
Object.entries(values).map(async ([key, value]) => {
return [
key,
- await args[key].parsedToStorage(context, value),
+ await args[key]?.parsedToStorage(context, value),
];
})
)
);
}
async convertStorageToParsed(
context: JDDContext,
values: ExtractStructuredComponentArgumentsStorage<ArgumentsT>
): Promise<ExtractStructuredComponentArgumentsParsed<ArgumentsT>> {
const args = this.getArguments();
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Object.fromEntries(
await Promise.all(
Object.entries(values).map(async ([key, value]) => {
if (!args[key]) {
return [key, null];
}
return [
key,
await args[key].storageToParsed(context, value),
];
})
)
);
}
// returns the argument, remaining path, and the values for that argument
getArgumentAtPath(
argument_path: string[],
values: ExtractStructuredComponentArgumentsParsed<ArgumentsT>
): [ComponentArgument<unknown> | null, string[], unknown] {
argument_path = [...argument_path];
if (argument_path.length == 0) {
return [null, [], null];
}
const arg_name = argument_path.shift() as string;
let argument: ComponentArgument<unknown> | null =
this.getArguments()[arg_name];
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
values = values[arg_name] as any;
if (argument_path.length == 0) {
return [argument, [], values];
}
do {
// the getSubArgument method can consume as many keys from the path as it wants
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
[argument, argument_path, values] = argument.getSubArgument(
argument_path,
values
) as any;
} while (argument_path.length && argument !== null);
return [argument, argument_path, values];
}
}

File Metadata

Mime Type
text/x-diff
Expires
Mon, Dec 23, 06:22 (20 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
556261
Default Alt Text
(6 KB)

Event Timeline