61 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-02-27 21:51:03 +01:00
import { PdfFile } from "../wrappers/PdfFile";
import { Action } from "../../declarations/Action";
import Joi from "@stirling-tools/joi";
export interface ValidationResult {
valid: boolean,
reason?: string
}
export interface Progress {
/** A percentage between 0-1 describing the progress on the currently processed file */
curFileProgress: number,
/** A percentage between 0-1 describing the progress on all input files / operations */
operationProgress: number,
}
export class Operator {
2023-11-27 23:35:18 +01:00
/** The internal name of the operator in camelCase (impose, merge, etc.) */
static type: string;
2023-11-27 23:35:18 +01:00
/** The Joi validators & decorators */
2023-12-21 15:57:51 +01:00
protected static inputSchema: Joi.Schema;
protected static valueSchema: Joi.Schema;
protected static outputSchema: Joi.Schema;
static schema: Joi.ObjectSchema<{
input: any;
values: any;
output: any;
2023-12-21 15:57:51 +01:00
}>;
actionValues: any;
constructor (action: Action) {
this.actionValues = action.values;
}
2024-02-25 20:55:48 +01:00
async run(input: PdfFile[] | any[], progressCallback: (progress: Progress) => void): Promise<PdfFile[] | any[]> {
return [];
}
2023-11-27 23:35:18 +01:00
}
2023-11-27 23:35:18 +01:00
/** This function should be used if the Operation may take multiple files as inputs and only outputs one file */
export async function nToOne <I, O>(inputs: I[], callback: (input: I[]) => Promise<O>): Promise<O[]> {
return [await callback(inputs)];
}
2023-11-27 23:35:18 +01:00
/** This function should be used if the Operation takes one file as input and may output multiple files */
export async function oneToN <I, O>(inputs: I[], callback: (input: I, index: number, max: number) => Promise<O[]>): Promise<O[]> {
2024-01-04 20:17:54 -05:00
let output: O[] = [];
2023-11-27 23:35:18 +01:00
for (let i = 0; i < inputs.length; i++) {
output = output.concat(await callback(inputs[i], i, inputs.length));
}
2023-11-27 23:35:18 +01:00
return output;
}
2023-11-27 23:35:18 +01:00
/** This function should be used if the Operation takes one file as input and outputs only one file */
export async function oneToOne <I, O>(inputs: I[], callback: (input: I, index: number, max: number) => Promise<O>): Promise<O[]> {
return oneToN(inputs, async (input, index, max) => {
2024-01-04 20:17:54 -05:00
return [await callback(input, index, max)];
2023-11-27 23:35:18 +01:00
});
2023-11-20 22:12:03 +01:00
}