// TODO memoize
module.exports = (from, to) => {
- if (to === '#') {
- return to
- } else if (to.charAt(to.length - 1) === '/') {
- return from === to ? './' : path.relative(path.dirname(from + '.'), to) + '/'
+ if (to.charAt() === '#') return to
+ let hash = ''
+ const hashIdx = to.indexOf('#')
+ if (~hashIdx) {
+ hash = to.substr(hashIdx)
+ to = to.substr(0, hashIdx)
+ }
+ if (from === to) {
+ return hash || (isDir(to) ? './' : path.basename(to))
} else {
- return path.relative(path.dirname(from + '.'), to)
+ return path.relative(path.dirname(from + '.'), to) + (isDir(to) ? '/' : '') + hash
}
}
+
+function isDir (str) {
+ return str.charAt(str.length - 1) === '/'
+}