How to design a pipe that accepts configuration options for flexible transformations

0 votes
Can i know How to design a pipe that accepts configuration options for flexible transformations?
6 days ago in Node-js by Nidhi
• 15,620 points
29 views

1 answer to this question.

0 votes

Angular Pipe Implementation

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({

  name: 'flexTransform'

})

export class FlexTransformPipe implements PipeTransform {

  transform(value: any, config: { [key: string]: any }): any {

    // Apply transformations based on configuration

    let result = value;

    if (config.prefix) {

      result = config.prefix + result;

    }

    if (config.suffix) {

      result = result + config.suffix;

    }

    if (config.uppercase) {

      result = result.toString().toUpperCase();

    }

    if (config.multiply && typeof result === 'number') {

      result = result * config.multiply;

    }

    return result;

  }

}

Usage Examples

<!-- Simple string transformation -->

<p>{{ 'hello' | flexTransform:{prefix: '>> ', suffix: '!', uppercase: true} }}</p>

<!-- Output: ">> HELLO!" -->

<!-- Number transformation -->

<p>{{ 5 | flexTransform:{multiply: 3, prefix: 'Result: '} }}</p>

<!-- Output: "Result: 15" -->

<!-- Chaining with other pipes -->

<p>{{ 2.5 | currency | flexTransform:{prefix: 'Price: '} }}</p>

<!-- Output: "Price: $2.50" -->

answered 3 days ago by anonymous

Related Questions In Node-js

0 votes
1 answer

How to create a pipe that converts RGB color codes to hexadecimal format?

To create an Angular pipe that converts ...READ MORE

answered 3 days ago in Node-js by anonymous
36 views
0 votes
1 answer

How to implement a pipe that calculates the age from a birthdate input?

To implement a custom pipe that calculates ...READ MORE

answered 3 days ago in Node-js by anonymous
34 views
0 votes
1 answer

How do I add a custom script to my package.json file that runs a javascript file?

run npm run script1 it works for me READ MORE

answered Jan 10, 2023 in Node-js by Harisudarsan

edited Mar 5 42,076 views
0 votes
1 answer

How to Handle Jest Unit Testing for 'ɵcmp' in a React-in-Angular Hybrid App?

Encountering the 'ɵcmp' property error during Jest ...READ MORE

answered Dec 23, 2024 in Node-js by Navya
132 views
0 votes
1 answer

How to create a service that manages user sessions and authentication tokens?

1. Create the Auth Service (auth.service.ts) import { ...READ MORE

answered 3 days ago in Node-js by anonymous
35 views
0 votes
1 answer

How to develop a service that handles centralized event broadcasting across components?

To develop a centralized event broadcasting service ...READ MORE

answered 3 days ago in Node-js by anonymous
45 views
0 votes
1 answer

How do services help in implementing the Facade Pattern in Angular applications?

In Angular, services help implement the Facade ...READ MORE

answered 3 days ago in Node-js by anonymous
44 views
0 votes
1 answer

How to use services to abstract API endpoints and data models from components?

To abstract API endpoints and data models ...READ MORE

answered 3 days ago in Node-js by anonymous
51 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP