wouter
wouter copied to clipboard
[Enhancement] - Possible Code Reduction
Background
The flattenChildren method uses the reduce - map pattern on MDN Array.prototype.flat docs. MDN Array.prototype.flatMap is the same pattern but claims to be slightly faster and is less code.
https://github.com/molefrog/wouter/blob/467d8b2fb4bea48d5f6686743a785f7c87a2e5ad/index.js#L134
I took the time to change the function if your interested in this code change. Also flattenChildren was written twice, below solution writes it once.
const flattenChildren = children => {
return Array.isArray(children)
? children.flatMap(c => flattenChildren(c && c.type === Fragment ? c.props.children : c))
: [children];
};
compatibility issues worth considering, can i use flatMap
Yes that is an issue worth considering. I don't know what browsers wouter is trying to support.
can i use says that all major browsers support flatmap, but IE 11 doesn't and some non-major browsers don't.
Most webpack browserlists in my projects are set to last 2 versions, not dead and greater than 0.1% market share. I would assume most projects would have very similar browserlist and thus this wouldn't break their projects.
You can close this issue if you want.
Another issue worth considering is that the flattenChildren function is a core piece of wouter and any changes to it should come with serious considerations and regression testing.