summaryrefslogtreecommitdiff
path: root/javascript/videojs/src/js/utils/url.js
blob: 7ef62aaae9d2861694807cd1d3ec8de0e59d5208 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
 * @file url.js
 * @module url
 */
import document from 'global/document';
import window from 'global/window';

/**
 * Resolve and parse the elements of a URL.
 *
 * @function
 * @param    {string} url
 *           The url to parse
 *
 * @return   {URL}
 *           An object of url details
 */
export const parseUrl = function(url) {
  return new URL(url, document.baseURI);
};

/**
 * Get absolute version of relative URL.
 *
 * @function
 * @param    {string} url
 *           URL to make absolute
 *
 * @return   {string}
 *           Absolute URL
 */
export const getAbsoluteURL = function(url) {
  return (new URL(url, document.baseURI)).href;
};

/**
 * Returns the extension of the passed file name. It will return an empty string
 * if passed an invalid path.
 *
 * @function
 * @param    {string} path
 *           The fileName path like '/path/to/file.mp4'
 *
 * @return  {string}
 *           The extension in lower case or an empty string if no
 *           extension could be found.
 */
export const getFileExtension = function(path) {
  if (typeof path === 'string') {
    const cleanPath = path.split('?')[0].replace(/\/+$/, '');

    const match = cleanPath.match(/\.([^.\/]+)$/);

    return match ? match[1].toLowerCase() : '';
  }
  return '';
};

/**
 * Returns whether the url passed is a cross domain request or not.
 *
 * @function
 * @param    {string} url
 *           The url to check.
 *
 * @param    {URL} [winLoc]
 *           the domain to check the url against, defaults to window.location
 *
 * @return   {boolean}
 *           Whether it is a cross domain request or not.
 */
export const isCrossOrigin = function(url, winLoc = window.location) {
  return parseUrl(url).origin !== winLoc.origin;
};