Angular Impure Pipe for Dynamic Data Transformation
Imagine you're building a dashboard that displays real-time stock prices. The displayed price needs to be formatted with a currency symbol and potentially a conditional indicator (e.g., an up or down arrow). This formatting should update automatically as new data arrives. This challenge asks you to create an impure pipe that can handle such dynamic, frequently changing data.
Problem Description
Your task is to create an Angular impure pipe named StockPriceFormatterPipe. This pipe will take a numerical stock price as input and return a formatted string.
Key Requirements:
- Input: The pipe should accept a
numberrepresenting the stock price. - Output: The pipe should return a
stringformatted as follows:- It should always prepend a dollar sign (
$). - It should always display two decimal places.
- If the price is positive, it should append a green upward arrow (
↑). - If the price is negative, it should append a red downward arrow (
↓). - If the price is zero, it should append a neutral symbol (e.g.,
-).
- It should always prepend a dollar sign (
- Impure Pipe: The pipe must be an impure pipe. This means it should re-execute its
transformmethod every time Angular checks for changes in the component, even if the input value hasn't changed. This is crucial for reacting to real-time data updates. - Error Handling: If the input is not a valid number (e.g.,
null,undefined, or a non-numeric string), the pipe should return a placeholder string like "Invalid Price".
Expected Behavior:
The pipe should correctly format various numerical inputs according to the rules above. For example:
123.456should become$123.46↑-78.9should become$-78.90↓0should become$0.00-nullshould becomeInvalid Price
Edge Cases:
- Handling of
NaNshould result in "Invalid Price". - The pipe should handle both positive and negative numbers correctly, including those with varying decimal places.
Examples
Example 1:
Input: 150.759
Output: $150.76↑
Explanation: The number is positive, so it's formatted to two decimal places and has an upward arrow appended.
Example 2:
Input: -25.123
Output: $-25.12↓
Explanation: The number is negative, so it's formatted to two decimal places and has a downward arrow appended.
Example 3:
Input: 0
Output: $0.00-
Explanation: Zero is handled with a neutral symbol.
Example 4:
Input: null
Output: Invalid Price
Explanation: Non-numeric input is handled by returning an error string.
Constraints
- The pipe must be implemented in TypeScript.
- The pipe must be declared as an
impurepipe in Angular. - The formatting of currency and decimal places should adhere to standard number formatting conventions in JavaScript.
- The performance impact of an impure pipe is a consideration; however, for this challenge, correctness of transformation is prioritized.
Notes
Remember that impure pipes can impact performance if not used judiciously, as they run more frequently than pure pipes. For this exercise, focus on implementing the required transformation logic correctly. You'll need to leverage Angular's Pipe decorator and its pure: false option.
To implement the conditional arrow logic, you can use simple if/else statements or ternary operators. For number formatting, consider using toFixed() and potentially Math.sign() or direct comparison.