-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (65 loc) · 2.05 KB
/
Copy pathindex.js
File metadata and controls
67 lines (65 loc) · 2.05 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
66
import $ from 'jquery'
import _ from 'lodash'
const tools = {
isNotEmptyString (obj) {
return typeof (obj) === "string" && obj !== "";
},
parseString(obj, defaultValue) {
if (this.isNullOrEmpty(obj)) {
return defaultValue;
}
return obj.toString();
},
isNullOrEmpty(obj) {
return this.isNull(obj) || obj === "";
},
isNull(obj) {
return typeof (obj) === "undefined" || obj == null;
},
urlParamToJson(urlParam) {
let item, index, json = {};
const array = this.parseString(urlParam, "").split("&");
for (let i = 0; i < array.length; i++) {
if (this.isNotEmptyString(array[i])) {
index = array[i].indexOf("=");
item = [array[i].substring(0, index), array[i].substring(index + 1)];
if (item.length === 1 && this.isNotEmptyString(item[0])) {
json[item[0]] = "";
} else if (item.length > 1 && this.isNotEmptyString(item[0])) {
json[item[0]] = item[1];
}
}
}
return json;
},
}
export default function splitUrl(url){
const result = {
url: "",
params: {}
};
if (tools.isNotEmptyString(url)) {
url = _.trim(url)
let hash = '';
if (url.indexOf("#") > -1) {
hash = url.substring(url.indexOf("#") + 1);
url = url.substring(0, url.indexOf("#"));
}
let index = url.indexOf("?");
if (index < 0) {
result["url"] = url;
} else {
result["url"] = tools.parseString(url.substring(0, index), "");
result["urlParams"] = tools.urlParamToJson(url.substring(index + 1))
}
index = hash.indexOf("?");
if (index < 0) {
result["hash"] = hash;
} else {
result["hash"] = tools.parseString(hash.substring(0, index), "");
result["hashParams"] = tools.urlParamToJson(hash.substring(index + 1))
}
result["params"] = $.extend({}, result["urlParams"], result["hashParams"]);
}
return result;
}