Skip to content

Instantly share code, notes, and snippets.

@Zmetser
Created February 28, 2014 11:13
Show Gist options
  • Save Zmetser/9269328 to your computer and use it in GitHub Desktop.
Save Zmetser/9269328 to your computer and use it in GitHub Desktop.
function normalizeUrl(url) {
var protocol;
url = encodeURI(url);
protocol = url.match(/^https?/);
url = url.replace(/\/$/, '');
url = url.replace(/^https?:\/\//, '');
url = url.replace(/^www./, '');
url = (protocol ? protocol[0] : 'http') + '://www.' + url;
return url;
}
var http = [
'http://vimeo.com/65980230',
'http://www.vimeo.com/65980230',
'http://vimeo.com/65980230/',
'http://www.vimeo.com/65980230/',
'www.vimeo.com/65980230',
'www.vimeo.com/65980230/'
];
var https = [
'https://vimeo.com/65980230',
'https://www.vimeo.com/65980230',
'https://vimeo.com/65980230/',
'https://www.vimeo.com/65980230/'
]
http.forEach(function (url) {
console.assert(normalizeUrl(url) === 'http://www.vimeo.com/65980230', 'Failed with', url);
});
https.forEach(function (url) {
console.assert(normalizeUrl(url) === 'https://www.vimeo.com/65980230', 'Failed with', url);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment