1 | var staticLocation = require('./staticLocation')
|
2 |
|
3 | var unserialize = require('./unserialize')
|
4 |
|
5 | var helperGetLocatOrigin = require('./helperGetLocatOrigin')
|
6 |
|
7 | function parseURLQuery (uri) {
|
8 | return unserialize(uri.split('?')[1] || '')
|
9 | }
|
10 |
|
11 | function parseUrl (url) {
|
12 | var hashs, portText, searchs, parsed
|
13 | var href = '' + url
|
14 | if (href.indexOf('//') === 0) {
|
15 | href = (staticLocation ? staticLocation.protocol : '') + href
|
16 | } else if (href.indexOf('/') === 0) {
|
17 | href = helperGetLocatOrigin() + href
|
18 | }
|
19 | searchs = href.replace(/#.*/, '').match(/(\?.*)/)
|
20 | parsed = {
|
21 | href: href,
|
22 | hash: '',
|
23 | host: '',
|
24 | hostname: '',
|
25 | protocol: '',
|
26 | port: '',
|
27 | search: searchs && searchs[1] && searchs[1].length > 1 ? searchs[1] : ''
|
28 | }
|
29 | parsed.path = href.replace(/^([a-z0-9.+-]*:)\/\//, function (text, protocol) {
|
30 | parsed.protocol = protocol
|
31 | return ''
|
32 | }).replace(/^([a-z0-9.+-]*)(:\d+)?\/?/, function (text, hostname, port) {
|
33 | portText = port || ''
|
34 | parsed.port = portText.replace(':', '')
|
35 | parsed.hostname = hostname
|
36 | parsed.host = hostname + portText
|
37 | return '/'
|
38 | }).replace(/(#.*)/, function (text, hash) {
|
39 | parsed.hash = hash.length > 1 ? hash : ''
|
40 | return ''
|
41 | })
|
42 | hashs = parsed.hash.match(/#((.*)\?|(.*))/)
|
43 | parsed.pathname = parsed.path.replace(/(\?|#.*).*/, '')
|
44 | parsed.origin = parsed.protocol + '//' + parsed.host
|
45 | parsed.hashKey = hashs ? (hashs[2] || hashs[1] || '') : ''
|
46 | parsed.hashQuery = parseURLQuery(parsed.hash)
|
47 | parsed.searchQuery = parseURLQuery(parsed.search)
|
48 | return parsed
|
49 | }
|
50 |
|
51 | module.exports = parseUrl
|