-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change distributed code to use ECMAScript modules (#76)
- Loading branch information
Showing
6 changed files
with
115 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,70 @@ | ||
(function (name, context, definition) { | ||
if (typeof module !== 'undefined' && module.exports) module.exports = definition(); | ||
else if (typeof define === 'function' && define.amd) define(definition); | ||
else context[name] = definition(); | ||
})('urljoin', this, function () { | ||
|
||
function normalize (strArray) { | ||
var resultArray = []; | ||
if (strArray.length === 0) { return ''; } | ||
|
||
if (typeof strArray[0] !== 'string') { | ||
throw new TypeError('Url must be a string. Received ' + strArray[0]); | ||
} | ||
|
||
// If the first part is a plain protocol, we combine it with the next part. | ||
if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) { | ||
var first = strArray.shift(); | ||
strArray[0] = first + strArray[0]; | ||
} | ||
function normalize (strArray) { | ||
var resultArray = []; | ||
if (strArray.length === 0) { return ''; } | ||
|
||
// There must be two or three slashes in the file protocol, two slashes in anything else. | ||
if (strArray[0].match(/^file:\/\/\//)) { | ||
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///'); | ||
} else { | ||
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://'); | ||
} | ||
if (typeof strArray[0] !== 'string') { | ||
throw new TypeError('Url must be a string. Received ' + strArray[0]); | ||
} | ||
|
||
for (var i = 0; i < strArray.length; i++) { | ||
var component = strArray[i]; | ||
// If the first part is a plain protocol, we combine it with the next part. | ||
if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) { | ||
var first = strArray.shift(); | ||
strArray[0] = first + strArray[0]; | ||
} | ||
|
||
if (typeof component !== 'string') { | ||
throw new TypeError('Url must be a string. Received ' + component); | ||
} | ||
// There must be two or three slashes in the file protocol, two slashes in anything else. | ||
if (strArray[0].match(/^file:\/\/\//)) { | ||
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///'); | ||
} else { | ||
strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://'); | ||
} | ||
|
||
if (component === '') { continue; } | ||
for (var i = 0; i < strArray.length; i++) { | ||
var component = strArray[i]; | ||
|
||
if (i > 0) { | ||
// Removing the starting slashes for each component but the first. | ||
component = component.replace(/^[\/]+/, ''); | ||
} | ||
if (i < strArray.length - 1) { | ||
// Removing the ending slashes for each component but the last. | ||
component = component.replace(/[\/]+$/, ''); | ||
} else { | ||
// For the last component we will combine multiple slashes to a single one. | ||
component = component.replace(/[\/]+$/, '/'); | ||
} | ||
if (typeof component !== 'string') { | ||
throw new TypeError('Url must be a string. Received ' + component); | ||
} | ||
|
||
resultArray.push(component); | ||
if (component === '') { continue; } | ||
|
||
if (i > 0) { | ||
// Removing the starting slashes for each component but the first. | ||
component = component.replace(/^[\/]+/, ''); | ||
} | ||
if (i < strArray.length - 1) { | ||
// Removing the ending slashes for each component but the last. | ||
component = component.replace(/[\/]+$/, ''); | ||
} else { | ||
// For the last component we will combine multiple slashes to a single one. | ||
component = component.replace(/[\/]+$/, '/'); | ||
} | ||
|
||
var str = resultArray.join('/'); | ||
// Each input component is now separated by a single slash except the possible first plain protocol part. | ||
resultArray.push(component); | ||
|
||
// remove trailing slash before parameters or hash | ||
str = str.replace(/\/(\?|&|#[^!])/g, '$1'); | ||
} | ||
|
||
// replace ? in parameters with & | ||
var parts = str.split('?'); | ||
str = parts.shift() + (parts.length > 0 ? '?': '') + parts.join('&'); | ||
var str = resultArray.join('/'); | ||
// Each input component is now separated by a single slash except the possible first plain protocol part. | ||
|
||
return str; | ||
} | ||
// remove trailing slash before parameters or hash | ||
str = str.replace(/\/(\?|&|#[^!])/g, '$1'); | ||
|
||
return function () { | ||
var input; | ||
// replace ? in parameters with & | ||
var parts = str.split('?'); | ||
str = parts.shift() + (parts.length > 0 ? '?': '') + parts.join('&'); | ||
|
||
if (typeof arguments[0] === 'object') { | ||
input = arguments[0]; | ||
} else { | ||
input = [].slice.call(arguments); | ||
} | ||
return str; | ||
} | ||
|
||
return normalize(input); | ||
}; | ||
export default function urlJoin() { | ||
var input; | ||
|
||
if (typeof arguments[0] === 'object') { | ||
input = arguments[0]; | ||
} else { | ||
input = [].slice.call(arguments); | ||
} | ||
|
||
}); | ||
return normalize(input); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.