Hone logo
Hone
Problems

Implementing Vue's v-if Directive in TypeScript

Vue.js is known for its declarative rendering and powerful directives. The v-if directive is fundamental to conditional rendering, allowing developers to dynamically show or hide elements based on data. This challenge asks you to implement a simplified version of this directive in TypeScript to understand its core mechanics.

Problem Description

Your task is to create a function that simulates the behavior of Vue's v-if directive. This function will take a template string, a data object, and a component context as input. It should parse the template, identify elements with a v-if attribute, and conditionally render them based on the truthiness of the expression evaluated against the provided data.

Key requirements:

  • Template Parsing: The function needs to parse an HTML-like template string. You don't need to handle complex HTML parsing; assume well-formed structures for this challenge.
  • v-if Detection: Identify elements that have a v-if attribute.
  • Expression Evaluation: Evaluate the expression specified in the v-if attribute using the provided data object and component context.
  • Conditional Rendering: Render the element and its children only if the v-if expression evaluates to true. If it's false or evaluates to a falsy value, the element should not be included in the output.
  • Nested Directives (Optional but Recommended): Consider how v-if would interact with other directives or nested v-ifs, although for this core implementation, handling simple cases is sufficient.

Expected behavior:

The function should return a string representing the rendered HTML. Elements with v-if evaluating to true should appear in the output, along with their content. Elements with v-if evaluating to false should be omitted entirely.

Examples

Example 1:

Input:
template = "<div v-if='isLoggedIn'><p>Welcome, User!</p></div><button v-if='!isLoggedIn'>Login</button>"
data = { isLoggedIn: true }
context = {}

Output:
"<div><p>Welcome, User!</p></div>"

Explanation:
'isLoggedIn' is true, so the first div is rendered. The 'Login' button has 'v-if' evaluating to false (!true), so it's omitted.

Example 2:

Input:
template = "<h1 v-if='userCount > 0'>Users: {{ userCount }}</h1>"
data = { userCount: 5 }
context = {}

Output:
"<h1>Users: 5</h1>"

Explanation:
'userCount > 0' evaluates to true (5 > 0), so the h1 element is rendered.

Example 3:

Input:
template = "<div v-if='showError'><p>An error occurred.</p></div>"
data = { showError: false }
context = {}

Output:
""

Explanation:
'showError' is false, so the div and its content are completely omitted.

Constraints

  • The template string will contain valid HTML-like tags and attributes.
  • v-if expressions will be simple JavaScript expressions that can be evaluated within the scope of the data object and context. No complex scope chain lookups or function calls within expressions are required for the basic implementation.
  • The evaluation of v-if expressions should be safe and not introduce security vulnerabilities (e.g., using eval is discouraged; a safer approach like a dedicated expression parser or limited evaluation context is preferred).
  • Focus on the v-if logic. You do not need to implement v-bind, v-on, or text interpolation like {{ variable }} for this challenge.

Notes

  • Think about how you would parse the HTML string. Regular expressions can be useful for simpler cases, but consider their limitations. A more robust approach might involve DOM manipulation in a browser environment or a lightweight HTML parser.
  • For expression evaluation, consider how to safely execute simple JavaScript expressions. You might need to create a scope for evaluation.
  • The context parameter is provided for potential future extensions or for simulating a more complete component instance, but for this challenge, you can primarily focus on the data object for expression evaluation.
  • The core of this problem lies in the conditional removal of elements based on an expression.
Loading editor...
typescript