Skip to content

Instantly share code, notes, and snippets.

@Venugopal46
Last active April 23, 2018 05:26
Show Gist options
  • Save Venugopal46/1554bee7945353c0c191d72407312199 to your computer and use it in GitHub Desktop.
Save Venugopal46/1554bee7945353c0c191d72407312199 to your computer and use it in GitHub Desktop.
const urlParser = url => {
const parser = document.createElement('a');
parser.href = url;
return parser;
};
const compareSearchParams = (p1, p2) => {
const urlParams1 = new URLSearchParams(p1);
const urlParams2 = new URLSearchParams(p2);
var keys1 = urlParams1.keys();
var keys2 = urlParams2.keys();
for(key of keys1) {
const eq = urlParams1.has(key) && urlParams2.has(key) && (urlParams1.get(key) === urlParams2.get(key));
if (!eq) {
return false;
}
}
for(key of keys2) {
const eq = urlParams1.has(key) && urlParams2.has(key) && (urlParams1.get(key) === urlParams2.get(key));
if (!eq) {
return false;
}
}
return true;
};
if (String.prototype.isSameURL === undefined) {
String.prototype.isSameURL = function (url) {
const pUrl1 = urlParser(this);
const pUrl2 = urlParser(url);
if (pUrl1.username !== pUrl2.username || pUrl1.password !== pUrl2.password) {
console.log('auth');
return false;
} else if (pUrl1.origin !== pUrl2.origin) {
console.log('origin');
return false;
} else if (pUrl1.pathname !== pUrl2.pathname) {
console.log('pathname');
return false;
} else if (!compareSearchParams(pUrl1.search, pUrl2.search)) {
console.log('query');
return false
} else if (pUrl1.hash !== pUrl2.hash) {
console.log('hash');
return false;
}
return true;
}
}
const url1 = 'http://ABC:cd@example.com/go/~down/foo.html/?a=~1&c=1&b=2#hash';
const url2 = 'http://ABC:cd@EXAMPLE.com:80/go/further/%7Edown/foo.html/?b=2&a=%7E1&c=1#hash';
const isEqual = url1.isSameURL(url2);
console.log(isEqual);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment