I am writing a language parser in javascript and went with the functional approach of constructing nested functions to increase scalability.
My code currently looks like the following
const parseDate = (query) => {
return query.match(/stuff/)
}
const parseAuthor = (query) => {
return query.match(/stuff/)
}
const parseTags = (query) => {
return query.match(/stuff/)
}
//...
const transform = (match) => {
const target = match?.[0]?.trim()
// some more post processing that modifies "target"
return target;
}
const parsers = [parseAuthor, parseTags, parseReviewer, parseSearch, parseAfter].map(parser => {
return (query) => transform(parser(query))
})
I am trying to creplace function parseDate in the array with another function that will take the output of parseDate and turn it into ISO format.
parsers[4] = (query) => {
return new Date(parsers[4](query)).toISOString();
};
This causes a RangeError: Maximum call stack size exceeded which I assume is coming from the fact that javascript is constructing a recursive function that takes the output of the old parseDate function and runs it again through the new function, and that output to the same function ... and so on.
That is not what I want. I just want to replace the parsers[4] function with a new one.
I tried duplicating the function but had no luck and getting the same error
parsers[4] = (query) => {
return new Date(parsers[4].bind({})(query)).toISOString();
};
How do we exactly do this?
parseXXXfunctions are missing areturn.querytotransform(parser(query)), but if the parsers are defined as the OP has them, then this will always betransform(undefined).