-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
34 lines (31 loc) · 1.05 KB
/
index.js
File metadata and controls
34 lines (31 loc) · 1.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
'use strict';
/**
* Convert a link (HTMLAnchorElement or URL) to an abbreviated Location object.
*
* @param {HTMLAnchorElement | string} input - Either an anchor node or a URL.
* @return {Object} - An abbreviated Location object.
*/
module.exports = function (input) {
var link = input;
// Take advantage of the browser's built-in URL parsing by creating
// an anchor and then reading its properties.
if (typeof input === 'string') {
// Remove credentials, which cause problems in IE11.
// http://bit.ly/2rvtfN4
input = input.replace(/^\S+?@/, '');
link = document.createElement('a');
link.href = input;
// IE doesn't populate all link properties when setting href with a
// relative URL. However, href will return an absolute URL which then can
// be used on itself to populate these additional fields.
// https://stackoverflow.com/a/13405933/2284669
if (location.host === '') {
location.href = location.href;
}
}
return {
pathname: link.pathname,
hash: link.hash,
search: link.search
};
};