-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresolveUrl.js
29 lines (25 loc) · 1.13 KB
/
resolveUrl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// from https://gist.github.com/Yaffle/1088850
var parseUri = require('./parseUri')
module.exports = function (base, href) { // RFC 3986
function removeDotSegments (input) {
var output = []
input.replace(/^(\.\.?(\/|$))+/, '')
.replace(/\/(\.(\/|$))+/g, '/')
.replace(/\/\.\.$/, '/../')
.replace(/\/?[^/]*/g, function (p) {
if (p === '/..') {
output.pop()
} else {
output.push(p)
}
})
return output.join('').replace(/^\//, input.charAt(0) === '/' ? '/' : '')
}
href = parseUri(href || '')
base = parseUri(base || '')
return !href || !base ? null : (href.protocol || base.protocol) +
(href.protocol || href.authority ? href.authority : base.authority) +
removeDotSegments(href.protocol || href.authority || href.pathname.charAt(0) === '/' ? href.pathname : (href.pathname ? ((base.authority && !base.pathname ? '/' : '') + base.pathname.slice(0, base.pathname.lastIndexOf('/') + 1) + href.pathname) : base.pathname)) +
(href.protocol || href.authority || href.pathname ? href.search : (href.search || base.search)) +
href.hash
}