Hone logo
Hone
Problems

Reactive Data Transformation and Update with Computed Properties and Setters in Vue.js (TypeScript)

This challenge focuses on leveraging Vue.js computed properties with setters to create reactive data transformations and trigger side effects when the transformed value changes. Understanding computed properties with setters is crucial for managing complex data dependencies and ensuring your Vue components react appropriately to changes in underlying data. This exercise will solidify your understanding of Vue's reactivity system.

Problem Description

You are tasked with creating a Vue component that manages a rawPrice property and a computed property called formattedPrice. The formattedPrice should format the rawPrice as a currency string with two decimal places (e.g., 1234.567 becomes "$1,234.57"). Crucially, when the formattedPrice is set (not just the rawPrice), you need to log a message to the console indicating that the formatted price was manually changed. This demonstrates the power of computed properties with setters for both deriving values and reacting to changes in those derived values.

Key Requirements:

  • Create a Vue component with a rawPrice data property (initialized to 0).
  • Create a computed property formattedPrice that:
    • Formats rawPrice as a currency string with two decimal places, including a dollar sign and comma separators.
    • Includes a setter that logs a message to the console when the formattedPrice is directly modified. The message should be "Formatted price changed manually!".
  • Provide a method updateRawPrice that updates the rawPrice data property. This method should not trigger the console log.
  • The component should display both rawPrice and formattedPrice in the template.

Expected Behavior:

  • When rawPrice is updated using updateRawPrice, formattedPrice should update accordingly, but the console log should not be triggered.
  • When formattedPrice is directly modified (e.g., in the console), the console log "Formatted price changed manually!" should be triggered.
  • The component should render the rawPrice and formattedPrice correctly.

Edge Cases to Consider:

  • rawPrice being a non-numeric value (handle gracefully, perhaps by displaying "N/A" or a default value).
  • rawPrice being negative.
  • rawPrice being zero.

Examples

Example 1:

Input: rawPrice = 1234.567, then updateRawPrice(1500.00)
Output: rawPrice displays 1500.00, formattedPrice displays $1,500.00, no console log.
Explanation: Updating rawPrice triggers the computed property, but not the setter.

Example 2:

Input: rawPrice = 100.00, then manually set this.formattedPrice = "$200.00" in the console.
Output: rawPrice displays 100.00, formattedPrice displays $200.00, console log displays "Formatted price changed manually!".
Explanation: Directly setting formattedPrice triggers the setter.

Example 3:

Input: rawPrice = -50.25
Output: rawPrice displays -50.25, formattedPrice displays -$50.25
Explanation: Handles negative numbers correctly.

Constraints

  • The component must be written in TypeScript.
  • The formatting logic must use JavaScript's built-in number formatting capabilities (e.g., toFixed(), toLocaleString()). Avoid external libraries for formatting.
  • The component should be relatively simple and focused on demonstrating the computed property with setter functionality.
  • The component should be functional and not rely on any external state management libraries.

Notes

  • Remember that computed properties are cached based on their dependencies. Only re-evaluate when the dependencies change.
  • The setter is only invoked when the computed property is set, not when its dependencies change.
  • Consider using template literals for cleaner string concatenation.
  • Think about how to handle potential errors or invalid input for rawPrice. A simple "N/A" display is sufficient for this challenge.
Loading editor...
typescript