Page MenuHomeSealhub

single-reference.ts
No OneTemporary

single-reference.ts

import { is, predicates } from "@sealcode/ts-predicates";
import { JDDContext } from "../jdd-context.js";
import { MaybePromise } from "../utils/util-types.js";
import { StringBasedArgument } from "./string-based-argument.js";
import { Collection, CollectionItem, Context } from "sealious";
function pickRandom<T>(array: T[]): T {
return array[Math.floor(Math.random() * array.length)];
}
export class SingleReference<
C extends Collection
> extends StringBasedArgument<string> {
constructor(
public getSealiousCollection: () => Promise<C>,
public getSealiousContext: (jdd_context: JDDContext) => Context,
public itemToLabel: (item: CollectionItem<C>) => Promise<string>
) {
super();
}
getTypeName() {
return "single-reference";
}
async getEmptyValue(jdd_ctx: JDDContext) {
const context = this.getSealiousContext(jdd_ctx);
const ids = (
await (await this.getSealiousCollection())
.list(context)
.paginate({ items: 5 })
.fetch()
).items.map(({ id }) => id);
if (!ids.length) {
return "";
} else {
return pickRandom(ids);
}
}
async getValues(
jdd_ctx: JDDContext
): Promise<{ label: string; value: string }[]> {
const context = this.getSealiousContext(jdd_ctx);
const { items } = await (await this.getSealiousCollection())
.list(context)
.fetch();
const labels = Promise.all(
items.map(async (item) => ({
value: item.id,
label: await this.itemToLabel(item),
}))
);
return labels;
}
getExampleValue(context: JDDContext) {
return this.getEmptyValue(context);
}
countWords(): number {
return 0;
}
example_values = [];
parseFormInput(
_: JDDContext,
input: unknown
): MaybePromise<string | string[] | null> {
if (is(input, predicates.string)) {
return input;
} else {
return null;
}
}
}

File Metadata

Mime Type
text/x-Algol68
Expires
Thu, Jan 23, 19:19 (20 h, 5 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
600841
Default Alt Text
single-reference.ts (1 KB)

Event Timeline