-
Notifications
You must be signed in to change notification settings - Fork 1
/
url.js
29 lines (23 loc) · 800 Bytes
/
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
export function processUrl(url, ignoreQueryString = true, isYt = false) {
if (isYt) {
return extractSearchableVideoId(url);
}
return ignoreQueryString ? removeQueryString(url) : url;
}
export function removeQueryString(url) {
return url.split(/[?#]/i)[0];
}
/* Youtube video handling */
export const YT_REGEX = /^https?:\/\/(?:www\.|m\.|)?youtu(?:\.be|be\.com)\/(?:embed\/|v\/|watch\?(?:.+&)*v=)?([\w-_]{11})/i;
export const DASHES_REGEX = /^-*(.*)/i;
export function isYoutubeUrl(url) {
return YT_REGEX.test(url);
}
export function extractSearchableVideoId(ytUrl) {
let videoId = getYoutubeVideoId(ytUrl);
let videoIdWithoutLeadingDashes = DASHES_REGEX.exec(videoId)[1];
return videoIdWithoutLeadingDashes;
}
export function getYoutubeVideoId(url) {
return YT_REGEX.exec(url)[1];
}