shape-json icon indicating copy to clipboard operation
shape-json copied to clipboard

Parse is filtering out Dates

Open wilson-salonkee opened this issue 1 year ago • 0 comments

Hello, I'm having an issue with the parse function where it filters out fields that are of type date.

Example: My Shape:

 const shape =    "{
  $group[requests](migrationRequestId)": {
      migrationRequestId: "migrationRequestId",
      status: "migrationRequestStatus",
      migrationDate: "migrationDate",
    },
}

Original JSON:

 {
  migrationRequestStatus: 'FAILED',
  migrationRequestId: 26,
  migrationDate: 2024-02-12T12:57:28.000Z,
}

After Parse:

{
  requests: [ { migrationRequestId: 26, status: 'FAILED' } ]
}

However if the Date is a string in the original JSON it works fine (eg: '2024-02-12')

wilson-salonkee avatar Mar 01 '24 13:03 wilson-salonkee

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.

tunnckoCore avatar Sep 01 '17 21:09 tunnckoCore

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.

TooTallNate avatar Sep 20 '17 19:09 TooTallNate

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

tunnckoCore avatar Sep 20 '17 20:09 tunnckoCore

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

For example

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

tunnckoCore avatar Mar 02 '18 15:03 tunnckoCore

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 }]

TooTallNate avatar Mar 03 '18 23:03 TooTallNate

Oh yea, really, mislooked the things.

tunnckoCore avatar Mar 03 '18 23:03 tunnckoCore

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 avatar May 15 '18 02:05 tunnckoCore

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.

bluelovers avatar Jun 04 '18 08:06 bluelovers

Sweet, looks even more good.

tunnckoCore avatar Jun 04 '18 08:06 tunnckoCore

https://github.com/tunnckoCore/parse-function/pull/141 https://github.com/tunnckoCore/parse-function/pull/142

bluelovers avatar Jun 04 '18 08:06 bluelovers

@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)

bluelovers avatar Jun 05 '18 03:06 bluelovers