Hone logo
Hone
Problems

Creating a Reusable Shared Module in Angular

Angular applications often contain components, pipes, and directives that are used across multiple modules. Creating a shared module allows you to encapsulate these reusable elements, promoting code organization, maintainability, and reducing redundancy. This challenge will guide you through building a shared module that contains a custom pipe and a simple component.

Problem Description

You are tasked with creating a shared module named SharedModule in an Angular application. This module should contain:

  1. A Custom Pipe: A pipe named ReversePipe that takes a string as input and returns the reversed string.
  2. A Simple Component: A component named DisplayMessageComponent that accepts an @Input() property called message and displays it within a <p> tag.

The SharedModule should be designed to be easily imported into other modules within the application without causing conflicts or unnecessary dependencies. The module should be declared as standalone to align with modern Angular practices.

Key Requirements:

  • The ReversePipe must correctly reverse any given string.
  • The DisplayMessageComponent must display the value of its message input property.
  • The SharedModule must be declared as standalone.
  • The module should export both the ReversePipe and the DisplayMessageComponent so they can be used in other modules.

Expected Behavior:

When another module imports SharedModule, it should be able to use both the ReversePipe and the DisplayMessageComponent without needing to declare them individually.

Edge Cases to Consider:

  • Empty strings passed to ReversePipe.
  • Null or undefined values passed to the message input of DisplayMessageComponent. (Handle gracefully, perhaps displaying a default message).
  • Non-string input to ReversePipe. (Consider how to handle this - either throw an error or attempt to convert to a string).

Examples

Example 1:

Input (to ReversePipe): "hello"
Output: "olleh"
Explanation: The pipe reverses the input string "hello" to "olleh".

Example 2:

Input (to DisplayMessageComponent): message = "Welcome!"
Output: <p>Welcome!</p>
Explanation: The component displays the string "Welcome!" within a paragraph tag.

Example 3: (Edge Case)

Input (to ReversePipe): ""
Output: ""
Explanation: An empty string remains an empty string after reversal.

Constraints

  • The solution must be written in TypeScript.
  • The SharedModule must be declared as standalone.
  • The code should be well-formatted and easy to understand.
  • The ReversePipe should handle non-string inputs gracefully (either by converting to a string or throwing an error - document your choice).
  • The DisplayMessageComponent should handle null/undefined input gracefully.

Notes

  • Consider using Angular's dependency injection system appropriately.
  • Think about how to structure your code for maximum reusability and maintainability.
  • Focus on creating a clean and well-defined shared module that can be easily integrated into other parts of your Angular application.
  • You don't need to create a full Angular application; just the SharedModule, ReversePipe, and DisplayMessageComponent are required. You can assume the existence of a basic Angular project structure.
Loading editor...
typescript