Skip to content

Use handlebars template engine for context vars substitutions #819

@marrouchi

Description

@marrouchi

The processTokenReplacements function is currently implementing manual string replacement logic to replace tokens in a text message with values from various context objects. This approach is error-prone, difficult to maintain, and not scalable for complex token replacement scenarios.

To improve maintainability and scalability, refactor this function to use [Handlebars.js](https://handlebarsjs.com/) for token replacement. Handlebars provides a robust templating engine that simplifies token substitution and allows for more complex use cases.

However, before refactoring the function, we need to ensure that comprehensive unit tests are in place to verify the correctness of the functionality post-change.


Tasks:

  1. Write Exhaustive Unit Tests:

    • Ensure that unit tests cover all possible cases for token replacement, including:
      • Simple token replacements (e.g., {context.vars.phone}).
      • Nested token replacements (e.g., {context.user_location.address.street}).
      • Missing or null values for tokens.
      • Edge cases with empty input or invalid context.
    • Include tests for the merged context (e.g., combining subscriberContext.vars, context.vars, context.user_location, context.user, and settings.contact).
  2. Integrate Handlebars:

    • Add Handlebars.js as a dependency to the project (if not already included).
  3. Refactor the Function:

    • Replace the current manual string replacement logic with Handlebars templates.
    • Generate a single Handlebars template from the input text.
    • Compile the template with the combined context (context, subscriberContext, settings, etc.) passed to Handlebars.
  4. Verify Unit Test Coverage:

    • Ensure the tests created in step 1 cover all branches of the code and edge cases.
  5. Update Documentation:

    • Update the function's JSDoc comments to reflect the use of Handlebars and the expected input/output.
    • Provide examples of supported token formats.
  6. Performance Testing:

    • Verify that the updated implementation performs efficiently for large inputs.

Acceptance Criteria:

  • Exhaustive unit tests are written, covering all cases and edge cases for token replacement.
  • The function uses Handlebars for token replacement.
  • All unit tests pass successfully.
  • The function's documentation is updated to reflect the changes.
  • No regressions are introduced in performance or functionality.

Additional Context:

Current function example:

const text = "Your phone number is {context.vars.phone}";
const context = { vars: { phone: "6354-543-534" } };
processTokenReplacements(text, context, subscriberContext, settings);
// Output: "Your phone number is 6354-543-534"

Post-refactor example with Handlebars:

const text = "Your phone number is {{context.vars.phone}}";
const context = { vars: { phone: "6354-543-534" } };
processTokenReplacements(text, context, subscriberContext, settings);
// Output: "Your phone number is 6354-543-534"

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions