Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register pure/simple functions as Pipes #24559

Closed
luchsamapparat opened this issue Jun 18, 2018 · 10 comments
Closed

Register pure/simple functions as Pipes #24559

luchsamapparat opened this issue Jun 18, 2018 · 10 comments
Labels
area: core Issues related to the framework runtime core: NgModule core: pipes feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors feature: votes required Feature request which is currently still in the voting phase feature Issue that requests a new feature P4 A relatively minor issue that is not relevant to core functions
Milestone

Comments

@luchsamapparat
Copy link

luchsamapparat commented Jun 18, 2018

I'm submitting a...


[X] Feature request

Current behavior

I have various simple/pure functions (e.g. predicates, selectors, etc.) which are used throughout my application. When I want to use these functions in my components, I have to either (a) create a component method or (b) create a pipe which delegates to my function. This creates unnecessary boilerplate code.

I tried to create a factory method for creating pipes from functions, however that does not work with AOT.

export const ShoppingCartIsEmptyPipe = createPipe(shoppingCartIsEmpty);

https://gist.github.com/luchsamapparat/d535749f37def17ffdd312665a475cce#file-pipe-factory-ts

Expected behavior

I want to register a method as pipe without the need to define a separate class.

@Pipe({ name: shoppingCartIsEmpty })
export function shoppingCartIsEmpty(shoppingCart: ShoppingCart) {
    if (isNull(shoppingCart)) {
        return true;
    }

    return isEmpty(shoppingCart.items);
}

@NgModule({
    declarations: [
        shoppingCartIsEmpty
    ]
}
export class ShoppingCartModule {}

Minimal reproduction of the problem with instructions

Function:
https://github.com/luchsamapparat/cfha-client/blob/f0405838a32318d2b975019b1d63ae64419d2380/libs/shopping-cart-common/src/lib/shopping-cart.util.ts

Boilerplate Pipe to use Function in Template:
https://github.com/luchsamapparat/cfha-client/blob/f0405838a32318d2b975019b1d63ae64419d2380/libs/shopping-cart/src/lib/shopping-cart/shopping-cart-is-empty.pipe.ts

What is the motivation / use case for changing the behavior?

Easily define re-usable business behavior in simple/pure functions without any dependencies to angular. When there's the need to use that function, I can just register it as pipe.

@jasonaden jasonaden added the area: core Issues related to the framework runtime label Jun 21, 2018
@ngbot ngbot bot added this to the needsTriage milestone Jun 21, 2018
@babeal
Copy link

babeal commented Jun 27, 2018

or maybe a way to indicate in the component class that the function is pure.

@Pure()
public computeSum(a, b) {... }

@trotyl
Copy link
Contributor

trotyl commented Jun 28, 2018

Also please note that there's no such function decorator syntax.

@sandangel
Copy link

sandangel commented Jun 28, 2018

@trotyl maybe there will be in the future ^^ https://github.com/Microsoft/TypeScript/wiki/Roadmap

@trotyl
Copy link
Contributor

trotyl commented Jun 28, 2018

@sandangel There can only be decorator for Function Expression, but not possible for Function Declaration, which has already been discussed many time:

@mhevery mhevery added fixed by Ivy feature Issue that requests a new feature labels Jun 28, 2018
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Jun 28, 2018
@trotyl
Copy link
Contributor

trotyl commented Jun 29, 2018

Seems related to #9761

@babeal
Copy link

babeal commented Jun 29, 2018

I should have made mine more clear. I assumed the context the component class (a method of) which decorators can live not a random exported function. It reduces the utility but might still be nice. @trotyl

@jelbourn jelbourn added P4 A relatively minor issue that is not relevant to core functions and removed severity2: inconvenient labels Oct 1, 2020
@angular-robot angular-robot bot added the feature: votes required Feature request which is currently still in the voting phase label Jun 4, 2021
@angular-robot
Copy link
Contributor

angular-robot bot commented Jun 4, 2021

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

@angular-robot
Copy link
Contributor

angular-robot bot commented Jun 24, 2021

Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

@angular-robot angular-robot bot added the feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors label Jun 24, 2021
@alxhub
Copy link
Member

alxhub commented Jan 21, 2022

Closing as this is not really possible at this time, because of the lack of top-level function declaration decorators. With Angular's upcoming "standalone" feature, standalone pipes imported into standalone components will significantly reduce the boilerplate required, and come pretty close to a lightweight pipe API.

@alxhub alxhub closed this as completed Jan 21, 2022
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: core Issues related to the framework runtime core: NgModule core: pipes feature: insufficient votes Label to add when the not a sufficient number of votes or comments from unique authors feature: votes required Feature request which is currently still in the voting phase feature Issue that requests a new feature P4 A relatively minor issue that is not relevant to core functions
Projects
None yet
Development

No branches or pull requests

9 participants