Skip to content

feat: Add handlebars helpers for math, arrays, and json - #408

Open
tbuckley wants to merge 2 commits into
mainfrom
handlebars-math-arrays-json
Open

feat: Add handlebars helpers for math, arrays, and json#408
tbuckley wants to merge 2 commits into
mainfrom
handlebars-math-arrays-json

Conversation

@tbuckley

Copy link
Copy Markdown
Owner

No description provided.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 21, 2025

Copy link
Copy Markdown

Deploying evals-pwa with  Cloudflare Pages  Cloudflare Pages

Latest commit: bd4c28d
Status: ✅  Deploy successful!
Preview URL: https://ccb12067.evals-pwa.pages.dev
Branch Preview URL: https://handlebars-math-arrays-json.evals-pwa.pages.dev

View logs

cursor[bot]

This comment was marked as outdated.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Handlebars Helper Bugs: Type Errors and Name Collisions

The recent addition of Handlebars helpers introduces two issues:

  1. Array Helper Flaws: The first, last, rest, length, and slice array helpers lack runtime type checking, causing errors when used with non-array inputs (e.g., null, undefined, strings). Additionally, their TypeScript types are incorrectly string[] instead of any[], leading to compilation errors for non-string arrays.
  2. Name Collisions: Many new helpers (first, last, rest, length, slice, add, sub, mult, div, json) use common names. Due to Handlebars' helper precedence, these helpers shadow existing variables with the same names, causing breaking changes in templates that previously accessed those variables (e.g., {{ first }} now invokes the helper).

src/lib/utils/HandlebarsPromptFormatter.ts#L34-L43

// Array
Handlebars.registerHelper('first', (a: string[]) => a[0]);
Handlebars.registerHelper('last', (a: string[]) => a[a.length - 1]);
Handlebars.registerHelper('rest', (a: string[]) => a.slice(1));
Handlebars.registerHelper('length', (a: string[]) => a.length);
Handlebars.registerHelper('slice', (a: string[], start: number, end: number) =>
a.slice(start, end),
);
// JSON
Handlebars.registerHelper('json', (a: unknown) => JSON.stringify(a));

Fix in CursorFix in Web


Was this report helpful? Give feedback by reacting with 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant