Parse is filtering out Dates
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')
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.
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.
Sweeeet! :) You can release it as plugin now, so we can add it to the readme :tada:
@TooTallNate, btw, it seems that it would override existing regular argument with same name?
For example
const foo = (foo, { bar, abc = 123, foo = 5 }) => {}
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 }]
Oh yea, really, mislooked the things.
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
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.
Sweet, looks even more good.
https://github.com/tunnckoCore/parse-function/pull/141 https://github.com/tunnckoCore/parse-function/pull/142
@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)