Skip to content
This repository has been archived by the owner on Feb 8, 2020. It is now read-only.

Destructuring #47

Open
gunar opened this issue Sep 1, 2017 · 11 comments
Open

Destructuring #47

gunar opened this issue Sep 1, 2017 · 11 comments

Comments

@gunar
Copy link

gunar commented Sep 1, 2017

Yo, how hard would it be to make this package understand destructuring?

({ foo, bar = 5 }) => null

Wanted to ask before trying to do it myself.

@tunnckoCore
Copy link
Owner

tunnckoCore commented Sep 1, 2017

I think it wouldn't be easy. Or if it is easy, it is very strange case and may lead to more things. Once i asked, one should be the expected result of such arguments?

But yea, in any way, it's cool because we support plugins, so i don't think it would be needed to include it by default. If it isn't something big - yes, otherwise no.

Can't answer, didn't see or remember what the AST would be in destructuring case.

@TooTallNate
Copy link
Contributor

TooTallNate commented Sep 20, 2017

I was able to make it work the way I expected with a few lines of code:

function parseParam(param) {
  switch (param.type) {
    case 'ArrayPattern':
      return param.elements.map(parseParam);

    case 'ObjectPattern':
      return param.properties.reduce((o, property) => {
        let value;
        if (property.value.type === 'Identifier') {
          value = true;
        } else {
          value = parseParam(property.value);
        }
        const { name } = property.key;
        o[name] = value;
        return o;
      }, {});

    case 'Identifier':
      return param.name;

    default:
      throw new TypeError(`${param.type} is not a recognized "type"`);
  }
}

parse.use(app => (node, result) => {
  node.params.forEach((param, i) => {
    result.args[i] = parseParam(param);
  });
  return result;
})

Feel free to merge upstream if you would like.

@tunnckoCore
Copy link
Owner

tunnckoCore commented Sep 20, 2017

Sweeeet! :) You can release it as plugin now, so we can add it to the readme 🎉

@tunnckoCore
Copy link
Owner

@TooTallNate, btw, it seems that it would override existing regular argument with same name?

For example

const foo = (foo, { bar, abc = 123, foo = 5 }) => {}

@TooTallNate
Copy link
Contributor

TooTallNate commented Mar 3, 2018

I don't think so because each argument is it's own array entry. So in your example result.args would be ['foo', { bar: true, abc: 123, foo: 5 }]

@tunnckoCore
Copy link
Owner

Oh yea, really, mislooked the things.

@tunnckoCore
Copy link
Owner

Hey @TooTallNate, do you mind adding a PR with it? Next release (#138) can just include it. So we can add it to bullets of default supported features :P

@tunnckoCore tunnckoCore added this to the v6 milestone May 15, 2018
@bluelovers
Copy link

bluelovers commented Jun 4, 2018

https://github.com/bluelovers/node-func-args/blob/c71f7d8dc0bba5b4f36c17b3c1bb3433b1af023f/index.ts#L166

GitHub
node-func-args - ECMAScript Function Arguments parser, Get arguments of a function, useful for and used in dependency injectors. Works for regular functions, generator functions and arrow functions.

@tunnckoCore
Copy link
Owner

Sweet, looks even more good.

@bluelovers
Copy link

bluelovers commented Jun 4, 2018

#141
#142

@bluelovers
Copy link

@olstenlarck anything wanna do at pr? (but not include make test, but can see here)

because i can't run build this repo at local (also when no any my commit)

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

No branches or pull requests

4 participants