-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcan-parse-uri.js
47 lines (47 loc) · 1.35 KB
/
can-parse-uri.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use strict";
/**
* @module {function} can-parse-uri can-parse-uri
* @parent can-js-utilities
* @collection can-infrastructure
* @package ./package.json
* @signature `parseURI(url)`
*
* Parse a URI into its components.
*
* ```js
* import {parseURI} from "can"
* parseURI("http://foo:8080/bar.html?query#change")
* //-> {
* // authority: "//foo:8080",
* // hash: "#change",
* // host: "foo:8080",
* // hostname: "foo",
* // href: "http://foo:8080/bar.html?query#change",
* // pathname: "/bar.html",
* // port: "8080",
* // protocol: "http:",
* // search: "?query"
* // }
* ```
*
* @param {String} url The URL you want to parse.
*
* @return {Object} Returns an object with properties for each part of the URL. `null`
* is returned if the url can not be parsed.
*/
var namespace = require("can-namespace");
module.exports = namespace.parseURI = function(url){
var m = String(url).replace(/^\s+|\s+$/g, '').match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
// authority = '//' + user + ':' + pass '@' + hostname + ':' port
return (m ? {
href : m[0] || '',
protocol : m[1] || '',
authority: m[2] || '',
host : m[3] || '',
hostname : m[4] || '',
port : m[5] || '',
pathname : m[6] || '',
search : m[7] || '',
hash : m[8] || ''
} : null);
};