mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-15 18:30:39 +09:00
Add draft routine defaults and run-time overrides
This commit is contained in:
parent
b4a58ba8a6
commit
5d021583be
18 changed files with 592 additions and 113 deletions
|
|
@ -3,6 +3,26 @@ import type { RoutineVariable } from "./types/routine.js";
|
|||
const ROUTINE_VARIABLE_MATCHER = /\{\{\s*([A-Za-z][A-Za-z0-9_]*)\s*\}\}/g;
|
||||
type RoutineTemplateInput = string | null | undefined | Array<string | null | undefined>;
|
||||
|
||||
/**
|
||||
* Built-in variable names that are automatically available in routine templates
|
||||
* without needing to be defined in the routine's variables list.
|
||||
*/
|
||||
export const BUILTIN_ROUTINE_VARIABLE_NAMES = new Set(["date"]);
|
||||
|
||||
export function isBuiltinRoutineVariable(name: string): boolean {
|
||||
return BUILTIN_ROUTINE_VARIABLE_NAMES.has(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current values for all built-in routine variables.
|
||||
* `date` expands to the current date in YYYY-MM-DD format (UTC).
|
||||
*/
|
||||
export function getBuiltinRoutineVariableValues(): Record<string, string> {
|
||||
return {
|
||||
date: new Date().toISOString().slice(0, 10),
|
||||
};
|
||||
}
|
||||
|
||||
export function isValidRoutineVariableName(name: string): boolean {
|
||||
return /^[A-Za-z][A-Za-z0-9_]*$/.test(name);
|
||||
}
|
||||
|
|
@ -40,7 +60,7 @@ export function syncRoutineVariablesWithTemplate(
|
|||
template: RoutineTemplateInput,
|
||||
existing: RoutineVariable[] | null | undefined,
|
||||
): RoutineVariable[] {
|
||||
const names = extractRoutineVariableNames(template);
|
||||
const names = extractRoutineVariableNames(template).filter((name) => !isBuiltinRoutineVariable(name));
|
||||
const existingByName = new Map((existing ?? []).map((variable) => [variable.name, variable]));
|
||||
return names.map((name) => existingByName.get(name) ?? defaultRoutineVariable(name));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue