|
1 |
| -(function (name, context, definition) { |
2 |
| - if (typeof module !== 'undefined' && module.exports) module.exports = definition(); |
3 |
| - else if (typeof define === 'function' && define.amd) define(definition); |
4 |
| - else context[name] = definition(); |
5 |
| -})('urljoin', this, function () { |
6 |
| - |
7 |
| - function normalize (strArray) { |
8 |
| - var resultArray = []; |
9 |
| - if (strArray.length === 0) { return ''; } |
10 |
| - |
11 |
| - if (typeof strArray[0] !== 'string') { |
12 |
| - throw new TypeError('Url must be a string. Received ' + strArray[0]); |
13 |
| - } |
14 |
| - |
15 |
| - // If the first part is a plain protocol, we combine it with the next part. |
16 |
| - if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) { |
17 |
| - var first = strArray.shift(); |
18 |
| - strArray[0] = first + strArray[0]; |
19 |
| - } |
| 1 | +function normalize (strArray) { |
| 2 | + var resultArray = []; |
| 3 | + if (strArray.length === 0) { return ''; } |
20 | 4 |
|
21 |
| - // There must be two or three slashes in the file protocol, two slashes in anything else. |
22 |
| - if (strArray[0].match(/^file:\/\/\//)) { |
23 |
| - strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///'); |
24 |
| - } else { |
25 |
| - strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://'); |
26 |
| - } |
| 5 | + if (typeof strArray[0] !== 'string') { |
| 6 | + throw new TypeError('Url must be a string. Received ' + strArray[0]); |
| 7 | + } |
27 | 8 |
|
28 |
| - for (var i = 0; i < strArray.length; i++) { |
29 |
| - var component = strArray[i]; |
| 9 | + // If the first part is a plain protocol, we combine it with the next part. |
| 10 | + if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) { |
| 11 | + var first = strArray.shift(); |
| 12 | + strArray[0] = first + strArray[0]; |
| 13 | + } |
30 | 14 |
|
31 |
| - if (typeof component !== 'string') { |
32 |
| - throw new TypeError('Url must be a string. Received ' + component); |
33 |
| - } |
| 15 | + // There must be two or three slashes in the file protocol, two slashes in anything else. |
| 16 | + if (strArray[0].match(/^file:\/\/\//)) { |
| 17 | + strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///'); |
| 18 | + } else { |
| 19 | + strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://'); |
| 20 | + } |
34 | 21 |
|
35 |
| - if (component === '') { continue; } |
| 22 | + for (var i = 0; i < strArray.length; i++) { |
| 23 | + var component = strArray[i]; |
36 | 24 |
|
37 |
| - if (i > 0) { |
38 |
| - // Removing the starting slashes for each component but the first. |
39 |
| - component = component.replace(/^[\/]+/, ''); |
40 |
| - } |
41 |
| - if (i < strArray.length - 1) { |
42 |
| - // Removing the ending slashes for each component but the last. |
43 |
| - component = component.replace(/[\/]+$/, ''); |
44 |
| - } else { |
45 |
| - // For the last component we will combine multiple slashes to a single one. |
46 |
| - component = component.replace(/[\/]+$/, '/'); |
47 |
| - } |
| 25 | + if (typeof component !== 'string') { |
| 26 | + throw new TypeError('Url must be a string. Received ' + component); |
| 27 | + } |
48 | 28 |
|
49 |
| - resultArray.push(component); |
| 29 | + if (component === '') { continue; } |
50 | 30 |
|
| 31 | + if (i > 0) { |
| 32 | + // Removing the starting slashes for each component but the first. |
| 33 | + component = component.replace(/^[\/]+/, ''); |
| 34 | + } |
| 35 | + if (i < strArray.length - 1) { |
| 36 | + // Removing the ending slashes for each component but the last. |
| 37 | + component = component.replace(/[\/]+$/, ''); |
| 38 | + } else { |
| 39 | + // For the last component we will combine multiple slashes to a single one. |
| 40 | + component = component.replace(/[\/]+$/, '/'); |
51 | 41 | }
|
52 | 42 |
|
53 |
| - var str = resultArray.join('/'); |
54 |
| - // Each input component is now separated by a single slash except the possible first plain protocol part. |
| 43 | + resultArray.push(component); |
55 | 44 |
|
56 |
| - // remove trailing slash before parameters or hash |
57 |
| - str = str.replace(/\/(\?|&|#[^!])/g, '$1'); |
| 45 | + } |
58 | 46 |
|
59 |
| - // replace ? in parameters with & |
60 |
| - var parts = str.split('?'); |
61 |
| - str = parts.shift() + (parts.length > 0 ? '?': '') + parts.join('&'); |
| 47 | + var str = resultArray.join('/'); |
| 48 | + // Each input component is now separated by a single slash except the possible first plain protocol part. |
62 | 49 |
|
63 |
| - return str; |
64 |
| - } |
| 50 | + // remove trailing slash before parameters or hash |
| 51 | + str = str.replace(/\/(\?|&|#[^!])/g, '$1'); |
65 | 52 |
|
66 |
| - return function () { |
67 |
| - var input; |
| 53 | + // replace ? in parameters with & |
| 54 | + var parts = str.split('?'); |
| 55 | + str = parts.shift() + (parts.length > 0 ? '?': '') + parts.join('&'); |
68 | 56 |
|
69 |
| - if (typeof arguments[0] === 'object') { |
70 |
| - input = arguments[0]; |
71 |
| - } else { |
72 |
| - input = [].slice.call(arguments); |
73 |
| - } |
| 57 | + return str; |
| 58 | +} |
74 | 59 |
|
75 |
| - return normalize(input); |
76 |
| - }; |
| 60 | +export default function urlJoin() { |
| 61 | + var input; |
| 62 | + |
| 63 | + if (typeof arguments[0] === 'object') { |
| 64 | + input = arguments[0]; |
| 65 | + } else { |
| 66 | + input = [].slice.call(arguments); |
| 67 | + } |
77 | 68 |
|
78 |
| -}); |
| 69 | + return normalize(input); |
| 70 | +} |
0 commit comments