Last active
February 25, 2019 12:21
-
-
Save PinkiNice/9c9a4946e7387ae2f925e1c42f6f72b1 to your computer and use it in GitHub Desktop.
Function to manually, programmatically compare active route to some other
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
const START = '/'; | |
const trailingSlashRE = /\/?$/; | |
function isObjectEqual (a, b) { | |
if ( a === void 0 ) a = {}; | |
if ( b === void 0 ) b = {}; | |
// handle null value #1566 | |
if (!a || !b) { return a === b } | |
var aKeys = Object.keys(a); | |
var bKeys = Object.keys(b); | |
if (aKeys.length !== bKeys.length) { | |
return false | |
} | |
return aKeys.every(function (key) { | |
var aVal = a[key]; | |
var bVal = b[key]; | |
// check nested equality | |
if (typeof aVal === 'object' && typeof bVal === 'object') { | |
return isObjectEqual(aVal, bVal) | |
} | |
return String(aVal) === String(bVal) | |
}) | |
} | |
function isSameRoute (a, b) { | |
if (b === START) { | |
return a === b | |
} else if (!b) { | |
return false | |
} else if (a.path && b.path) { | |
return ( | |
a.path.replace(trailingSlashRE, '') === b.path.replace(trailingSlashRE, '') && | |
a.hash === b.hash && | |
isObjectEqual(a.query, b.query) | |
) | |
} else if (a.name && b.name) { | |
return ( | |
a.name === b.name && | |
a.hash === b.hash && | |
isObjectEqual(a.query, b.query) && | |
isObjectEqual(a.params, b.params) | |
) | |
} else { | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment