Skip to content

Instantly share code, notes, and snippets.

@endaaman
Last active August 29, 2015 14:21
Show Gist options
  • Save endaaman/9b8dabb88e7e13856728 to your computer and use it in GitHub Desktop.
Save endaaman/9b8dabb88e7e13856728 to your computer and use it in GitHub Desktop.
var url = require('url');
var querystring = require('querystring');
prettifyUrl = function (uglyUrl) {
var oldUrlObj = url.parse(uglyUrl);
var queryObj = querystring.parse(oldUrlObj.query);
if ('_escaped_fragment_' in queryObj) {
var escapedFragment = queryObj._escaped_fragment_;
delete queryObj['_escaped_fragment_'];
} else {
// url is not UGLYYYY
return uglyUrl;
}
var newUrlObj = {
protocol: oldUrlObj.protocol,
slash: oldUrlObj.slash,
host: oldUrlObj.host,
port: oldUrlObj.port,
hostname: oldUrlObj.hostname,
hash: '#!' + escapedFragment,
query: queryObj,
pathname: oldUrlObj.pathname
}
return url.format(newUrlObj)
}
var u1_org = 'http://example.com/?query=123#!key1=value1&key2=value2';
var u1_ugl = 'http://example.com/?_escaped_fragment_=key1=value1%26key2=value2&query=123';
var u2_org = 'http://example.com/?query=123';
var u2_ugl = 'http://example.com/?_escaped_fragment_=&query=123';
console.log('[original] ', u1_org);
console.log('[uglified] ', u1_ugl);
console.log('[prettified]', prettifyUrl(u1_ugl));
console.log('');
console.log('[original] ', u2_org);
console.log('[uglified] ', u2_ugl);
console.log('[prettified]', prettifyUrl(u2_ugl));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment