-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move getStablePath function into the wordrpess/url package (#35992)
* Refactor preloading.js to use the normalizePath function from the wordpress/url package. * Refactor index.js to use the normalizePath function from the wordpress/url package. * Move the normalizePath uinit test from wordpress/api-fetch to the wordpress/url package (because we've moved normalizePath to the wordpress/url package). * Update changelogs.
- Loading branch information
1 parent
955c487
commit d391388
Showing
10 changed files
with
100 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( '&' ) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters