I agree, it might be better to restructure the code to not require this functionality at all but it does make the function do what it is supposed to do:
/**
Returns the current `location.pathname` without rootURL
*/
Instead of just replacing every occurrence of Discourse.BaseUri
with an empty string my version only drops the baseUri when it exists on the start of the url. The only case that this will break is when you decide to use any of the starts of system paths (/u, /t, /admin, etc) as your BaseUri. This will break your site, but not more then before.