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

Feature Request: Enhance injectParams to Support Parameter Transformation #495

Open
jon9090 opened this issue Sep 12, 2024 · 1 comment
Open

Comments

@jon9090
Copy link

jon9090 commented Sep 12, 2024

Description:

Currently, the usage of injectParams is as follows:

id = injectParams('id'); // Signal<unknown>

This results in a signal of type unknown, requiring manual parsing and transformation in subsequent steps. I propose adding an option to include a transformation function as part of the injectParams method, enabling direct casting or processing of the parameter.

Desired Functionality:

I would like to use injectParams in a way that allows for the automatic transformation of the parameters, for example:

id = injectParams('id', { transform: toNumber() }); // Signal<number>

This feature would allow developers to specify a transformation function (such as toNumber(), toBoolean(), or a custom function), which would be applied to the value of the parameter before it is returned as a signal.

Benefits:

  1. Cleaner Code: Reduces the need for repetitive transformation logic in the component, resulting in more concise and readable code.
  2. Type Safety: By applying a transformation function, the type of the returned signal can be strictly typed (e.g., Signal<number>), reducing type uncertainty and errors during development.
  3. Customizability: Developers could provide custom transformation functions, offering greater flexibility when handling URL parameters.

Proposed API:

function injectParams<T>(param: string, options?: { transform?: (value: unknown) => T }): Signal<T | unknown> {
  // Implementation...
}

The options parameter would be optional, and the transform function would apply the provided transformation to the parameter value, if specified.

Example Usage:

import { toNumber } from 'somewhere';

const id = injectParams('id', { transform: toNumber }); // Signal<number>
const active = injectParams('active', { transform: (value) => value === 'true' }); // Signal<boolean>

This proposal would allow developers to effortlessly transform and type URL parameters in a declarative and reusable way.

Conclusion:

This enhancement would improve developer experience by simplifying parameter handling and ensuring better type safety, while maintaining flexibility through optional transformation functions.

@Dafnik
Copy link
Contributor

Dafnik commented Sep 13, 2024

Hello! I've already started with this :)

Life has gotten in the way of me finishing the PR, so if you'd like, feel free to take it from here. Docs and tests are still missing, but the core is mostly done.

#450

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants