-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move getStablePath function into the wordrpess/url package #35992
Changes from all commits
9a325f8
2d7ffe5
c50fe73
c53aafc
d724344
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Given a path, returns a normalized path where equal query parameter values | ||
* will be treated as identical, regardless of order they appear in the original | ||
* text. | ||
* | ||
* @param {string} path Original path. | ||
* | ||
* @return {string} Normalized path. | ||
*/ | ||
export function normalizePath( path ) { | ||
const splitted = path.split( '?' ); | ||
const query = splitted[ 1 ]; | ||
const base = splitted[ 0 ]; | ||
if ( ! query ) { | ||
return base; | ||
} | ||
|
||
// 'b=1&c=2&a=5' | ||
return ( | ||
base + | ||
'?' + | ||
query | ||
// [ 'b=1', 'c=2', 'a=5' ] | ||
.split( '&' ) | ||
// [ [ 'b, '1' ], [ 'c', '2' ], [ 'a', '5' ] ] | ||
.map( ( entry ) => entry.split( '=' ) ) | ||
// [ [ 'a', '5' ], [ 'b, '1' ], [ 'c', '2' ] ] | ||
.sort( ( a, b ) => a[ 0 ].localeCompare( b[ 0 ] ) ) | ||
// [ 'a=5', 'b=1', 'c=2' ] | ||
.map( ( pair ) => pair.join( '=' ) ) | ||
// 'a=5&b=1&c=2' | ||
.join( '&' ) | ||
); | ||
Comment on lines
+18
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that we don't have to support IE, we can do this instead: const searchParams = new URLSearchParams( query );
searchParams.sort();
return `${base}?${ searchParams.toString() }`; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good idea. Thank you, @kevin940726 !
I've created an issue for this improvement: #36046 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to add changelog for internal function. They aren't expected to be used by users anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kevin940726
Excuse me, but I don't understand why we don't have to add such changes to the changelog.
This document states that:
Also, it says this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, I guess that we're just not strictly following this 😅 ? Anyway, this looks fine to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it looks like so. 😅
But maybe we should (I am not excluding myself from the list).
Again, thank you for the review!