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

Destructured Function Parameter Injection #14

Open
basejump opened this issue May 28, 2022 · 2 comments
Open

Destructured Function Parameter Injection #14

basejump opened this issue May 28, 2022 · 2 comments

Comments

@basejump
Copy link

basejump commented May 28, 2022

Would be great to support unpacking or destructured objects for function parameters.
Working on a PR here with update to the readme.

// destructured object parameter wont require any annotations. 
function createEngine({power}) { ... }

//then in module config can take the simple route as well since function params are parsed and $inject is automatically added
const carModule = {
  engine: ['factory', createEngine ],
  power:  ['value', { horses: 400 }]
};

Let me know what you think. Would this be something your interested in merging in. Plan on finishing tests today and will convert PR off draft once done.
Great project, thanks.

@basejump
Copy link
Author

PR is updated. Will push more if we discover more issues in out testing. my REGEX kungfu is not so great. I am certain there is a better way to parse the parameters to slim is down a bit than what I did, but its working.

It will add about only about 0.4k to the minified size, and a negligible increase to the gzip size, to enable a very powerful and flexible design pattern. albiet: the RORO design pattern is great but itself adds some weight if size is crucial since the minification process does not mess with the object property names. so the functions parameter destructuring gets kept. Makes it great for injection with DIDI though.

@nikku
Copy link
Owner

nikku commented Jun 6, 2022

Looks like something that could be worth supporting, especially as it would make generating typings very powerful (TS setup that is):

import { InjectionContext } from '../types';

function Car({ power } : InjectionContext) {
  ...
}

However, whatever we support should work without major caveats, and shall be properly tested. I'd be afraid that it would take quite some regex-ninja to get instantiation right (and I don't want to add a JS ast-parser to this library).

A couple of examples:

// inject `power as foobar`
function Car({ 
  power: foobar
}) { 
  // access `power` as `foobar`
}

// default value for power
function Car({ 
  power = 1000
}) { 
  // if power is not provided, default to 1000
}

// interaction with existing patterns
function Car({ 
  power = 1000
}) { 
  // if power is not provided, default to 1000
}

Car.$inject = [ 'config.car' ];

@nikku nikku added the backlog label Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants