-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Url.js
55 lines (51 loc) · 1.38 KB
/
Url.js
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
import 'react-native-url-polyfill/auto';
/**
* Add / to the end of any URL if not present
* @param {String} url
* @returns {String}
*/
function addTrailingForwardSlash(url) {
if (!url.endsWith('/')) {
return `${url}/`;
}
return url;
}
/**
* Get path from URL string
* @param {String} url
* @returns {String}
*/
function getPathFromURL(url) {
try {
const parsedUrl = new URL(url);
const path = parsedUrl.pathname + parsedUrl.search + parsedUrl.hash;
return path.substring(1); // Remove the leading '/'
} catch (error) {
console.error('Error parsing URL:', error);
return ''; // Return empty string for invalid URLs
}
}
/**
* Determine if two urls have the same origin
* @param {String} url1
* @param {String} url2
* @returns {Boolean}
*/
function hasSameExpensifyOrigin(url1, url2) {
const removeW3 = (host) => host.replace(/^www\./i, '');
try {
const parsedUrl1 = new URL(url1);
const parsedUrl2 = new URL(url2);
return removeW3(parsedUrl1.host) === removeW3(parsedUrl2.host);
} catch (error) {
// Handle invalid URLs or other parsing errors
console.error('Error parsing URLs:', error);
return false;
}
}
export {
// eslint-disable-next-line import/prefer-default-export
addTrailingForwardSlash,
hasSameExpensifyOrigin,
getPathFromURL,
};