Hone logo
Hone
Problems

Vue Transform Pipeline

You are tasked with building a reusable Vue.js component that can apply a series of transformations to data. This component will act as a pipeline, allowing users to define a sequence of operations (transforms) that will be executed in order on an input value. This is useful for scenarios like data formatting, validation, or complex data manipulation before rendering.

Problem Description

Implement a Vue component, TransformPipeline, that accepts an array of transformation functions and an initial input value. The component should render the final transformed output of the input value after it has passed through all the provided transformations.

Key Requirements:

  1. Component Structure: Create a Vue.js component (using TypeScript with the Composition API) named TransformPipeline.
  2. Props:
    • transforms: An array of functions. Each function should accept one argument (the current value) and return the transformed value. The type of these functions should be (value: any) => any.
    • initialValue: The initial data to be passed through the pipeline. Its type can be any.
  3. Transformation Logic: The component must iterate through the transforms array and apply each function sequentially. The output of one function becomes the input for the next.
  4. Rendering: The component should display the final transformed value. You can use a simple <div> or <p> tag for this.
  5. Reactivity: If initialValue or transforms change reactively, the pipeline should re-run and update the displayed output.

Expected Behavior:

Given an initialValue and a list of transforms, the component should compute the result of transformN(...transformN-1(...transform1(initialValue)...)).

Edge Cases to Consider:

  • Empty transforms array: If no transformations are provided, the initialValue should be displayed directly.
  • initialValue being null or undefined: The transformations should handle these values gracefully.
  • Transformations returning null or undefined: The pipeline should continue processing with these values.
  • Non-function elements in transforms: While ideally the input will be clean, consider how to handle unexpected types in the transforms array (though the type definition should help prevent this). For this challenge, you can assume the array contains only valid functions.

Examples

Example 1:

Input (props):
transforms: [(value) => value * 2, (value) => value + 5]
initialValue: 10

Output:
<p>25</p>

Explanation:

  1. initialValue is 10.
  2. First transform: 10 * 2 = 20.
  3. Second transform: 20 + 5 = 25.
  4. Final output is 25.

Example 2:

Input (props):
transforms: [(value) => value.toUpperCase(), (value) => `Hello, ${value}!`]
initialValue: "world"

Output:
<p>Hello, WORLD!</p>

Explanation:

  1. initialValue is "world".
  2. First transform: "world".toUpperCase() = "WORLD".
  3. Second transform: "Hello, WORLD!".
  4. Final output is "Hello, WORLD!".

Example 3: Empty transforms

Input (props):
transforms: []
initialValue: "No transformations"

Output:
<p>No transformations</p>

Explanation: Since the transforms array is empty, the initialValue is displayed directly.

Constraints

  • The transforms array will contain functions that accept one argument and return one value.
  • The initialValue can be of any primitive type or object.
  • The component should be built using Vue 3 with TypeScript and the Composition API.
  • The output should be rendered within a single HTML element.

Notes

  • Consider using Vue's computed property to efficiently manage the transformed value, ensuring it updates only when necessary.
  • Think about how to handle asynchronous transformations if you wanted to extend this concept, though for this challenge, assume all transformations are synchronous.
  • The type (value: any) => any is intentionally broad. In a real-world application, you might want to use generics for more type safety.
Loading editor...
typescript