1 | var isFunction = require('x-common-utils/isFunction');
|
2 | var isAbsoluteURL = require('x-common-utils/isAbsoluteURL');
|
3 | var isPlainObject = require('x-common-utils/isPlainObject');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function buildURL(options) {
|
16 | var url = options.url;
|
17 | var baseURL = options.baseURL;
|
18 | var model = options.model;
|
19 | var query = options.query;
|
20 | var compileURL = options.compileURL;
|
21 | var encodeQueryString = options.encodeQueryString;
|
22 | var array;
|
23 |
|
24 | if (url === null || url === undefined) {
|
25 | url = '';
|
26 | }
|
27 |
|
28 |
|
29 | url = '' + url;
|
30 |
|
31 |
|
32 |
|
33 | if (!isAbsoluteURL(url)) {
|
34 | if (baseURL === null || baseURL === undefined) {
|
35 | baseURL = '';
|
36 | }
|
37 | url = baseURL + url;
|
38 | }
|
39 |
|
40 |
|
41 | if (isPlainObject(model) && isFunction(compileURL)) {
|
42 | url = compileURL(url, model, options);
|
43 | }
|
44 |
|
45 |
|
46 | if (isPlainObject(query) && isFunction(encodeQueryString)) {
|
47 | query = encodeQueryString(query, options);
|
48 | array = url.split('#');
|
49 | url = array[0];
|
50 |
|
51 | if (url.indexOf('?') > -1) {
|
52 | if (url.charAt(url.length - 1) === '&') {
|
53 | url = url + query;
|
54 | } else {
|
55 | url = url + '&' + query;
|
56 | }
|
57 | } else {
|
58 | url = url + '?' + query;
|
59 | }
|
60 |
|
61 | array[0] = url;
|
62 | url = array.join('#');
|
63 | }
|
64 |
|
65 | return url;
|
66 | }
|
67 |
|
68 | module.exports = buildURL;
|