Skip to content

Instantly share code, notes, and snippets.

@lucymtc
Created July 30, 2018 21:23
Show Gist options
  • Select an option

  • Save lucymtc/6d52aa81cdedf255a11bed287f503f6e to your computer and use it in GitHub Desktop.

Select an option

Save lucymtc/6d52aa81cdedf255a11bed287f503f6e to your computer and use it in GitHub Desktop.
JavaScript, URL normalization, make sure HTML entities are escaped.
/**
* 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