Created
July 30, 2018 21:23
-
-
Save lucymtc/6d52aa81cdedf255a11bed287f503f6e to your computer and use it in GitHub Desktop.
JavaScript, URL normalization, make sure HTML entities are escaped.
This file contains hidden or 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
| /** | |
| * URL normalization, make sure HTML entities are escaped. | |
| * | |
| * @param String url | |
| * | |
| * @return String | |
| */ | |
| export function normalizeURL(url) { | |
| const fixedEncodeURIComponent = str => encodeURIComponent(str).replace(/[!'()*]/g, x => `%${x.charCodeAt(0).toString(16)}`); | |
| const newURL = url | |
| .replace(/&/g, '&') | |
| .replace(/<|>|\(|\)|!|'|\*|;| |/g, x => fixedEncodeURIComponent(x)); | |
| return trim(JSON.stringify(newURL), '"'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment