wouter icon indicating copy to clipboard operation
wouter copied to clipboard

[Enhancement] - Possible Code Reduction

Open abjfdi opened this issue 1 year ago • 3 comments

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

abjfdi avatar Jul 21 '22 10:07 abjfdi

compatibility issues worth considering, can i use flatMap

cbbfcd avatar Jul 22 '22 11:07 cbbfcd

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.

abjfdi avatar Aug 08 '22 09:08 abjfdi

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.

abjfdi avatar Aug 09 '22 10:08 abjfdi