-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathurl.js
More file actions
65 lines (53 loc) · 1.63 KB
/
Copy pathurl.js
File metadata and controls
65 lines (53 loc) · 1.63 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*!
* url 相关操作
* @author ydr.me
* @create 2015-03-05 15:42
*/
define(function (require, exports, module) {
/**
* @module utils/url
* @requires utils/querystring
* @requires utils/hashbang
*/
'use strict';
var qs = require('./querystring.js');
var hb = require('./hashbang.js');
var keys = ['hash', 'host', 'hostname', 'href', 'pathname', 'port', 'protocol'];
var REG_QUERY = /\?(.*?)(#|$)/;
var link = document.createElement('a');
/**
* url 解析
* @param url {String} url 地址
* @returns {Object}
*/
exports.parse = function (url) {
link.href = url;
var ret = {};
keys.forEach(function (key) {
ret[key] = link[key];
});
ret.querystring = (ret.href.match(REG_QUERY) || ['', ''])[1];
ret.query = qs.parse(ret.querystring);
ret.search = ret.querystring ? '?' + ret.querystring : '';
ret.hashbang = hb.parse(ret.hash);
ret.base = ret.protocol + '//' + ret.host + ret.pathname;
return ret;
};
/**
* url 字符化
* @param obj {Object} url 信息
* @returns {string}
*/
exports.stringify = function (obj) {
obj.protocol = obj.protocol || 'http:';
obj.host = obj.host || '';
obj.pathname = obj.pathname || '';
obj.search = obj.search || '';
obj.hash = obj.hash || '';
if (!obj.search) {
obj.search = qs.stringify(obj.query);
obj.search = obj.search ? '?' + obj.search : '';
}
return obj.protocol + '//' + obj.host + obj.pathname + obj.search + obj.hash;
};
});