Hone logo
Hone
Problems

Custom Data Transformer in Angular

This challenge focuses on creating a reusable data transformer component in Angular. Data transformers are useful for consistently formatting data before displaying it in your application, ensuring a uniform look and feel and centralizing formatting logic. You'll build a component that takes raw data and applies a configurable transformation function to it.

Problem Description

You need to create an Angular component called DataTransformer that accepts raw data and a transformation function as inputs. The component should apply the transformation function to the raw data and output the transformed data. The transformation function should be provided as a string representing a TypeScript function. The component should handle potential errors during transformation gracefully.

Key Requirements:

  • Input:
    • rawData: The raw data to be transformed (any type).
    • transformFunction: A string containing the TypeScript code for the transformation function. This function should accept one argument (the raw data) and return the transformed data.
  • Output: The transformed data.
  • Error Handling: If the transformFunction is invalid or throws an error during execution, the component should output an error message instead of crashing.
  • Reusability: The component should be designed to be easily reusable with different data types and transformation functions.
  • Type Safety: Leverage TypeScript to ensure type safety where possible.

Expected Behavior:

The DataTransformer component should dynamically execute the provided transformFunction on the rawData. The component should handle invalid transformFunction strings and errors that occur during function execution. The output should be the result of the transformation, or an error message if the transformation fails.

Edge Cases to Consider:

  • Empty rawData.
  • Invalid transformFunction string (e.g., syntax errors, missing return statement).
  • transformFunction throwing an error during execution (e.g., type mismatch, undefined variable).
  • transformFunction returning a value of an unexpected type.

Examples

Example 1:

Input: rawData: { name: "John", age: 30 }, transformFunction: "data => ({ fullName: data.name, yearsOld: data.age + 10 })"
Output: { fullName: "John", yearsOld: 40 }
Explanation: The transformFunction renames the properties and adds 10 to the age.

Example 2:

Input: rawData: "123", transformFunction: "data => parseInt(data, 10)"
Output: 123
Explanation: The transformFunction parses the string into an integer.

Example 3: (Edge Case)

Input: rawData: { name: "Jane" }, transformFunction: "data => data.nonExistentProperty"
Output: "Error: Cannot read property 'nonExistentProperty' of { name: 'Jane' }"
Explanation: The transformFunction attempts to access a non-existent property, resulting in an error. The component should catch this and output an error message.

Constraints

  • The transformFunction string will be provided as a valid TypeScript function string. However, the component must handle invalid or erroneous functions gracefully.
  • The component should be able to handle various data types for rawData (string, number, object, array, etc.).
  • The component should not introduce any significant performance bottlenecks. Simple function execution is acceptable.
  • The transformFunction should be limited to a reasonable length (e.g., 200 characters) to prevent excessively long strings from being passed. While not strictly enforced, consider this for practical use.

Notes

  • Consider using eval() or new Function() to dynamically execute the transformFunction. Be mindful of the security implications of using eval() and sanitize the input if necessary in a production environment (though this is less of a concern for this exercise). new Function() is generally considered safer.
  • Implement robust error handling to catch and display meaningful error messages.
  • Think about how to make the component configurable and reusable for different transformation scenarios.
  • Focus on clear and concise code.
  • You are expected to create a complete Angular component, including the template, styles, and logic.
Loading editor...
typescript