Hone logo
Hone
Problems

Vue Transform Pipeline with TypeScript

This challenge focuses on building a reusable and flexible transform pipeline within a Vue component using TypeScript. Transform pipelines are useful for applying a series of operations to data, such as formatting, validation, or enrichment, before displaying or processing it. This exercise will help you solidify your understanding of Vue reactivity, TypeScript types, and functional composition.

Problem Description

You need to create a Vue component that accepts an initial data value and a series of transformation functions. The component should apply these transformations sequentially to the initial data, updating the displayed value whenever a transformation function changes the data. The component should be generic, allowing it to work with different data types and transformation functions.

Key Requirements:

  • Generic Type: The component should be generic, accepting a type parameter T for the initial data type.
  • Transformation Functions: Accept an array of transformation functions, each taking a value of type T and returning a value of type U (where U can be the same as T or a different type).
  • Reactivity: The displayed value should be reactive, updating automatically whenever any of the transformation functions change.
  • Clear Interface: Provide a clear and concise interface for defining the initial data and transformation functions.
  • Error Handling: Handle potential errors that might occur within the transformation functions gracefully (e.g., by logging the error and displaying a default value).

Expected Behavior:

  1. The component should initialize with the provided initial data.
  2. The component should apply the transformation functions sequentially to the initial data.
  3. The component should display the final transformed value.
  4. If any transformation function throws an error, the error should be logged, and a default value (e.g., "Error") should be displayed.
  5. Changes to the transformation functions should trigger re-evaluation of the pipeline and update the displayed value.

Edge Cases to Consider:

  • Empty array of transformation functions.
  • Transformation functions that return null or undefined.
  • Transformation functions that throw errors.
  • Transformation functions that change the data type significantly.

Examples

Example 1:

Input:
initialData: 10
transformations: [
  (x) => x * 2,
  (x) => x + 5,
  (x) => String(x)
]
Output: "25"
Explanation: 10 is multiplied by 2 (20), then 5 is added (25), and finally converted to a string ("25").

Example 2:

Input:
initialData: "hello"
transformations: [
  (x) => x.toUpperCase(),
  (x) => x + " world"
]
Output: "HELLO WORLD"
Explanation: "hello" is converted to uppercase ("HELLO"), and then " world" is appended ("HELLO WORLD").

Example 3: (Error Handling)

Input:
initialData: 5
transformations: [
  (x) => x * 2,
  (x) => { throw new Error("Simulated error"); },
  (x) => x + 5
]
Output: "Error" (or a similar error message)
Explanation: The first transformation succeeds (10). The second transformation throws an error, which is caught, and the component displays "Error". The third transformation is not executed.

Constraints

  • The component should be written in Vue 3 with TypeScript.
  • The transformation functions should be defined as separate functions, not inline within the component.
  • The component should be reusable and easily adaptable to different data types and transformation functions.
  • Performance should be reasonable for typical use cases (e.g., a small number of transformations and relatively simple operations). Avoid unnecessary re-renders.

Notes

  • Consider using Vue's computed property to implement the transform pipeline. This will ensure reactivity and efficient updates.
  • Think about how to handle errors gracefully within the transformation functions. A try-catch block is a good approach.
  • The generic type parameter T allows for flexibility in the data type being transformed.
  • Focus on creating a clean, readable, and maintainable solution. Proper TypeScript typing is crucial.
  • You can use a functional approach to compose the transformation functions for better readability.
Loading editor...
typescript